From 6ae142173476f7ad58a5b8b1bb661cf3b438143f Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Wed, 10 Jan 2024 15:42:29 +0900 Subject: [PATCH] Define capture_subprocess_io from minitest --- test/test_rake_file_utils.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/test_rake_file_utils.rb b/test/test_rake_file_utils.rb index a4d4adca7..1df6ce4cb 100644 --- a/test/test_rake_file_utils.rb +++ b/test/test_rake_file_utils.rb @@ -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\""