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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
require 'test/unit'
require_relative 'ruby/envutil'
require 'shellwords'
require 'tmpdir'
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"')
rescue RuntimeError
skip $!
else
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)
}
rescue RuntimeError
skip $!
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)
}
rescue RuntimeError
skip $!
end
def test_argv0
PTY.spawn([RUBY, "argv0"], '-e', 'puts "bar"') {|r,w,pid|
assert_equal("bar\r\n", r.gets)
Process.wait(pid)
}
rescue RuntimeError
skip $!
end
def test_open_without_block
ret = PTY.open
rescue RuntimeError
skip $!
else
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
}
rescue RuntimeError
skip $!
else
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?)
}
rescue RuntimeError
skip $!
else
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)
}
rescue RuntimeError
skip $!
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)
}
rescue RuntimeError
skip $!
end
def test_close_master
PTY.open {|master, slave|
master.close
assert_raise(EOFError) { slave.readpartial(10) }
}
rescue RuntimeError
skip $!
end
def test_close_slave
PTY.open {|master, slave|
slave.close
# This exception is platform dependent.
assert_raise(
EOFError, # FreeBSD
Errno::EIO # GNU/Linux
) { master.readpartial(10) }
}
rescue RuntimeError
skip $!
end
def test_getpty_nonexistent
bug3672 = '[ruby-dev:41965]'
Dir.mktmpdir do |tmpdir|
assert_raise(Errno::ENOENT, bug3672) {
begin
PTY.getpty(File.join(tmpdir, "no-such-command"))
rescue RuntimeError
skip $!
end
}
end
end
def test_pty_check_default
st1 = st2 = pid = nil
`echo` # preset $?
PTY.spawn("cat") do |r,w,id|
pid = id
st1 = PTY.check(pid)
w.close
r.close
begin
sleep(0.1)
end until st2 = PTY.check(pid)
end
rescue RuntimeError
skip $!
else
assert_equal(pid, st1.pid) if st1
assert_nil(st1)
assert_equal(pid, st2.pid)
end
def test_pty_check_raise
bug2642 = '[ruby-dev:44600]'
st1 = st2 = pid = nil
PTY.spawn("cat") do |r,w,id|
pid = id
assert_nothing_raised(PTY::ChildExited, bug2642) {st1 = PTY.check(pid, true)}
w.close
r.close
sleep(0.1)
st2 = assert_raise(PTY::ChildExited, bug2642) {PTY.check(pid, true)}.status
end
rescue RuntimeError
skip $!
else
assert_equal(pid, st1.pid) if st1
assert_nil(st1)
assert_equal(pid, st2.pid)
end
def test_cloexec
PTY.open {|m, s|
assert(m.close_on_exec?)
assert(s.close_on_exec?)
}
PTY.spawn(RUBY, '-e', '') {|r, w, pid|
assert(r.close_on_exec?)
assert(w.close_on_exec?)
}
rescue RuntimeError
skip $!
end
end if defined? PTY
|