TL; DR
Use 1.9.2 and wait while the PTY process correctly sets $?
PTY.spawn(command) do |r,w,pid|
Process.wait(pid)
end
Full story
1.9.2 PTY, PTY pid. (AFAIK). , , - , , (. http://redmine.ruby-lang.org/issues/5253).
:
require 'pty'
require 'test/unit'
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
:
$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]
$ ruby example.rb
Loaded suite example
Started
....
Finished in 1.093349 seconds.
4 tests, 9 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 31924
1.8.7 . rubies PTY PTY:: ChildExited, PTY. , , :
$ ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]
$ ruby example.rb
Loaded suite example
Started
EE.E
Finished in 1.170357 seconds.
1) Error:
test_pty_with_kill(PTYTest):
PTY::ChildExited: pty - exited: 35196
example.rb:11:in `test_pty_with_kill'
example.rb:11:in `spawn'
example.rb:11:in `pty'
example.rb:45:in `test_pty_with_kill'
2) Error:
test_pty_with_wait_correctly_sets_exit_status_for_basic_commands(PTYTest):
PTY::ChildExited: pty - exited: 35198
example.rb:11:in `test_pty_with_wait_correctly_sets_exit_status_for_basic_commands'
example.rb:11:in `spawn'
example.rb:11:in `pty'
example.rb:26:in `test_pty_with_wait_correctly_sets_exit_status_for_basic_commands'
3) Error:
test_pty_with_wait_sets_exit_status_1_for_immediate_exit(PTYTest):
PTY::ChildExited: pty - exited: 35202
example.rb:11:in `test_pty_with_wait_sets_exit_status_1_for_immediate_exit'
example.rb:11:in `spawn'
example.rb:11:in `pty'
example.rb:38:in `test_pty_with_wait_sets_exit_status_1_for_immediate_exit'
4 tests, 5 assertions, 0 failures, 3 errors
, ChildExited, (, , PTY) , . , , , 1.9.2.
. ChildExited, - :
def pty(cmd, &block)
begin
PTY.spawn(cmd, &block)
$?.exitstatus
rescue PTY::ChildExited
$!.status.exitstatus
end
end
, , , 1.9.2, , $? ( 1.9.2). , :
def test_setting_of_process_status
system "true"
assert_equal 0, $?.exitstatus
begin
PTY.spawn("false") do |r,w,pid|
Process.wait(pid)
end
rescue PTY::ChildExited
end
assert_equal 1, $?.exitstatus
end
1.9.2, 1.8.7. 1.8.7 PTY ChildExited - Process.wait , , $?. $? "" true "" , 0 1 .
$? , (.. PTY Process.wait).