From 1d167a6b9fcc8c9ed494a0cf8d4e2b8406934f8a Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 24 Jul 2018 00:09:11 +0200 Subject: [PATCH] Reraise `SystemCallError` as `ErrorDuringExecution`. --- Library/Homebrew/system_command.rb | 24 +++++++++++--------- Library/Homebrew/test/system_command_spec.rb | 8 +++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Library/Homebrew/system_command.rb b/Library/Homebrew/system_command.rb index cdd74b0c77..c49d773a90 100644 --- a/Library/Homebrew/system_command.rb +++ b/Library/Homebrew/system_command.rb @@ -1,4 +1,5 @@ require "open3" +require "ostruct" require "vendor/plist/plist" require "shellwords" @@ -28,18 +29,15 @@ class SystemCommand def run! @merged_output = [] - @processed_output = { stdout: "", stderr: "" } odebug command.shelljoin each_output_line do |type, line| case type when :stdout puts line.chomp if print_stdout? - processed_output[:stdout] << line @merged_output << [:stdout, line] when :stderr $stderr.puts Formatter.error(line.chomp) if print_stderr? - processed_output[:stderr] << line @merged_output << [:stderr, line] end end @@ -71,7 +69,7 @@ class SystemCommand private - attr_reader :executable, :args, :input, :options, :processed_output, :processed_status, :env + attr_reader :executable, :args, :input, :options, :env attr_predicate :sudo?, :print_stdout?, :print_stderr?, :must_succeed? @@ -94,9 +92,9 @@ class SystemCommand end def assert_success - return if processed_status&.success? + return if @status.success? raise ErrorDuringExecution.new(command, - status: processed_status, + status: @status, output: @merged_output) end @@ -122,7 +120,10 @@ class SystemCommand raw_stdin.close_write each_line_from [raw_stdout, raw_stderr], &b - @processed_status = raw_wait_thr.value + @status = raw_wait_thr.value + rescue SystemCallError => e + @status = $CHILD_STATUS + @merged_output << [:stderr, e.message] end def write_input_to(raw_stdin) @@ -152,10 +153,11 @@ class SystemCommand end def result - Result.new(command, - processed_output[:stdout], - processed_output[:stderr], - processed_status.exitstatus) + output = @merged_output.each_with_object(stdout: "", stderr: "") do |(type, line), hash| + hash[type] << line + end + + Result.new(command, output[:stdout], output[:stderr], @status.exitstatus) end class Result diff --git a/Library/Homebrew/test/system_command_spec.rb b/Library/Homebrew/test/system_command_spec.rb index f57d22f612..0b74d1fbfe 100644 --- a/Library/Homebrew/test/system_command_spec.rb +++ b/Library/Homebrew/test/system_command_spec.rb @@ -208,4 +208,12 @@ describe SystemCommand do expect(described_class.run("tool", env: { "PATH" => path }).stdout).to include "Hello, world!" end end + + describe "#run" do + it "does not raise a `SystemCallError` when the executable does not exist" do + expect { + described_class.run("non_existent_executable") + }.not_to raise_error + end + end end