Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created August 31, 2011 20:54
Show Gist options
  • Save thinkerbot/1184693 to your computer and use it in GitHub Desktop.
Save thinkerbot/1184693 to your computer and use it in GitHub Desktop.
PTY exit status test
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