rails / thor

Thor is a toolkit for building powerful command-line interfaces.
http://whatisthor.com/
MIT License
5.12k stars 552 forks source link

How can I raise failed in Thor like bash command return value '1' ? #437

Open dehengxu opened 10 years ago

dehengxu commented 10 years ago

I use thor as command line tools in jenkins, but the command failed and jenkins cannot receive this error .

huwr commented 9 years ago

Would exit or abort solve your problem?

See: http://www.ruby-doc.org/core-2.1.4/Kernel.html#method-i-exit

dkniffin commented 8 years ago

Sorry to comment on an old issue, but my problem is very similar.

exit or abort would work, except when testing Thor. If exit is used, the tests will be interrupted and not finish. I'd like to propose a better solution, which is to evaluate the return value of the command and if it's falsey, exit with a status code of 1. That way, in the tests, if we're expecting a non-zero exit code, we can check for false.

rupurt commented 8 years ago

I've also been running into this problem. I have a thor CLI that runs multiple processes via popen3 and I'd like to return a non-zero status code when any of those processes fail.

rupurt commented 8 years ago

@dkniffin I actually figured out how to do it with exit. exit raises a SystemExit error so I just catch that in my tests. i.e.

expect {
   thor \
      "spec", \
      "spec/thor/fixtures/pass_1_spec_fixture.rb", \
      "spec/thor/fixtures/fail_spec_fixture.rb"
}.to raise_error SystemExit
dkniffin commented 8 years ago

Ah, interesting. I'll check that out.