1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
require 'test_case'
module Open4
class POpen4Test < TestCase
UNKNOWN_CMD = 'asdfadsfjlkkk'
UNKNOWN_CMD_ERRORS = [Errno::ENOENT, Errno::EINVAL]
def test_unknown_command_propagates_exception
err = assert_raises(*UNKNOWN_CMD_ERRORS) { popen4ext true, UNKNOWN_CMD }
assert_match /#{UNKNOWN_CMD}/, err.to_s if on_mri?
end
def test_exception_propagation_avoids_zombie_child_process
assert_raises(*UNKNOWN_CMD_ERRORS) { popen4ext true, UNKNOWN_CMD }
assert_empty Process.waitall
end
def test_exit_failure
code = 43
cid, _ = popen4ext true, %{ruby -e "exit #{43}"}
assert_equal code, wait_status(cid)
end
def test_exit_success
cid, _ = popen4ext true, %{ruby -e "exit"}
assert_equal 0, wait_status(cid)
end
def test_passes_child_pid_to_block
cmd = %{ruby -e "STDOUT.print Process.pid"}
cid_in_block = nil
cid_in_fun = nil
status = popen4ext(true, cmd) do |cid, _, stdout, _|
cid_in_block = cid
cid_in_fun = stdout.read.to_i
end
assert_equal cid_in_fun, cid_in_block
end
def test_io_pipes_without_block
via_msg = 'foo'
err_msg = 'bar'
cmd = <<-END
ruby -e "
STDOUT.write STDIN.read
STDERR.write '#{err_msg}'
"
END
cid, stdin, stdout, stderr = popen4ext true, cmd
stdin.write via_msg
stdin.close
out_actual = stdout.read
err_actual = stderr.read
assert_equal via_msg, out_actual
assert_equal err_msg, err_actual
assert_equal 0, wait_status(cid)
end
def test_io_pipes_with_block
via_msg = 'foo'
err_msg = 'bar'
out_actual, err_actual = nil
cmd = <<-END
ruby -e "
STDOUT.write STDIN.read
STDERR.write '#{err_msg}'
"
END
status = popen4ext(true, cmd) do |_, stdin, stdout, stderr|
stdin.write via_msg
stdin.close
out_actual = stdout.read
err_actual = stderr.read
end
assert_equal via_msg, out_actual
assert_equal err_msg, err_actual
assert_equal 0, status.exitstatus
end
end
end
|