Skip to content

Commit afdcf1c

Browse files
committed
Merge pull request #651 from hibariya/abort-run-on-failure
Abort if the command for `run` fails and `exit_on_failure?` is true
2 parents 400eff5 + 5738a3b commit afdcf1c

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

lib/thor/actions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def run(command, config = {})
269269
success = result
270270
end
271271

272-
abort if config[:abort_on_failure] && !success
272+
abort if !success && config.fetch(:abort_on_failure, self.class.exit_on_failure?)
273273

274274
result
275275
end

lib/thor/base.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@ def handle_argument_error(command, error, args, arity) #:nodoc:
509509
raise InvocationError, msg
510510
end
511511

512+
# A flag that makes the process exit with status 1 if any error happens.
513+
def exit_on_failure?
514+
Thor.deprecation_warning "Thor exit with status 0 on errors. To keep this behavior, you must define `exit_on_failure?` in `#{self.name}`"
515+
false
516+
end
517+
512518
protected
513519

514520
# Prints the class options per group. If an option does not belong to
@@ -646,12 +652,6 @@ def from_superclass(method, default = nil)
646652
end
647653
end
648654

649-
# A flag that makes the process exit with status 1 if any error happens.
650-
def exit_on_failure?
651-
Thor.deprecation_warning 'Thor exit with status 0 on errors. To keep this behavior, you must define `exit_on_failure?`'
652-
false
653-
end
654-
655655
#
656656
# The basename of the program invoking the thor class.
657657
#

spec/actions_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,20 @@ def file
328328
end
329329
end
330330
end
331+
332+
context "exit_on_failure? is true" do
333+
before do
334+
allow(MyCounter).to receive(:exit_on_failure?).and_return(true)
335+
end
336+
337+
it "aborts when command fails even if abort_on_failure is not given" do
338+
expect { action :run, "false" }.to raise_error(SystemExit)
339+
end
340+
341+
it "does not abort when abort_on_failure is false even if the command fails" do
342+
expect { action :run, "false", :abort_on_failure => false }.not_to raise_error
343+
end
344+
end
331345
end
332346

333347
describe "#run_ruby_script" do

spec/fixtures/group.thor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ class MyCounter < Thor::Group
22
include Thor::Actions
33
add_runtime_options!
44

5+
def self.exit_on_failure?
6+
false
7+
end
8+
59
def self.get_from_super
610
from_superclass(:get_from_super, 13)
711
end

0 commit comments

Comments
 (0)