From 246db8a134f162b764c36cd0050c8303a2197615 Mon Sep 17 00:00:00 2001 From: Claudia Date: Sun, 30 Aug 2020 22:36:28 +0200 Subject: [PATCH] Capture stdout during `popen_write` Fix tests and fulfill intended semantics by having `popen_write` transparently capture standard output. --- Library/Homebrew/utils/popen.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/utils/popen.rb b/Library/Homebrew/utils/popen.rb index f9d189219c..18cd057fe8 100644 --- a/Library/Homebrew/utils/popen.rb +++ b/Library/Homebrew/utils/popen.rb @@ -12,8 +12,25 @@ module Utils raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]]) end - def self.popen_write(*args, **options, &block) - popen(args, "wb", options, &block) + def self.popen_write(*args, **options) + 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 def self.safe_popen_write(*args, **options, &block)