From f43ecf4031b7d7391804e5eb584a72c1c5c468de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 23 Oct 2018 08:04:29 -0300 Subject: [PATCH 1/2] Remove unused stuff --- spec/fixtures/bundle/execute.rb | 6 ------ spec/fixtures/bundle/main.thor | 1 - 2 files changed, 7 deletions(-) delete mode 100644 spec/fixtures/bundle/execute.rb delete mode 100644 spec/fixtures/bundle/main.thor diff --git a/spec/fixtures/bundle/execute.rb b/spec/fixtures/bundle/execute.rb deleted file mode 100644 index 0530d877c..000000000 --- a/spec/fixtures/bundle/execute.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Execute < Thor - desc "ls", "Execute ls" - def ls - system "ls" - end -end diff --git a/spec/fixtures/bundle/main.thor b/spec/fixtures/bundle/main.thor deleted file mode 100644 index 38bdfbcb7..000000000 --- a/spec/fixtures/bundle/main.thor +++ /dev/null @@ -1 +0,0 @@ -require File.join(File.dirname(__FILE__), 'execute') From b97d86c5c91b83572ae17a32c878a222cec419b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 23 Oct 2018 08:16:02 -0300 Subject: [PATCH 2/2] Add `abort_on_failure` option to #run action --- lib/thor/actions.rb | 11 +++++++++-- spec/actions_spec.rb | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/thor/actions.rb b/lib/thor/actions.rb index cab419405..7a70e2548 100644 --- a/lib/thor/actions.rb +++ b/lib/thor/actions.rb @@ -255,9 +255,16 @@ def run(command, config = {}) say_status :run, desc, config.fetch(:verbose, true) - unless options[:pretend] - config[:capture] ? `#{command}` : system(command.to_s) + return if options[:pretend] + + result = config[:capture] ? `#{command}` : system(command.to_s) + + if config[:abort_on_failure] + success = config[:capture] ? $?.success? : result + abort unless success end + + result end # Executes a ruby script (taking into account WIN32 platform quirks). diff --git a/spec/actions_spec.rb b/spec/actions_spec.rb index b9d933863..c46468ce4 100644 --- a/spec/actions_spec.rb +++ b/spec/actions_spec.rb @@ -291,6 +291,24 @@ def file end 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 + + 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 "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 + describe "when pretending" do it "doesn't execute the command" do runner = MyCounter.new([1], %w(--pretend))