1 # frozen_string_literal: true
6 if RUBY_ENGINE == 'ruby'
7 require_relative 'lib/jit_support'
10 class TestOpen3 < Test::Unit::TestCase
11 RUBY = EnvUtil.rubybin
14 Open3.popen3(RUBY, '-e', 'exit true') {|i,o,e,t|
15 assert_equal(true, t.value.success?)
17 Open3.popen3(RUBY, '-e', 'exit false') {|i,o,e,t|
18 assert_equal(false, t.value.success?)
23 Open3.popen3(RUBY, '-e', 'exit STDIN.gets.chomp == "t"') {|i,o,e,t|
25 assert_equal(true, t.value.success?)
27 Open3.popen3(RUBY, '-e', 'exit STDIN.gets.chomp == "t"') {|i,o,e,t|
29 assert_equal(false, t.value.success?)
34 Open3.popen3(RUBY, '-e', 'STDOUT.print "foo"') {|i,o,e,t|
35 assert_equal("foo", o.read)
40 Open3.popen3(RUBY, '-e', 'STDERR.print "bar"') {|i,o,e,t|
41 assert_equal("bar", e.read)
46 r = Open3.popen3(RUBY, '-e', 'STDOUT.print STDIN.read') {|i,o,e,t|
49 assert_equal("baz", o.read)
52 assert_equal("qux", r)
56 i,o,e,t = Open3.popen3(RUBY, '-e', 'STDOUT.print STDIN.read')
59 assert_equal("baz", o.read)
68 commandline = "echo quux\n"
69 Open3.popen3(commandline) {|i,o,e,t|
70 assert_equal("quux\n", o.read)
75 Open3.popen3(RUBY, '-e', 'print $$') {|i,o,e,t|
77 assert_equal(pid, t[:pid])
78 assert_equal(pid, t.pid)
83 Open3.popen3({'A' => 'B', 'C' => 'D'}, RUBY, '-e' 'p ENV["A"]') do |i, out, err, thr|
85 assert_equal("\"B\"\n", output)
89 def test_numeric_file_descriptor2
91 Open3.popen2(RUBY, '-e', 'STDERR.puts "foo"', 2 => w) {|i,o,t|
92 assert_equal("foo\n", r.gets)
97 def test_numeric_file_descriptor3
98 omit "passing FDs bigger than 2 is not supported on Windows" if /mswin|mingw/ =~ RbConfig::CONFIG['host_os']
100 Open3.popen3(RUBY, '-e', 'IO.open(3).puts "foo"', 3 => w) {|i,o,e,t|
101 assert_equal("foo\n", r.gets, "[GH-808] [ruby-core:67347] [Bug #10699]")
114 def with_reopen(io, arg)
125 with_reopen(STDERR, w) {|old|
127 Open3.popen2(RUBY, '-e', 's=STDIN.read; STDOUT.print s+"o"; STDERR.print s+"e"') {|i,o,t|
128 assert_kind_of(Thread, t)
132 assert_equal("zo", o.read)
133 if defined?(JITSupport)
134 assert_equal("ze", JITSupport.remove_mjit_logs(r.read))
136 assert_equal("ze", r.read)
145 with_reopen(STDERR, w) {|old|
147 Open3.popen2e(RUBY, '-e', 's=STDIN.read; STDOUT.print s+"o"; STDOUT.flush; STDERR.print s+"e"') {|i,o,t|
148 assert_kind_of(Thread, t)
152 assert_equal("yoye", o.read)
153 assert_equal("", r.read)
159 def test_popen2e_noblock
160 i, o, t = Open3.popen2e(RUBY, '-e', 'STDOUT.print STDIN.read')
163 assert_equal("baz", o.read)
171 o, e, s = Open3.capture3(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>"i")
172 assert_equal("io", o)
173 assert_equal("ie", e)
177 def test_capture3_stdin_data_io
181 o, e, s = Open3.capture3(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>r)
182 assert_equal("io", o)
183 assert_equal("ie", e)
188 def test_capture3_flip
189 o, e, s = Open3.capture3(RUBY, '-e', 'STDOUT.sync=true; 1000.times { print "o"*1000; STDERR.print "e"*1000 }')
190 assert_equal("o"*1000000, o)
191 assert_equal("e"*1000000, e)
196 o, s = Open3.capture2(RUBY, '-e', 'i=STDIN.read; print i+"o"', :stdin_data=>"i")
197 assert_equal("io", o)
201 def test_capture2_stdin_data_io
205 o, s = Open3.capture2(RUBY, '-e', 'i=STDIN.read; print i+"o"', :stdin_data=>r)
206 assert_equal("io", o)
212 oe, s = Open3.capture2e(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>"i")
213 assert_equal("ioie", oe)
217 def test_capture2e_stdin_data_io
221 oe, s = Open3.capture2e(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>r)
222 assert_equal("ioie", oe)
227 def test_capture3_stdin_data
228 o, e, s = Open3.capture3(RUBY, '-e', '', :stdin_data=>"z"*(1024*1024))
234 def test_capture2_stdin_data
235 o, s = Open3.capture2(RUBY, '-e', '', :stdin_data=>"z"*(1024*1024))
240 def test_capture2e_stdin_data
241 oe, s = Open3.capture2e(RUBY, '-e', '', :stdin_data=>"z"*(1024*1024))
247 Open3.pipeline_rw([RUBY, '-e', 'print STDIN.read + "1"'],
248 [RUBY, '-e', 'print STDIN.read + "2"']) {|i,o,ts|
249 assert_kind_of(IO, i)
250 assert_kind_of(IO, o)
251 assert_kind_of(Array, ts)
252 assert_equal(2, ts.length)
253 ts.each {|t| assert_kind_of(Thread, t) }
256 assert_equal("012", o.read)
258 assert(t.value.success?)
264 Open3.pipeline_r([RUBY, '-e', 'print "1"'],
265 [RUBY, '-e', 'print STDIN.read + "2"']) {|o,ts|
266 assert_kind_of(IO, o)
267 assert_kind_of(Array, ts)
268 assert_equal(2, ts.length)
269 ts.each {|t| assert_kind_of(Thread, t) }
270 assert_equal("12", o.read)
272 assert(t.value.success?)
278 command = [RUBY, '-e', 's=STDIN.read; print s[1..-1]; exit s[0] == ?t']
280 Open3.pipeline_w(*[command]*str.length) {|i,ts|
281 assert_kind_of(IO, i)
282 assert_kind_of(Array, ts)
283 assert_equal(str.length, ts.length)
284 ts.each {|t| assert_kind_of(Thread, t) }
287 ts.each_with_index {|t, ii|
288 assert_equal(str[ii] == ?t, t.value.success?)
293 def test_pipeline_start
294 command = [RUBY, '-e', 's=STDIN.read; print s[1..-1]; exit s[0] == ?t']
296 Open3.pipeline_start([RUBY, '-e', 'print ARGV[0]', str],
297 *([command]*str.length)) {|ts|
298 assert_kind_of(Array, ts)
299 assert_equal(str.length+1, ts.length)
300 ts.each {|t| assert_kind_of(Thread, t) }
301 ts.each_with_index {|t, i|
303 assert(t.value.success?)
305 assert_equal(str[i-1] == ?t, t.value.success?)
311 def test_pipeline_start_noblock
312 ts = Open3.pipeline_start([RUBY, '-e', ''])
313 assert_kind_of(Array, ts)
314 assert_equal(1, ts.length)
315 ts.each {|t| assert_kind_of(Thread, t) }
317 assert(t.value.success?)
321 command = [RUBY, '-e', 's=STDIN.read; print s[1..-1]; exit s[0] == ?t']
323 ss = Open3.pipeline([RUBY, '-e', 'print ARGV[0]', str],
324 *([command]*str.length))
325 assert_kind_of(Array, ss)
326 assert_equal(str.length+1, ss.length)
327 ss.each {|s| assert_kind_of(Process::Status, s) }
328 ss.each_with_index {|s, i|
332 assert_equal(str[i-1] == ?t, s.success?)
337 def test_integer_and_symbol_key
338 command = [RUBY, '-e', 'puts "test_integer_and_symbol_key"']
339 out, status = Open3.capture2(*command, :chdir => '.', 2 => IO::NULL)
340 assert_equal("test_integer_and_symbol_key\n", out)
341 assert_predicate(status, :success?)