Created
August 31, 2011 20:54
-
-
Save thinkerbot/1184693 to your computer and use it in GitHub Desktop.
PTY exit status test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pty' | |
require 'test/unit' | |
# See also: | |
# * http://stackoverflow.com/questions/3874604/pty-spawn-child-exit-code/7263243#7263243 | |
# * http://redmine.ruby-lang.org/issues/5253 | |
class PTYTest < Test::Unit::TestCase | |
def setup | |
system "true" | |
assert_equal 0, $?.exitstatus | |
end | |
def pty(cmd, &block) | |
PTY.spawn(cmd, &block) | |
$?.exitstatus | |
end | |
def test_pty_with_wait_correctly_sets_exit_status_for_master_slave_io | |
status = pty("printf 'abc'; exit 8") do |r,w,pid| | |
while !r.eof? | |
r.getc | |
end | |
Process.wait(pid) | |
end | |
assert_equal 8, status | |
end | |
def test_pty_with_wait_correctly_sets_exit_status_for_basic_commands | |
status = pty("true") do |r,w,pid| | |
Process.wait(pid) | |
end | |
assert_equal 0, status | |
status = pty("false") do |r,w,pid| | |
Process.wait(pid) | |
end | |
assert_equal 1, status | |
end | |
def test_pty_with_wait_sets_exit_status_1_for_immediate_exit | |
status = pty("exit 8") do |r,w,pid| | |
Process.wait(pid) | |
end | |
assert_equal 1, status | |
end | |
def test_pty_with_kill | |
status = pty("sleep 10") do |r,w,pid| | |
Process.kill(9, pid) | |
Process.wait(pid) | |
end | |
assert_equal nil, status | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment