Capture stdout during popen_write

Fix tests and fulfill intended semantics by having `popen_write`
transparently capture standard output.
This commit is contained in:
Claudia 2020-08-30 22:36:28 +02:00
parent 772032f18a
commit 246db8a134
No known key found for this signature in database
GPG Key ID: 246AC3C0F10BE51F

View File

@ -12,8 +12,25 @@ module Utils
raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]]) raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]])
end end
def self.popen_write(*args, **options, &block) def self.popen_write(*args, **options)
popen(args, "wb", options, &block) popen(args, "w+b", options) do |pipe|
output = ""
# Before we yield to the block, capture as much output as we can
loop do
output += pipe.read_nonblock(4096)
rescue IO::WaitReadable, EOFError
break
end
yield pipe
pipe.close_write
IO.select([pipe])
# Capture the rest of the output
output += pipe.read
output.freeze
end
end end
def self.safe_popen_write(*args, **options, &block) def self.safe_popen_write(*args, **options, &block)