Skip to content

Commit b97d86c

Browse files
Add abort_on_failure option to #run action
1 parent f43ecf4 commit b97d86c

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/thor/actions.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,16 @@ def run(command, config = {})
255255

256256
say_status :run, desc, config.fetch(:verbose, true)
257257

258-
unless options[:pretend]
259-
config[:capture] ? `#{command}` : system(command.to_s)
258+
return if options[:pretend]
259+
260+
result = config[:capture] ? `#{command}` : system(command.to_s)
261+
262+
if config[:abort_on_failure]
263+
success = config[:capture] ? $?.success? : result
264+
abort unless success
260265
end
266+
267+
result
261268
end
262269

263270
# Executes a ruby script (taking into account WIN32 platform quirks).

spec/actions_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,24 @@ def file
291291
end
292292
end
293293

294+
describe "aborting on failure" do
295+
it "aborts when abort_on_failure is given and command fails" do
296+
expect { action :run, "false", :abort_on_failure => true }.to raise_error(SystemExit)
297+
end
298+
299+
it "suceeds when abort_on_failure is given and command succeeds" do
300+
expect { action :run, "true", :abort_on_failure => true }.not_to raise_error
301+
end
302+
303+
it "aborts when abort_on_failure is given, capture is given and command fails" do
304+
expect { action :run, "false", :abort_on_failure => true, :capture => true }.to raise_error(SystemExit)
305+
end
306+
307+
it "suceeds when abort_on_failure is given and command succeeds" do
308+
expect { action :run, "true", :abort_on_failure => true, :capture => true }.not_to raise_error
309+
end
310+
end
311+
294312
describe "when pretending" do
295313
it "doesn't execute the command" do
296314
runner = MyCounter.new([1], %w(--pretend))

0 commit comments

Comments
 (0)