diff --git a/Library/Homebrew/build.rb b/Library/Homebrew/build.rb index 03814cc855..ce81f72772 100644 --- a/Library/Homebrew/build.rb +++ b/Library/Homebrew/build.rb @@ -240,7 +240,14 @@ rescue Exception => e # rubocop:disable Lint/RescueException error_hash["env"] = e.env when "ErrorDuringExecution" error_hash["cmd"] = e.cmd - error_hash["status"] = e.status.exitstatus + error_hash["status"] = if e.status.is_a?(Process::Status) + { + exitstatus: e.status.exitstatus, + termsig: e.status.termsig, + } + else + e.status + end error_hash["output"] = e.output end diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb index 9fca2b5b9e..55b15062da 100644 --- a/Library/Homebrew/exceptions.rb +++ b/Library/Homebrew/exceptions.rb @@ -579,19 +579,32 @@ class ErrorDuringExecution < RuntimeError @status = status @output = output + raise ArgumentError, "Status cannot be nil." if status.nil? + exitstatus = case status when Integer status + when Hash + status["exitstatus"] else - status&.exitstatus + status.exitstatus + end + + termsig = case status + when Integer + nil + when Hash + status["termsig"] + else + status.termsig end redacted_cmd = redact_secrets(cmd.shelljoin.gsub('\=', "="), secrets) reason = if exitstatus "exited with #{exitstatus}" - elsif (uncaught_signal = status&.termsig) - "was terminated by uncaught signal #{Signal.signame(uncaught_signal)}" + elsif termsig + "was terminated by uncaught signal #{Signal.signame(termsig)}" else raise ArgumentError, "Status neither has `exitstatus` nor `termsig`." end diff --git a/Library/Homebrew/test/error_during_execution_spec.rb b/Library/Homebrew/test/error_during_execution_spec.rb index a3f6221198..a2fdcc3a9d 100644 --- a/Library/Homebrew/test/error_during_execution_spec.rb +++ b/Library/Homebrew/test/error_during_execution_spec.rb @@ -5,7 +5,7 @@ describe ErrorDuringExecution do subject(:error) { described_class.new(command, status: status, output: output) } let(:command) { ["false"] } - let(:status) { instance_double(Process::Status, exitstatus: exitstatus) } + let(:status) { instance_double(Process::Status, exitstatus: exitstatus, termsig: nil) } let(:exitstatus) { 1 } let(:output) { nil } diff --git a/Library/Homebrew/test/exceptions_spec.rb b/Library/Homebrew/test/exceptions_spec.rb index 379e8a448d..5fd44a1271 100644 --- a/Library/Homebrew/test/exceptions_spec.rb +++ b/Library/Homebrew/test/exceptions_spec.rb @@ -186,7 +186,7 @@ describe "Exception" do describe ErrorDuringExecution do subject { described_class.new(["badprg", "arg1", "arg2"], status: status) } - let(:status) { instance_double(Process::Status, exitstatus: 17) } + let(:status) { instance_double(Process::Status, exitstatus: 17, termsig: nil) } its(:to_s) { is_expected.to eq("Failure while executing; `badprg arg1 arg2` exited with 17.") } end diff --git a/Library/Homebrew/utils/fork.rb b/Library/Homebrew/utils/fork.rb index 7094e7d8f4..ac1ff1182a 100644 --- a/Library/Homebrew/utils/fork.rb +++ b/Library/Homebrew/utils/fork.rb @@ -51,7 +51,14 @@ module Utils # to rescue them further down. if e.is_a?(ErrorDuringExecution) error_hash["cmd"] = e.cmd - error_hash["status"] = e.status.exitstatus + error_hash["status"] = if e.status.is_a?(Process::Status) + { + exitstatus: e.status.exitstatus, + termsig: e.status.termsig, + } + else + e.status + end error_hash["output"] = e.output end