Reraise SystemCallError as ErrorDuringExecution.
This commit is contained in:
parent
eccd20e276
commit
1d167a6b9f
@ -1,4 +1,5 @@
|
|||||||
require "open3"
|
require "open3"
|
||||||
|
require "ostruct"
|
||||||
require "vendor/plist/plist"
|
require "vendor/plist/plist"
|
||||||
require "shellwords"
|
require "shellwords"
|
||||||
|
|
||||||
@ -28,18 +29,15 @@ class SystemCommand
|
|||||||
|
|
||||||
def run!
|
def run!
|
||||||
@merged_output = []
|
@merged_output = []
|
||||||
@processed_output = { stdout: "", stderr: "" }
|
|
||||||
odebug command.shelljoin
|
odebug command.shelljoin
|
||||||
|
|
||||||
each_output_line do |type, line|
|
each_output_line do |type, line|
|
||||||
case type
|
case type
|
||||||
when :stdout
|
when :stdout
|
||||||
puts line.chomp if print_stdout?
|
puts line.chomp if print_stdout?
|
||||||
processed_output[:stdout] << line
|
|
||||||
@merged_output << [:stdout, line]
|
@merged_output << [:stdout, line]
|
||||||
when :stderr
|
when :stderr
|
||||||
$stderr.puts Formatter.error(line.chomp) if print_stderr?
|
$stderr.puts Formatter.error(line.chomp) if print_stderr?
|
||||||
processed_output[:stderr] << line
|
|
||||||
@merged_output << [:stderr, line]
|
@merged_output << [:stderr, line]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -71,7 +69,7 @@ class SystemCommand
|
|||||||
|
|
||||||
private
|
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?
|
attr_predicate :sudo?, :print_stdout?, :print_stderr?, :must_succeed?
|
||||||
|
|
||||||
@ -94,9 +92,9 @@ class SystemCommand
|
|||||||
end
|
end
|
||||||
|
|
||||||
def assert_success
|
def assert_success
|
||||||
return if processed_status&.success?
|
return if @status.success?
|
||||||
raise ErrorDuringExecution.new(command,
|
raise ErrorDuringExecution.new(command,
|
||||||
status: processed_status,
|
status: @status,
|
||||||
output: @merged_output)
|
output: @merged_output)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -122,7 +120,10 @@ class SystemCommand
|
|||||||
raw_stdin.close_write
|
raw_stdin.close_write
|
||||||
each_line_from [raw_stdout, raw_stderr], &b
|
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
|
end
|
||||||
|
|
||||||
def write_input_to(raw_stdin)
|
def write_input_to(raw_stdin)
|
||||||
@ -152,10 +153,11 @@ class SystemCommand
|
|||||||
end
|
end
|
||||||
|
|
||||||
def result
|
def result
|
||||||
Result.new(command,
|
output = @merged_output.each_with_object(stdout: "", stderr: "") do |(type, line), hash|
|
||||||
processed_output[:stdout],
|
hash[type] << line
|
||||||
processed_output[:stderr],
|
end
|
||||||
processed_status.exitstatus)
|
|
||||||
|
Result.new(command, output[:stdout], output[:stderr], @status.exitstatus)
|
||||||
end
|
end
|
||||||
|
|
||||||
class Result
|
class Result
|
||||||
|
|||||||
@ -208,4 +208,12 @@ describe SystemCommand do
|
|||||||
expect(described_class.run("tool", env: { "PATH" => path }).stdout).to include "Hello, world!"
|
expect(described_class.run("tool", env: { "PATH" => path }).stdout).to include "Hello, world!"
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user