Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/thor/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def run(command, config = {})

result = config[:capture] ? `#{command}` : system(command.to_s)

if config[:abort_on_failure]
if config.fetch(:abort_on_failure, self.class.exit_on_failure?)
success = config[:capture] ? $?.success? : result
abort unless success
end
Expand Down
10 changes: 5 additions & 5 deletions lib/thor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ 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?
false
end

protected

# Prints the class options per group. If an option does not belong to
Expand Down Expand Up @@ -641,11 +646,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?
false
end

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

describe "aborting on failure" do
it "aborts when abort_on_failure is given and command fails" do
expect { action :run, "false", :abort_on_failure => true }.to raise_error(SystemExit)
end
context "exit_on_failure? is false" do
before do
allow(MyCounter).to receive(:exit_on_failure?).and_return(false)
end

it "suceeds when abort_on_failure is given and command succeeds" do
expect { action :run, "true", :abort_on_failure => true }.not_to raise_error
end
it "aborts when abort_on_failure is given and command fails" do
expect { action :run, "false", :abort_on_failure => true }.to raise_error(SystemExit)
end

it "suceeds when abort_on_failure is given and command succeeds" do
expect { action :run, "true", :abort_on_failure => true }.not_to raise_error
end

it "aborts when abort_on_failure is given, capture is given and command fails" do
expect { action :run, "false", :abort_on_failure => true, :capture => true }.to raise_error(SystemExit)
end

it "aborts when abort_on_failure is given, capture is given and command fails" do
expect { action :run, "false", :abort_on_failure => true, :capture => true }.to raise_error(SystemExit)
it "suceeds when abort_on_failure is given and command succeeds" do
expect { action :run, "true", :abort_on_failure => true, :capture => true }.not_to raise_error
end
end

it "suceeds when abort_on_failure is given and command succeeds" do
expect { action :run, "true", :abort_on_failure => true, :capture => true }.not_to raise_error
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

Expand Down