1 # frozen_string_literal: true
6 class TestOpen3 < Test::Unit::TestCase
10 Open3.popen3(RUBY, '-e', 'exit true') {|i,o,e,t|
11 assert_equal(true, t.value.success?)
13 Open3.popen3(RUBY, '-e', 'exit false') {|i,o,e,t|
14 assert_equal(false, t.value.success?)
19 Open3.popen3(RUBY, '-e', 'exit STDIN.gets.chomp == "t"') {|i,o,e,t|
21 assert_equal(true, t.value.success?)
23 Open3.popen3(RUBY, '-e', 'exit STDIN.gets.chomp == "t"') {|i,o,e,t|
25 assert_equal(false, t.value.success?)
30 Open3.popen3(RUBY, '-e', 'STDOUT.print "foo"') {|i,o,e,t|
31 assert_equal("foo", o.read)
36 Open3.popen3(RUBY, '-e', 'STDERR.print "bar"') {|i,o,e,t|
37 assert_equal("bar", e.read)
42 r = Open3.popen3(RUBY, '-e', 'STDOUT.print STDIN.read') {|i,o,e,t|
45 assert_equal("baz", o.read)
48 assert_equal("qux", r)
52 i,o,e,t = Open3.popen3(RUBY, '-e', 'STDOUT.print STDIN.read')
55 assert_equal("baz", o.read)
64 commandline = "echo quux\n"
65 Open3.popen3(commandline) {|i,o,e,t|
66 assert_equal("quux\n", o.read)
71 Open3.popen3(RUBY, '-e', 'print $$') {|i,o,e,t|
73 assert_equal(pid, t[:pid])
74 assert_equal(pid, t.pid)
79 Open3.popen3({'A' => 'B', 'C' => 'D'}, RUBY, '-e' 'p ENV["A"]') do |i, out, err, thr|
81 assert_equal("\"B\"\n", output)
85 def test_numeric_file_descriptor2
87 Open3.popen2(RUBY, '-e', 'STDERR.puts "foo"', 2 => w) {|i,o,t|
88 assert_equal("foo\n", r.gets)
93 def test_numeric_file_descriptor3
94 omit "passing FDs bigger than 2 is not supported on Windows" if /mswin|mingw/ =~ RbConfig::CONFIG['host_os']
96 Open3.popen3(RUBY, '-e', 'IO.open(3).puts "foo"', 3 => w) {|i,o,e,t|
97 assert_equal("foo\n", r.gets, "[GH-808] [ruby-core:67347] [Bug #10699]")
110 def with_reopen(io, arg)
121 with_reopen(STDERR, w) {|old|
123 Open3.popen2(RUBY, '-e', 's=STDIN.read; STDOUT.print s+"o"; STDERR.print s+"e"') {|i,o,t|
124 assert_kind_of(Thread, t)
128 assert_equal("zo", o.read)
129 assert_equal("ze", r.read)
137 with_reopen(STDERR, w) {|old|
139 Open3.popen2e(RUBY, '-e', 's=STDIN.read; STDOUT.print s+"o"; STDOUT.flush; STDERR.print s+"e"') {|i,o,t|
140 assert_kind_of(Thread, t)
144 assert_equal("yoye", o.read)
145 assert_equal("", r.read)
151 def test_popen2e_noblock
152 i, o, t = Open3.popen2e(RUBY, '-e', 'STDOUT.print STDIN.read')
155 assert_equal("baz", o.read)
163 o, e, s = Open3.capture3(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>"i")
164 assert_equal("io", o)
165 assert_equal("ie", e)
169 def test_capture3_stdin_data_io
173 o, e, s = Open3.capture3(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>r)
174 assert_equal("io", o)
175 assert_equal("ie", e)
180 def test_capture3_flip
181 o, e, s = Open3.capture3(RUBY, '-e', 'STDOUT.sync=true; 1000.times { print "o"*1000; STDERR.print "e"*1000 }')
182 assert_equal("o"*1000000, o)
183 assert_equal("e"*1000000, e)
188 o, s = Open3.capture2(RUBY, '-e', 'i=STDIN.read; print i+"o"', :stdin_data=>"i")
189 assert_equal("io", o)
193 def test_capture2_stdin_data_io
197 o, s = Open3.capture2(RUBY, '-e', 'i=STDIN.read; print i+"o"', :stdin_data=>r)
198 assert_equal("io", o)
204 oe, s = Open3.capture2e(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>"i")
205 assert_equal("ioie", oe)
209 def test_capture2e_stdin_data_io
213 oe, s = Open3.capture2e(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>r)
214 assert_equal("ioie", oe)
219 def test_capture3_stdin_data
220 o, e, s = Open3.capture3(RUBY, '-e', '', :stdin_data=>"z"*(1024*1024))
226 def test_capture2_stdin_data
227 o, s = Open3.capture2(RUBY, '-e', '', :stdin_data=>"z"*(1024*1024))
232 def test_capture2e_stdin_data
233 oe, s = Open3.capture2e(RUBY, '-e', '', :stdin_data=>"z"*(1024*1024))
239 Open3.pipeline_rw([RUBY, '-e', 'print STDIN.read + "1"'],
240 [RUBY, '-e', 'print STDIN.read + "2"']) {|i,o,ts|
241 assert_kind_of(IO, i)
242 assert_kind_of(IO, o)
243 assert_kind_of(Array, ts)
244 assert_equal(2, ts.length)
245 ts.each {|t| assert_kind_of(Thread, t) }
248 assert_equal("012", o.read)
250 assert(t.value.success?)
256 Open3.pipeline_r([RUBY, '-e', 'print "1"'],
257 [RUBY, '-e', 'print STDIN.read + "2"']) {|o,ts|
258 assert_kind_of(IO, o)
259 assert_kind_of(Array, ts)
260 assert_equal(2, ts.length)
261 ts.each {|t| assert_kind_of(Thread, t) }
262 assert_equal("12", o.read)
264 assert(t.value.success?)
270 command = [RUBY, '-e', 's=STDIN.read; print s[1..-1]; exit s[0] == ?t']
272 Open3.pipeline_w(*[command]*str.length) {|i,ts|
273 assert_kind_of(IO, i)
274 assert_kind_of(Array, ts)
275 assert_equal(str.length, ts.length)
276 ts.each {|t| assert_kind_of(Thread, t) }
279 ts.each_with_index {|t, ii|
280 assert_equal(str[ii] == ?t, t.value.success?)
285 def test_pipeline_start
286 command = [RUBY, '-e', 's=STDIN.read; print s[1..-1]; exit s[0] == ?t']
288 Open3.pipeline_start([RUBY, '-e', 'print ARGV[0]', str],
289 *([command]*str.length)) {|ts|
290 assert_kind_of(Array, ts)
291 assert_equal(str.length+1, ts.length)
292 ts.each {|t| assert_kind_of(Thread, t) }
293 ts.each_with_index {|t, i|
295 assert(t.value.success?)
297 assert_equal(str[i-1] == ?t, t.value.success?)
303 def test_pipeline_start_noblock
304 ts = Open3.pipeline_start([RUBY, '-e', ''])
305 assert_kind_of(Array, ts)
306 assert_equal(1, ts.length)
307 ts.each {|t| assert_kind_of(Thread, t) }
309 assert(t.value.success?)
313 command = [RUBY, '-e', 's=STDIN.read; print s[1..-1]; exit s[0] == ?t']
315 ss = Open3.pipeline([RUBY, '-e', 'print ARGV[0]', str],
316 *([command]*str.length))
317 assert_kind_of(Array, ss)
318 assert_equal(str.length+1, ss.length)
319 ss.each {|s| assert_kind_of(Process::Status, s) }
320 ss.each_with_index {|s, i|
324 assert_equal(str[i-1] == ?t, s.success?)
329 def test_integer_and_symbol_key
330 command = [RUBY, '-e', 'puts "test_integer_and_symbol_key"']
331 out, status = Open3.capture2(*command, :chdir => '.', 2 => IO::NULL)
332 assert_equal("test_integer_and_symbol_key\n", out)
333 assert_predicate(status, :success?)