Skip to content

Commit 8b2ca6d

Browse files
committed
Import capture_subprocess_io from minitest
1 parent a091004 commit 8b2ca6d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

lib/rubygems/test_case.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,43 @@ def assert_directory_exists(path, msg = nil)
145145
assert File.directory?(path), msg
146146
end
147147

148+
# https://github.com/seattlerb/minitest/blob/master/lib/minitest/assertions.rb#L188
149+
def _synchronize
150+
yield
151+
end
152+
153+
# https://github.com/seattlerb/minitest/blob/master/lib/minitest/assertions.rb#L546
154+
def capture_subprocess_io
155+
_synchronize do
156+
begin
157+
require "tempfile"
158+
159+
captured_stdout, captured_stderr = Tempfile.new("out"), Tempfile.new("err")
160+
161+
orig_stdout, orig_stderr = $stdout.dup, $stderr.dup
162+
$stdout.reopen captured_stdout
163+
$stderr.reopen captured_stderr
164+
165+
yield
166+
167+
$stdout.rewind
168+
$stderr.rewind
169+
170+
return captured_stdout.read, captured_stderr.read
171+
ensure
172+
captured_stdout.unlink
173+
captured_stderr.unlink
174+
$stdout.reopen orig_stdout
175+
$stderr.reopen orig_stderr
176+
177+
orig_stdout.close
178+
orig_stderr.close
179+
captured_stdout.close
180+
captured_stderr.close
181+
end
182+
end
183+
end
184+
148185
##
149186
# Sets the ENABLE_SHARED entry in RbConfig::CONFIG to +value+ and restores
150187
# the original value when the block ends

0 commit comments

Comments
 (0)