Skip to content

Commit

Permalink
Merge pull request #651 from hibariya/abort-run-on-failure
Browse files Browse the repository at this point in the history
Abort if the command for `run` fails and `exit_on_failure?` is true
  • Loading branch information
rafaelfranca committed Nov 16, 2019
2 parents 400eff5 + 5738a3b commit afdcf1c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/thor/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def run(command, config = {})
success = result
end

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

result
end
Expand Down
12 changes: 6 additions & 6 deletions lib/thor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ def handle_argument_error(command, error, args, arity) #:nodoc:
raise InvocationError, msg
end

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

protected

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

# A flag that makes the process exit with status 1 if any error happens.
def exit_on_failure?
Thor.deprecation_warning 'Thor exit with status 0 on errors. To keep this behavior, you must define `exit_on_failure?`'
false
end

#
# The basename of the program invoking the thor class.
#
Expand Down
14 changes: 14 additions & 0 deletions spec/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,20 @@ def file
end
end
end

context "exit_on_failure? is true" do
before do
allow(MyCounter).to receive(:exit_on_failure?).and_return(true)
end

it "aborts when command fails even if abort_on_failure is not given" do
expect { action :run, "false" }.to raise_error(SystemExit)
end

it "does not abort when abort_on_failure is false even if the command fails" do
expect { action :run, "false", :abort_on_failure => false }.not_to raise_error
end
end
end

describe "#run_ruby_script" do
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/group.thor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ class MyCounter < Thor::Group
include Thor::Actions
add_runtime_options!

def self.exit_on_failure?
false
end

def self.get_from_super
from_superclass(:get_from_super, 13)
end
Expand Down

0 comments on commit afdcf1c

Please sign in to comment.