Skip to content

Commit e151a21

Browse files
authored
Merge pull request #628 from deivid-rodriguez/abort_on_failure
Abort when `run` subprocess fail
2 parents ed27fd9 + b97d86c commit e151a21

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
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))

spec/fixtures/bundle/execute.rb

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/fixtures/bundle/main.thor

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)