Method deprecated or moved
This method is deprecated or moved on the latest stable version.
The last existing version (v2_1_10) is shown here.
capture_subprocess_io()
public
Captures $stdout and $stderr into strings, using Tempfile to ensure that subprocess IO is captured as well.
out, err = capture_subprocess_io do
system "echo Some info"
system "echo You did a bad thing 1>&2"
end
assert_match %r%info%, out
assert_match %r%bad%, err
NOTE: This method is approximately 10x slower than #capture_io so only use it
when you need to test the output of a subprocess.
# File lib/minitest/unit.rb, line 503
def capture_subprocess_io
require 'tempfile'
captured_stdout, captured_stderr = Tempfile.new("out"), Tempfile.new("err")
synchronize do
orig_stdout, orig_stderr = $stdout.dup, $stderr.dup
$stdout.reopen captured_stdout
$stderr.reopen captured_stderr
begin
yield
$stdout.rewind
$stderr.rewind
[captured_stdout.read, captured_stderr.read]
ensure
captured_stdout.unlink
captured_stderr.unlink
$stdout.reopen orig_stdout
$stderr.reopen orig_stderr
end
end
end