1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/test_pty.rb
akr e27fbfbc98 * ext/pty/pty.c (getDevice): add nomesg argument.
(get_device_once): add nomesg argument.  chmod slave tty to 0600
  if nomesg.  more error tests.
  (no_mesg): new function.
  (pty_open): make slave tty's mode 0600.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20795 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-16 11:39:17 +00:00

109 lines
2.2 KiB
Ruby

require 'test/unit'
require_relative 'ruby/envutil'
require 'shellwords'
begin
require 'pty'
rescue LoadError
end
class TestPTY < Test::Unit::TestCase
RUBY = EnvUtil.rubybin
def test_spawn_without_block
r, w, pid = PTY.spawn(RUBY, '-e', 'puts "a"')
assert_equal("a\r\n", r.gets)
ensure
Process.wait pid if pid
end
def test_spawn_with_block
PTY.spawn(RUBY, '-e', 'puts "b"') {|r,w,pid|
assert_equal("b\r\n", r.gets)
Process.wait(pid)
}
end
def test_commandline
commandline = Shellwords.join([RUBY, '-e', 'puts "foo"'])
PTY.spawn(commandline) {|r,w,pid|
assert_equal("foo\r\n", r.gets)
Process.wait(pid)
}
end
def test_argv0
PTY.spawn([RUBY, "argv0"], '-e', 'puts "bar"') {|r,w,pid|
assert_equal("bar\r\n", r.gets)
Process.wait(pid)
}
end
def test_open_without_block
ret = PTY.open
assert_kind_of(Array, ret)
assert_equal(2, ret.length)
assert_equal(IO, ret[0].class)
assert_equal(File, ret[1].class)
master, slave = ret
assert(slave.tty?)
assert(File.chardev?(slave.path))
ensure
if ret
ret[0].close
ret[1].close
end
end
def test_open_with_block
r = nil
x = Object.new
y = PTY.open {|ret|
r = ret;
assert_kind_of(Array, ret)
assert_equal(2, ret.length)
assert_equal(IO, ret[0].class)
assert_equal(File, ret[1].class)
master, slave = ret
assert(slave.tty?)
assert(File.chardev?(slave.path))
x
}
assert(r[0].closed?)
assert(r[1].closed?)
assert_equal(y, x)
end
def test_close_in_block
PTY.open {|master, slave|
slave.close
master.close
assert(slave.closed?)
assert(master.closed?)
}
assert_nothing_raised {
PTY.open {|master, slave|
slave.close
master.close
}
}
end
def test_open
PTY.open {|master, slave|
slave.puts "foo"
assert_equal("foo", master.gets.chomp)
master.puts "bar"
assert_equal("bar", slave.gets.chomp)
}
end
def test_stat_slave
PTY.open {|master, slave|
s = File.stat(slave.path)
assert_equal(Process.uid, s.uid)
assert_equal(0600, s.mode & 0777)
}
end
end if defined? PTY