Skip to content

Commit

Permalink
Define capture_subprocess_io from minitest
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbt committed Jan 10, 2024
1 parent b99a21e commit 6ae1421
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/test_rake_file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,41 @@ def test_sh_if_a_command_exits_with_error_status_sh_echoes_it_fully
end
end

# https://github.com/seattlerb/minitest/blob/21d9e804b63c619f602f3f4ece6c71b48974707a/lib/minitest/assertions.rb#L188
def _synchronize
yield
end

# https://github.com/seattlerb/minitest/blob/21d9e804b63c619f602f3f4ece6c71b48974707a/lib/minitest/assertions.rb#L546
def capture_subprocess_io
_synchronize do
require "tempfile"

captured_stdout = Tempfile.new("out")
captured_stderr = Tempfile.new("err")

orig_stdout = $stdout.dup
orig_stderr = $stderr.dup
$stdout.reopen captured_stdout
$stderr.reopen captured_stderr

yield

$stdout.rewind
$stderr.rewind

return captured_stdout.read, captured_stderr.read
ensure
$stdout.reopen orig_stdout
$stderr.reopen orig_stderr

orig_stdout.close
orig_stderr.close
captured_stdout.close!
captured_stderr.close!
end
end

def assert_echoes_fully
long_string = "1234567890" * 10
shell_command = "ruby -e\"'#{long_string}';exit false\""
Expand Down

0 comments on commit 6ae1421

Please sign in to comment.