2006-06-14 23:39:20 -04:00
|
|
|
require 'test/unit'
|
2008-04-24 10:46:39 -04:00
|
|
|
require 'tmpdir'
|
2008-07-21 22:17:15 -04:00
|
|
|
require 'pathname'
|
2011-05-28 09:52:03 -04:00
|
|
|
require 'timeout'
|
2008-04-24 10:46:39 -04:00
|
|
|
require_relative 'envutil'
|
2009-01-08 20:40:16 -05:00
|
|
|
require 'rbconfig'
|
2006-06-14 23:39:20 -04:00
|
|
|
|
|
|
|
class TestProcess < Test::Unit::TestCase
|
2008-04-26 11:39:09 -04:00
|
|
|
RUBY = EnvUtil.rubybin
|
|
|
|
|
2008-06-04 12:30:03 -04:00
|
|
|
def setup
|
|
|
|
Process.waitall
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
Process.waitall
|
|
|
|
end
|
|
|
|
|
2011-08-01 07:23:44 -04:00
|
|
|
def windows?
|
|
|
|
return /mswin|mingw|bccwin/ =~ RUBY_PLATFORM
|
|
|
|
end
|
|
|
|
|
2008-04-30 02:17:28 -04:00
|
|
|
def write_file(filename, content)
|
|
|
|
File.open(filename, "w") {|f|
|
|
|
|
f << content
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_tmpchdir
|
|
|
|
Dir.mktmpdir {|d|
|
2008-07-21 22:17:15 -04:00
|
|
|
d = Pathname.new(d).realpath.to_s
|
2008-04-30 02:17:28 -04:00
|
|
|
Dir.chdir(d) {
|
|
|
|
yield d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-30 08:38:47 -04:00
|
|
|
def run_in_child(str) # should be called in a temporary directory
|
|
|
|
write_file("test-script", str)
|
|
|
|
Process.wait spawn(RUBY, "test-script")
|
|
|
|
$?
|
|
|
|
end
|
|
|
|
|
2006-06-15 04:29:15 -04:00
|
|
|
def test_rlimit_availability
|
2006-06-14 23:39:20 -04:00
|
|
|
begin
|
2006-06-15 03:07:09 -04:00
|
|
|
Process.getrlimit(nil)
|
2006-06-14 23:39:20 -04:00
|
|
|
rescue NotImplementedError
|
|
|
|
assert_raise(NotImplementedError) { Process.setrlimit }
|
2006-06-15 03:07:09 -04:00
|
|
|
rescue TypeError
|
2006-06-14 23:39:20 -04:00
|
|
|
assert_raise(ArgumentError) { Process.setrlimit }
|
|
|
|
end
|
|
|
|
end
|
2006-06-15 04:29:15 -04:00
|
|
|
|
|
|
|
def rlimit_exist?
|
|
|
|
Process.getrlimit(nil)
|
|
|
|
rescue NotImplementedError
|
|
|
|
return false
|
|
|
|
rescue TypeError
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rlimit_nofile
|
2011-07-29 09:14:59 -04:00
|
|
|
return unless rlimit_exist?
|
2008-04-30 02:17:28 -04:00
|
|
|
with_tmpchdir {
|
|
|
|
write_file 's', <<-"End"
|
2011-07-29 09:14:59 -04:00
|
|
|
# if limit=0, this test freeze pn OpenBSD
|
2011-07-29 04:01:45 -04:00
|
|
|
limit = /openbsd/ =~ RUBY_PLATFORM ? 1 : 0
|
2008-04-30 02:17:28 -04:00
|
|
|
result = 1
|
|
|
|
begin
|
2011-07-29 03:17:35 -04:00
|
|
|
Process.setrlimit(Process::RLIMIT_NOFILE, limit)
|
2008-04-30 02:17:28 -04:00
|
|
|
rescue Errno::EINVAL
|
|
|
|
result = 0
|
|
|
|
end
|
|
|
|
if result == 1
|
|
|
|
begin
|
|
|
|
IO.pipe
|
|
|
|
rescue Errno::EMFILE
|
|
|
|
result = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
exit result
|
|
|
|
End
|
|
|
|
pid = spawn RUBY, "s"
|
|
|
|
Process.wait pid
|
|
|
|
assert_equal(0, $?.to_i, "#{$?}")
|
2006-06-21 03:08:34 -04:00
|
|
|
}
|
2006-06-15 04:29:15 -04:00
|
|
|
end
|
2008-02-22 11:12:32 -05:00
|
|
|
|
|
|
|
def test_rlimit_name
|
|
|
|
return unless rlimit_exist?
|
|
|
|
[
|
|
|
|
:AS, "AS",
|
|
|
|
:CORE, "CORE",
|
|
|
|
:CPU, "CPU",
|
|
|
|
:DATA, "DATA",
|
|
|
|
:FSIZE, "FSIZE",
|
|
|
|
:MEMLOCK, "MEMLOCK",
|
2010-09-11 19:38:44 -04:00
|
|
|
:MSGQUEUE, "MSGQUEUE",
|
|
|
|
:NICE, "NICE",
|
2008-02-22 11:12:32 -05:00
|
|
|
:NOFILE, "NOFILE",
|
|
|
|
:NPROC, "NPROC",
|
|
|
|
:RSS, "RSS",
|
2010-09-11 19:38:44 -04:00
|
|
|
:RTPRIO, "RTPRIO",
|
|
|
|
:RTTIME, "RTTIME",
|
2008-02-22 11:12:32 -05:00
|
|
|
:SBSIZE, "SBSIZE",
|
2010-09-11 19:38:44 -04:00
|
|
|
:SIGPENDING, "SIGPENDING",
|
|
|
|
:STACK, "STACK",
|
2008-02-22 11:12:32 -05:00
|
|
|
].each {|name|
|
|
|
|
if Process.const_defined? "RLIMIT_#{name}"
|
|
|
|
assert_nothing_raised { Process.getrlimit(name) }
|
|
|
|
else
|
|
|
|
assert_raise(ArgumentError) { Process.getrlimit(name) }
|
|
|
|
end
|
|
|
|
}
|
|
|
|
assert_raise(ArgumentError) { Process.getrlimit(:FOO) }
|
|
|
|
assert_raise(ArgumentError) { Process.getrlimit("FOO") }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rlimit_value
|
|
|
|
return unless rlimit_exist?
|
|
|
|
assert_raise(ArgumentError) { Process.setrlimit(:CORE, :FOO) }
|
2009-02-07 05:22:20 -05:00
|
|
|
with_tmpchdir do
|
|
|
|
s = run_in_child(<<-'End')
|
|
|
|
cur, max = Process.getrlimit(:NOFILE)
|
2009-09-12 22:43:52 -04:00
|
|
|
Process.setrlimit(:NOFILE, [max-10, cur].min)
|
2009-02-07 19:10:13 -05:00
|
|
|
begin
|
|
|
|
Process.setrlimit(:NOFILE, :INFINITY)
|
|
|
|
rescue Errno::EPERM
|
2009-09-12 22:43:52 -04:00
|
|
|
exit false
|
2009-02-07 19:10:13 -05:00
|
|
|
end
|
2009-02-07 05:22:20 -05:00
|
|
|
End
|
|
|
|
assert_not_equal(0, s.exitstatus)
|
|
|
|
s = run_in_child(<<-'End')
|
|
|
|
cur, max = Process.getrlimit(:NOFILE)
|
2009-09-12 22:43:52 -04:00
|
|
|
Process.setrlimit(:NOFILE, [max-10, cur].min)
|
2009-02-07 19:10:13 -05:00
|
|
|
begin
|
|
|
|
Process.setrlimit(:NOFILE, "INFINITY")
|
|
|
|
rescue Errno::EPERM
|
2009-09-12 22:43:52 -04:00
|
|
|
exit false
|
2009-02-07 19:10:13 -05:00
|
|
|
end
|
2009-02-07 05:22:20 -05:00
|
|
|
End
|
|
|
|
assert_not_equal(0, s.exitstatus)
|
|
|
|
end
|
2008-02-22 11:12:32 -05:00
|
|
|
end
|
2008-04-24 10:46:39 -04:00
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
TRUECOMMAND = [RUBY, '-e', '']
|
|
|
|
|
2008-04-24 10:46:39 -04:00
|
|
|
def test_execopts_opts
|
|
|
|
assert_nothing_raised {
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*TRUECOMMAND, {})
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
assert_raise(ArgumentError) {
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*TRUECOMMAND, :foo => 100)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
assert_raise(ArgumentError) {
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*TRUECOMMAND, Process => 100)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_execopts_pgroup
|
2011-08-01 07:23:44 -04:00
|
|
|
skip "system(:pgroup) is not supported" if windows?
|
2008-04-26 11:39:09 -04:00
|
|
|
assert_nothing_raised { system(*TRUECOMMAND, :pgroup=>false) }
|
2008-04-24 10:46:39 -04:00
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
io = IO.popen([RUBY, "-e", "print Process.getpgrp"])
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal(Process.getpgrp.to_s, io.read)
|
|
|
|
io.close
|
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
io = IO.popen([RUBY, "-e", "print Process.getpgrp", :pgroup=>true])
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal(io.pid.to_s, io.read)
|
|
|
|
io.close
|
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
assert_raise(ArgumentError) { system(*TRUECOMMAND, :pgroup=>-1) }
|
2008-04-30 02:17:28 -04:00
|
|
|
assert_raise(Errno::EPERM) { Process.wait spawn(*TRUECOMMAND, :pgroup=>2) }
|
2008-04-24 10:46:39 -04:00
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
io1 = IO.popen([RUBY, "-e", "print Process.getpgrp", :pgroup=>true])
|
|
|
|
io2 = IO.popen([RUBY, "-e", "print Process.getpgrp", :pgroup=>io1.pid])
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal(io1.pid.to_s, io1.read)
|
|
|
|
assert_equal(io1.pid.to_s, io2.read)
|
|
|
|
Process.wait io1.pid
|
|
|
|
Process.wait io2.pid
|
|
|
|
io1.close
|
|
|
|
io2.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_execopts_rlimit
|
|
|
|
return unless rlimit_exist?
|
2008-04-26 11:39:09 -04:00
|
|
|
assert_raise(ArgumentError) { system(*TRUECOMMAND, :rlimit_foo=>0) }
|
|
|
|
assert_raise(ArgumentError) { system(*TRUECOMMAND, :rlimit_NOFILE=>0) }
|
|
|
|
assert_raise(ArgumentError) { system(*TRUECOMMAND, :rlimit_nofile=>[]) }
|
|
|
|
assert_raise(ArgumentError) { system(*TRUECOMMAND, :rlimit_nofile=>[1,2,3]) }
|
2008-04-24 10:46:39 -04:00
|
|
|
|
|
|
|
max = Process.getrlimit(:CORE).last
|
|
|
|
|
|
|
|
n = max
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen([RUBY, "-e",
|
2008-04-24 10:46:39 -04:00
|
|
|
"p Process.getrlimit(:CORE)", :rlimit_core=>n]) {|io|
|
|
|
|
assert_equal("[#{n}, #{n}]\n", io.read)
|
|
|
|
}
|
|
|
|
|
|
|
|
n = 0
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen([RUBY, "-e",
|
2008-04-24 10:46:39 -04:00
|
|
|
"p Process.getrlimit(:CORE)", :rlimit_core=>n]) {|io|
|
|
|
|
assert_equal("[#{n}, #{n}]\n", io.read)
|
|
|
|
}
|
|
|
|
|
|
|
|
n = max
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen([RUBY, "-e",
|
2008-04-24 10:46:39 -04:00
|
|
|
"p Process.getrlimit(:CORE)", :rlimit_core=>[n]]) {|io|
|
|
|
|
assert_equal("[#{n}, #{n}]", io.read.chomp)
|
|
|
|
}
|
|
|
|
|
|
|
|
m, n = 0, max
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen([RUBY, "-e",
|
2008-04-24 10:46:39 -04:00
|
|
|
"p Process.getrlimit(:CORE)", :rlimit_core=>[m,n]]) {|io|
|
|
|
|
assert_equal("[#{m}, #{n}]", io.read.chomp)
|
|
|
|
}
|
|
|
|
|
|
|
|
m, n = 0, 0
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen([RUBY, "-e",
|
2008-04-24 10:46:39 -04:00
|
|
|
"p Process.getrlimit(:CORE)", :rlimit_core=>[m,n]]) {|io|
|
|
|
|
assert_equal("[#{m}, #{n}]", io.read.chomp)
|
|
|
|
}
|
|
|
|
|
|
|
|
n = max
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen([RUBY, "-e",
|
2008-04-24 10:46:39 -04:00
|
|
|
"p Process.getrlimit(:CORE), Process.getrlimit(:CPU)",
|
|
|
|
:rlimit_core=>n, :rlimit_cpu=>3600]) {|io|
|
|
|
|
assert_equal("[#{n}, #{n}]\n[3600, 3600]", io.read.chomp)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-01-22 00:52:14 -05:00
|
|
|
MANDATORY_ENVS = %w[RUBYLIB]
|
2009-03-20 19:28:20 -04:00
|
|
|
case RbConfig::CONFIG['target_os']
|
|
|
|
when /linux/
|
2009-01-08 20:40:16 -05:00
|
|
|
MANDATORY_ENVS << 'LD_PRELOAD'
|
2009-03-20 19:28:20 -04:00
|
|
|
when /mswin|mingw/
|
|
|
|
MANDATORY_ENVS.concat(%w[HOME USER TMPDIR])
|
2009-01-08 20:40:16 -05:00
|
|
|
end
|
|
|
|
if e = RbConfig::CONFIG['LIBPATHENV']
|
|
|
|
MANDATORY_ENVS << e
|
|
|
|
end
|
|
|
|
PREENVARG = ['-e', "%w[#{MANDATORY_ENVS.join(' ')}].each{|e|ENV.delete(e)}"]
|
|
|
|
ENVARG = ['-e', 'ENV.each {|k,v| puts "#{k}=#{v}" }']
|
|
|
|
ENVCOMMAND = [RUBY].concat(PREENVARG).concat(ENVARG)
|
2008-04-26 11:39:09 -04:00
|
|
|
|
2008-04-24 10:46:39 -04:00
|
|
|
def test_execopts_env
|
|
|
|
assert_raise(ArgumentError) {
|
2008-04-26 11:39:09 -04:00
|
|
|
system({"F=O"=>"BAR"}, *TRUECOMMAND)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
|
2010-05-23 19:16:09 -04:00
|
|
|
with_tmpchdir {|d|
|
|
|
|
prog = "#{d}/notexist"
|
|
|
|
e = assert_raise(Errno::ENOENT) {
|
|
|
|
Process.wait Process.spawn({"FOO"=>"BAR"}, prog)
|
|
|
|
}
|
|
|
|
assert_equal(prog, e.message.sub(/.* - /, ''))
|
|
|
|
e = assert_raise(Errno::ENOENT) {
|
|
|
|
Process.wait Process.spawn({"FOO"=>"BAR"}, [prog, "blar"])
|
|
|
|
}
|
|
|
|
assert_equal(prog, e.message.sub(/.* - /, ''))
|
|
|
|
}
|
2008-04-24 10:46:39 -04:00
|
|
|
h = {}
|
2009-01-08 20:40:16 -05:00
|
|
|
cmd = [h, RUBY]
|
2010-01-12 02:12:12 -05:00
|
|
|
(ENV.keys + MANDATORY_ENVS).each do |k|
|
2009-01-08 20:40:16 -05:00
|
|
|
case k
|
|
|
|
when /\APATH\z/i
|
|
|
|
when *MANDATORY_ENVS
|
|
|
|
cmd << '-e' << "ENV.delete('#{k}')"
|
|
|
|
else
|
|
|
|
h[k] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
cmd << '-e' << 'puts ENV.keys.map{|e|e.upcase}'
|
|
|
|
IO.popen(cmd) {|io|
|
2008-04-26 11:39:09 -04:00
|
|
|
assert_equal("PATH\n", io.read)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen([{"FOO"=>"BAR"}, *ENVCOMMAND]) {|io|
|
|
|
|
assert_match(/^FOO=BAR$/, io.read)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
with_tmpchdir {|d|
|
2008-04-26 11:39:09 -04:00
|
|
|
system({"fofo"=>"haha"}, *ENVCOMMAND, STDOUT=>"out")
|
|
|
|
assert_match(/^fofo=haha$/, File.read("out").chomp)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
2010-01-13 12:00:48 -05:00
|
|
|
|
|
|
|
old = ENV["hmm"]
|
|
|
|
begin
|
|
|
|
ENV["hmm"] = "fufu"
|
|
|
|
IO.popen(ENVCOMMAND) {|io| assert_match(/^hmm=fufu$/, io.read) }
|
|
|
|
IO.popen([{"hmm"=>""}, *ENVCOMMAND]) {|io| assert_match(/^hmm=$/, io.read) }
|
|
|
|
IO.popen([{"hmm"=>nil}, *ENVCOMMAND]) {|io| assert_not_match(/^hmm=/, io.read) }
|
|
|
|
ENV["hmm"] = ""
|
|
|
|
IO.popen(ENVCOMMAND) {|io| assert_match(/^hmm=$/, io.read) }
|
|
|
|
IO.popen([{"hmm"=>""}, *ENVCOMMAND]) {|io| assert_match(/^hmm=$/, io.read) }
|
|
|
|
IO.popen([{"hmm"=>nil}, *ENVCOMMAND]) {|io| assert_not_match(/^hmm=/, io.read) }
|
|
|
|
ENV["hmm"] = nil
|
|
|
|
IO.popen(ENVCOMMAND) {|io| assert_not_match(/^hmm=/, io.read) }
|
|
|
|
IO.popen([{"hmm"=>""}, *ENVCOMMAND]) {|io| assert_match(/^hmm=$/, io.read) }
|
|
|
|
IO.popen([{"hmm"=>nil}, *ENVCOMMAND]) {|io| assert_not_match(/^hmm=/, io.read) }
|
|
|
|
ensure
|
|
|
|
ENV["hmm"] = old
|
|
|
|
end
|
2008-04-24 10:46:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_execopts_unsetenv_others
|
2009-01-08 20:40:16 -05:00
|
|
|
h = {}
|
|
|
|
MANDATORY_ENVS.each {|k| e = ENV[k] and h[k] = e}
|
|
|
|
IO.popen([h, *ENVCOMMAND, :unsetenv_others=>true]) {|io|
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("", io.read)
|
|
|
|
}
|
2009-01-08 20:40:16 -05:00
|
|
|
IO.popen([h.merge("A"=>"B"), *ENVCOMMAND, :unsetenv_others=>true]) {|io|
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("A=B\n", io.read)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
PWD = [RUBY, '-e', 'puts Dir.pwd']
|
|
|
|
|
2008-04-24 10:46:39 -04:00
|
|
|
def test_execopts_chdir
|
|
|
|
with_tmpchdir {|d|
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen([*PWD, :chdir => d]) {|io|
|
|
|
|
assert_equal(d, io.read.chomp)
|
|
|
|
}
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_raise(Errno::ENOENT) {
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*PWD, :chdir => "d/notexist")
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
UMASK = [RUBY, '-e', 'printf "%04o\n", File.umask']
|
|
|
|
|
2008-04-24 10:46:39 -04:00
|
|
|
def test_execopts_umask
|
2011-08-01 07:23:44 -04:00
|
|
|
skip "umask is not supported" if windows?
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen([*UMASK, :umask => 0]) {|io|
|
|
|
|
assert_equal("0000", io.read.chomp)
|
|
|
|
}
|
|
|
|
IO.popen([*UMASK, :umask => 0777]) {|io|
|
|
|
|
assert_equal("0777", io.read.chomp)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_pipe
|
|
|
|
begin
|
|
|
|
r, w = IO.pipe
|
|
|
|
yield r, w
|
|
|
|
ensure
|
|
|
|
r.close unless r.closed?
|
|
|
|
w.close unless w.closed?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_pipes(n)
|
|
|
|
ary = []
|
|
|
|
begin
|
|
|
|
n.times {
|
|
|
|
ary << IO.pipe
|
|
|
|
}
|
|
|
|
yield ary
|
|
|
|
ensure
|
|
|
|
ary.each {|r, w|
|
|
|
|
r.close unless r.closed?
|
|
|
|
w.close unless w.closed?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
ECHO = lambda {|arg| [RUBY, '-e', "puts #{arg.dump}; STDOUT.flush"] }
|
|
|
|
SORT = [RUBY, '-e', "puts ARGF.readlines.sort"]
|
|
|
|
CAT = [RUBY, '-e', "IO.copy_stream STDIN, STDOUT"]
|
|
|
|
|
2008-04-24 10:46:39 -04:00
|
|
|
def test_execopts_redirect
|
|
|
|
with_tmpchdir {|d|
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*ECHO["a"], STDOUT=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644])
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("a", File.read("out").chomp)
|
2011-08-01 07:23:44 -04:00
|
|
|
if windows?
|
2010-05-13 02:32:21 -04:00
|
|
|
# currently telling to child the file modes is not supported.
|
|
|
|
open("out", "a") {|f| f.write "0\n"}
|
|
|
|
else
|
|
|
|
Process.wait Process.spawn(*ECHO["0"], STDOUT=>["out", File::WRONLY|File::CREAT|File::APPEND, 0644])
|
|
|
|
assert_equal("a\n0\n", File.read("out"))
|
|
|
|
end
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*SORT, STDIN=>["out", File::RDONLY, 0644],
|
2008-04-24 10:46:39 -04:00
|
|
|
STDOUT=>["out2", File::WRONLY|File::CREAT|File::TRUNC, 0644])
|
|
|
|
assert_equal("0\na\n", File.read("out2"))
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*ECHO["b"], [STDOUT, STDERR]=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644])
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("b", File.read("out").chomp)
|
2008-04-26 11:39:09 -04:00
|
|
|
# problem occur with valgrind
|
|
|
|
#Process.wait Process.spawn(*ECHO["a"], STDOUT=>:close, STDERR=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644])
|
2008-04-24 10:46:39 -04:00
|
|
|
#p File.read("out")
|
2008-04-26 11:39:09 -04:00
|
|
|
#assert(!File.read("out").empty?) # error message such as "-e:1:in `flush': Bad file descriptor (Errno::EBADF)"
|
|
|
|
Process.wait Process.spawn(*ECHO["c"], STDERR=>STDOUT, STDOUT=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644])
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("c", File.read("out").chomp)
|
|
|
|
File.open("out", "w") {|f|
|
2011-03-07 03:44:45 -05:00
|
|
|
Process.wait Process.spawn(*ECHO["d"], STDOUT=>f)
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("d", File.read("out").chomp)
|
|
|
|
}
|
2011-03-07 03:44:45 -05:00
|
|
|
opts = {STDOUT=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644]}
|
2011-08-01 07:23:44 -04:00
|
|
|
opts.merge(3=>STDOUT, 4=>STDOUT, 5=>STDOUT, 6=>STDOUT, 7=>STDOUT) unless windows?
|
2011-03-07 03:44:45 -05:00
|
|
|
Process.wait Process.spawn(*ECHO["e"], opts)
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("e", File.read("out").chomp)
|
2011-03-07 03:44:45 -05:00
|
|
|
opts = {STDOUT=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644]}
|
2011-08-01 07:23:44 -04:00
|
|
|
opts.merge(3=>0, 4=>:in, 5=>STDIN, 6=>1, 7=>:out, 8=>STDOUT, 9=>2, 10=>:err, 11=>STDERR) unless windows?
|
2011-03-07 03:44:45 -05:00
|
|
|
Process.wait Process.spawn(*ECHO["ee"], opts)
|
2008-12-03 07:33:37 -05:00
|
|
|
assert_equal("ee", File.read("out").chomp)
|
2011-08-01 07:23:44 -04:00
|
|
|
unless windows?
|
2010-05-13 02:32:21 -04:00
|
|
|
# passing non-stdio fds is not supported on Windows
|
|
|
|
File.open("out", "w") {|f|
|
|
|
|
h = {STDOUT=>f, f=>STDOUT}
|
|
|
|
3.upto(30) {|i| h[i] = STDOUT if f.fileno != i }
|
|
|
|
Process.wait Process.spawn(*ECHO["f"], h)
|
|
|
|
assert_equal("f", File.read("out").chomp)
|
|
|
|
}
|
|
|
|
end
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_raise(ArgumentError) {
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*ECHO["f"], 1=>Process)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
assert_raise(ArgumentError) {
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*ECHO["f"], [Process]=>1)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
assert_raise(ArgumentError) {
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*ECHO["f"], [1, STDOUT]=>2)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
assert_raise(ArgumentError) {
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*ECHO["f"], -1=>2)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*ECHO["hhh\nggg\n"], STDOUT=>"out")
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("hhh\nggg\n", File.read("out"))
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait Process.spawn(*SORT, STDIN=>"out", STDOUT=>"out2")
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("ggg\nhhh\n", File.read("out2"))
|
|
|
|
|
2011-08-01 07:23:44 -04:00
|
|
|
unless windows?
|
2010-05-13 02:32:21 -04:00
|
|
|
# passing non-stdio fds is not supported on Windows
|
|
|
|
assert_raise(Errno::ENOENT) {
|
|
|
|
Process.wait Process.spawn("non-existing-command", (3..60).to_a=>["err", File::WRONLY|File::CREAT])
|
|
|
|
}
|
|
|
|
assert_equal("", File.read("err"))
|
|
|
|
end
|
2008-04-24 10:46:39 -04:00
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
system(*ECHO["bb\naa\n"], STDOUT=>["out", "w"])
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("bb\naa\n", File.read("out"))
|
2008-04-26 11:39:09 -04:00
|
|
|
system(*SORT, STDIN=>["out"], STDOUT=>"out2")
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("aa\nbb\n", File.read("out2"))
|
|
|
|
|
|
|
|
with_pipe {|r1, w1|
|
|
|
|
with_pipe {|r2, w2|
|
2011-08-01 07:23:44 -04:00
|
|
|
opts = {STDIN=>r1, STDOUT=>w2}
|
|
|
|
opts.merge(w1=>:close, r2=>:close) unless windows?
|
|
|
|
pid = spawn(*SORT, opts)
|
2008-04-24 10:46:39 -04:00
|
|
|
r1.close
|
|
|
|
w2.close
|
|
|
|
w1.puts "c"
|
|
|
|
w1.puts "a"
|
|
|
|
w1.puts "b"
|
|
|
|
w1.close
|
|
|
|
assert_equal("a\nb\nc\n", r2.read)
|
2010-05-13 02:32:21 -04:00
|
|
|
r2.close
|
2008-06-04 12:30:03 -04:00
|
|
|
Process.wait(pid)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-01 07:23:44 -04:00
|
|
|
unless windows?
|
2010-05-13 02:32:21 -04:00
|
|
|
# passing non-stdio fds is not supported on Windows
|
|
|
|
with_pipes(5) {|pipes|
|
|
|
|
ios = pipes.flatten
|
|
|
|
h = {}
|
|
|
|
ios.length.times {|i| h[ios[i]] = ios[(i-1)%ios.length] }
|
|
|
|
h2 = h.invert
|
|
|
|
rios = pipes.map {|r, w| r }
|
|
|
|
wios = pipes.map {|r, w| w }
|
|
|
|
child_wfds = wios.map {|w| h2[w].fileno }
|
|
|
|
pid = spawn(RUBY, "-e",
|
|
|
|
"[#{child_wfds.join(',')}].each {|fd| IO.new(fd, 'w').puts fd }", h)
|
|
|
|
pipes.each {|r, w|
|
|
|
|
assert_equal("#{h2[w].fileno}\n", r.gets)
|
|
|
|
}
|
|
|
|
Process.wait pid;
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
|
2010-05-13 02:32:21 -04:00
|
|
|
with_pipes(5) {|pipes|
|
|
|
|
ios = pipes.flatten
|
|
|
|
h = {}
|
|
|
|
ios.length.times {|i| h[ios[i]] = ios[(i+1)%ios.length] }
|
|
|
|
h2 = h.invert
|
|
|
|
rios = pipes.map {|r, w| r }
|
|
|
|
wios = pipes.map {|r, w| w }
|
|
|
|
child_wfds = wios.map {|w| h2[w].fileno }
|
|
|
|
pid = spawn(RUBY, "-e",
|
|
|
|
"[#{child_wfds.join(',')}].each {|fd| IO.new(fd, 'w').puts fd }", h)
|
|
|
|
pipes.each {|r, w|
|
|
|
|
assert_equal("#{h2[w].fileno}\n", r.gets)
|
|
|
|
}
|
|
|
|
Process.wait pid
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
|
2010-05-14 00:34:37 -04:00
|
|
|
closed_fd = nil
|
|
|
|
with_pipes(5) {|pipes|
|
|
|
|
io = pipes.last.last
|
|
|
|
closed_fd = io.fileno
|
|
|
|
}
|
|
|
|
assert_raise(Errno::EBADF) { Process.wait spawn(*TRUECOMMAND, closed_fd=>closed_fd) }
|
|
|
|
|
|
|
|
with_pipe {|r, w|
|
|
|
|
if w.respond_to?(:"close_on_exec=")
|
|
|
|
w.close_on_exec = true
|
|
|
|
pid = spawn(RUBY, "-e", "IO.new(#{w.fileno}, 'w').print 'a'", w=>w)
|
|
|
|
w.close
|
|
|
|
assert_equal("a", r.read)
|
|
|
|
Process.wait pid
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2008-04-24 10:46:39 -04:00
|
|
|
|
2008-04-26 11:39:09 -04:00
|
|
|
system(*ECHO["funya"], :out=>"out")
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("funya\n", File.read("out"))
|
2008-04-26 11:39:09 -04:00
|
|
|
system(RUBY, '-e', 'STDOUT.reopen(STDERR); puts "henya"', :err=>"out")
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("henya\n", File.read("out"))
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen([*CAT, :in=>"out"]) {|io|
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("henya\n", io.read)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-12-03 10:02:10 -05:00
|
|
|
def test_execopts_redirect_dup2_child
|
|
|
|
with_tmpchdir {|d|
|
2008-12-04 05:17:34 -05:00
|
|
|
Process.wait spawn(RUBY, "-e", "STDERR.print 'err'; STDOUT.print 'out'",
|
|
|
|
STDOUT=>"out", STDERR=>[:child, STDOUT])
|
2008-12-03 10:02:10 -05:00
|
|
|
assert_equal("errout", File.read("out"))
|
2008-12-04 05:17:34 -05:00
|
|
|
|
|
|
|
Process.wait spawn(RUBY, "-e", "STDERR.print 'err'; STDOUT.print 'out'",
|
|
|
|
STDERR=>"out", STDOUT=>[:child, STDERR])
|
|
|
|
assert_equal("errout", File.read("out"))
|
|
|
|
|
2011-08-01 07:23:44 -04:00
|
|
|
skip "inheritance of fd other than stdin,stdout and stderr is not supported" if windows?
|
2008-12-04 05:17:34 -05:00
|
|
|
Process.wait spawn(RUBY, "-e", "STDERR.print 'err'; STDOUT.print 'out'",
|
2009-03-05 22:56:38 -05:00
|
|
|
STDOUT=>"out",
|
2008-12-04 05:17:34 -05:00
|
|
|
STDERR=>[:child, 3],
|
2009-03-05 22:56:38 -05:00
|
|
|
3=>[:child, 4],
|
2008-12-04 05:17:34 -05:00
|
|
|
4=>[:child, STDOUT]
|
|
|
|
)
|
2008-12-03 10:02:10 -05:00
|
|
|
assert_equal("errout", File.read("out"))
|
|
|
|
|
|
|
|
IO.popen([RUBY, "-e", "STDERR.print 'err'; STDOUT.print 'out'", STDERR=>[:child, STDOUT]]) {|io|
|
|
|
|
assert_equal("errout", io.read)
|
|
|
|
}
|
|
|
|
|
2008-12-04 05:17:34 -05:00
|
|
|
assert_raise(ArgumentError) { Process.wait spawn(*TRUECOMMAND, STDOUT=>[:child, STDOUT]) }
|
|
|
|
assert_raise(ArgumentError) { Process.wait spawn(*TRUECOMMAND, 3=>[:child, 4], 4=>[:child, 3]) }
|
|
|
|
assert_raise(ArgumentError) { Process.wait spawn(*TRUECOMMAND, 3=>[:child, 4], 4=>[:child, 5], 5=>[:child, 3]) }
|
|
|
|
assert_raise(ArgumentError) { Process.wait spawn(*TRUECOMMAND, STDOUT=>[:child, 3]) }
|
2008-12-03 10:02:10 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-24 10:46:39 -04:00
|
|
|
def test_execopts_exec
|
|
|
|
with_tmpchdir {|d|
|
2008-04-30 02:17:28 -04:00
|
|
|
write_file("s", 'exec "echo aaa", STDOUT=>"foo"')
|
|
|
|
pid = spawn RUBY, 's'
|
2008-04-24 10:46:39 -04:00
|
|
|
Process.wait pid
|
|
|
|
assert_equal("aaa\n", File.read("foo"))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_execopts_popen
|
|
|
|
with_tmpchdir {|d|
|
2008-04-26 11:39:09 -04:00
|
|
|
IO.popen("#{RUBY} -e 'puts :foo'") {|io| assert_equal("foo\n", io.read) }
|
|
|
|
assert_raise(Errno::ENOENT) { IO.popen(["echo bar"]) {} } # assuming "echo bar" command not exist.
|
|
|
|
IO.popen(ECHO["baz"]) {|io| assert_equal("baz\n", io.read) }
|
|
|
|
assert_raise(ArgumentError) {
|
|
|
|
IO.popen([*ECHO["qux"], STDOUT=>STDOUT]) {|io| }
|
|
|
|
}
|
|
|
|
IO.popen([*ECHO["hoge"], STDERR=>STDOUT]) {|io|
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_equal("hoge\n", io.read)
|
|
|
|
}
|
2008-04-26 11:39:09 -04:00
|
|
|
assert_raise(ArgumentError) {
|
|
|
|
IO.popen([*ECHO["fuga"], STDOUT=>"out"]) {|io| }
|
|
|
|
}
|
2011-08-01 07:23:44 -04:00
|
|
|
skip "inheritance of fd other than stdin,stdout and stderr is not supported" if windows?
|
2008-04-26 11:39:09 -04:00
|
|
|
with_pipe {|r, w|
|
2009-01-22 01:27:44 -05:00
|
|
|
IO.popen([RUBY, '-e', 'IO.new(3, "w").puts("a"); puts "b"', 3=>w]) {|io|
|
2008-04-26 11:39:09 -04:00
|
|
|
assert_equal("b\n", io.read)
|
|
|
|
}
|
|
|
|
w.close
|
|
|
|
assert_equal("a\n", r.read)
|
|
|
|
}
|
2009-01-22 01:27:44 -05:00
|
|
|
IO.popen([RUBY, '-e', "IO.new(9, 'w').puts(:b)",
|
2008-04-24 10:46:39 -04:00
|
|
|
9=>["out2", File::WRONLY|File::CREAT|File::TRUNC]]) {|io|
|
|
|
|
assert_equal("", io.read)
|
|
|
|
}
|
|
|
|
assert_equal("b\n", File.read("out2"))
|
2008-04-26 11:39:09 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_popen_fork
|
|
|
|
IO.popen("-") {|io|
|
|
|
|
if !io
|
|
|
|
puts "fooo"
|
|
|
|
else
|
|
|
|
assert_equal("fooo\n", io.read)
|
|
|
|
end
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
2008-06-23 21:26:54 -04:00
|
|
|
rescue NotImplementedError
|
2008-04-24 10:46:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fd_inheritance
|
2011-08-01 07:23:44 -04:00
|
|
|
skip "inheritance of fd other than stdin,stdout and stderr is not supported" if windows?
|
2008-04-24 10:46:39 -04:00
|
|
|
with_pipe {|r, w|
|
2009-01-22 01:27:44 -05:00
|
|
|
system(RUBY, '-e', 'IO.new(ARGV[0].to_i, "w").puts(:ba)', w.fileno.to_s)
|
2008-04-24 10:46:39 -04:00
|
|
|
w.close
|
|
|
|
assert_equal("ba\n", r.read)
|
|
|
|
}
|
|
|
|
with_pipe {|r, w|
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait spawn(RUBY, '-e',
|
2009-01-22 01:27:44 -05:00
|
|
|
'IO.new(ARGV[0].to_i, "w").puts("bi") rescue nil',
|
2008-04-26 11:39:09 -04:00
|
|
|
w.fileno.to_s)
|
2008-04-24 10:46:39 -04:00
|
|
|
w.close
|
2008-04-25 11:50:24 -04:00
|
|
|
assert_equal("", r.read)
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
with_pipe {|r, w|
|
2008-04-30 02:17:28 -04:00
|
|
|
with_tmpchdir {|d|
|
|
|
|
write_file("s", <<-"End")
|
|
|
|
exec(#{RUBY.dump}, '-e',
|
2009-01-22 01:27:44 -05:00
|
|
|
'IO.new(ARGV[0].to_i, "w").puts("bu") rescue nil',
|
2008-04-30 02:17:28 -04:00
|
|
|
#{w.fileno.to_s.dump})
|
|
|
|
End
|
|
|
|
Process.wait spawn(RUBY, "s", :close_others=>false)
|
|
|
|
w.close
|
|
|
|
assert_equal("bu\n", r.read)
|
2008-04-26 11:39:09 -04:00
|
|
|
}
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
with_pipe {|r, w|
|
2009-01-22 01:27:44 -05:00
|
|
|
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('me')"])
|
2008-04-24 10:46:39 -04:00
|
|
|
w.close
|
|
|
|
errmsg = io.read
|
|
|
|
assert_equal("", r.read)
|
|
|
|
assert_not_equal("", errmsg)
|
2008-06-04 12:30:03 -04:00
|
|
|
Process.wait
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
with_pipe {|r, w|
|
2009-01-22 01:27:44 -05:00
|
|
|
errmsg = `#{RUBY} -e "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts(123)"`
|
2008-04-24 10:46:39 -04:00
|
|
|
w.close
|
|
|
|
assert_equal("", r.read)
|
|
|
|
assert_not_equal("", errmsg)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_execopts_close_others
|
2011-08-01 07:23:44 -04:00
|
|
|
skip "inheritance of fd other than stdin,stdout and stderr is not supported" if windows?
|
2008-04-24 10:46:39 -04:00
|
|
|
with_tmpchdir {|d|
|
|
|
|
with_pipe {|r, w|
|
2009-01-22 01:27:44 -05:00
|
|
|
system(RUBY, '-e', 'STDERR.reopen("err", "w"); IO.new(ARGV[0].to_i, "w").puts("ma")', w.fileno.to_s, :close_others=>true)
|
2008-04-24 10:46:39 -04:00
|
|
|
w.close
|
|
|
|
assert_equal("", r.read)
|
2008-04-26 11:39:09 -04:00
|
|
|
assert_not_equal("", File.read("err"))
|
2008-04-24 10:46:39 -04:00
|
|
|
File.unlink("err")
|
|
|
|
}
|
|
|
|
with_pipe {|r, w|
|
2009-01-22 01:27:44 -05:00
|
|
|
Process.wait spawn(RUBY, '-e', 'STDERR.reopen("err", "w"); IO.new(ARGV[0].to_i, "w").puts("mi")', w.fileno.to_s, :close_others=>true)
|
2008-04-24 10:46:39 -04:00
|
|
|
w.close
|
|
|
|
assert_equal("", r.read)
|
2008-04-26 11:39:09 -04:00
|
|
|
assert_not_equal("", File.read("err"))
|
2008-04-24 10:46:39 -04:00
|
|
|
File.unlink("err")
|
|
|
|
}
|
2008-04-25 11:50:24 -04:00
|
|
|
with_pipe {|r, w|
|
2009-01-22 01:27:44 -05:00
|
|
|
Process.wait spawn(RUBY, '-e', 'IO.new(ARGV[0].to_i, "w").puts("bi")', w.fileno.to_s, :close_others=>false)
|
2008-04-25 11:50:24 -04:00
|
|
|
w.close
|
|
|
|
assert_equal("bi\n", r.read)
|
|
|
|
}
|
2008-04-24 10:46:39 -04:00
|
|
|
with_pipe {|r, w|
|
2008-04-30 02:17:28 -04:00
|
|
|
write_file("s", <<-"End")
|
|
|
|
exec(#{RUBY.dump}, '-e',
|
2009-01-22 01:27:44 -05:00
|
|
|
'STDERR.reopen("err", "w"); IO.new(ARGV[0].to_i, "w").puts("mu")',
|
2008-04-30 02:17:28 -04:00
|
|
|
#{w.fileno.to_s.dump},
|
|
|
|
:close_others=>true)
|
|
|
|
End
|
|
|
|
Process.wait spawn(RUBY, "s", :close_others=>false)
|
2008-04-24 10:46:39 -04:00
|
|
|
w.close
|
|
|
|
assert_equal("", r.read)
|
2008-04-26 11:39:09 -04:00
|
|
|
assert_not_equal("", File.read("err"))
|
2008-04-24 10:46:39 -04:00
|
|
|
File.unlink("err")
|
|
|
|
}
|
|
|
|
with_pipe {|r, w|
|
2009-01-22 01:27:44 -05:00
|
|
|
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('me')", :close_others=>true])
|
2008-04-24 10:46:39 -04:00
|
|
|
w.close
|
|
|
|
errmsg = io.read
|
|
|
|
assert_equal("", r.read)
|
|
|
|
assert_not_equal("", errmsg)
|
2008-06-04 12:30:03 -04:00
|
|
|
Process.wait
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
with_pipe {|r, w|
|
2009-01-22 01:27:44 -05:00
|
|
|
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('mo')", :close_others=>false])
|
2008-04-24 10:46:39 -04:00
|
|
|
w.close
|
|
|
|
errmsg = io.read
|
|
|
|
assert_equal("mo\n", r.read)
|
|
|
|
assert_equal("", errmsg)
|
2008-06-04 12:30:03 -04:00
|
|
|
Process.wait
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
with_pipe {|r, w|
|
2009-01-22 01:27:44 -05:00
|
|
|
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('mo')", :close_others=>nil])
|
2008-04-24 10:46:39 -04:00
|
|
|
w.close
|
|
|
|
errmsg = io.read
|
|
|
|
assert_equal("mo\n", r.read)
|
|
|
|
assert_equal("", errmsg)
|
2008-06-04 12:30:03 -04:00
|
|
|
Process.wait
|
2008-04-24 10:46:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-30 08:38:47 -04:00
|
|
|
def test_execopts_redirect_self
|
2008-12-04 11:39:55 -05:00
|
|
|
begin
|
|
|
|
with_pipe {|r, w|
|
|
|
|
w << "haha\n"
|
|
|
|
w.close
|
|
|
|
r.close_on_exec = true
|
2009-01-22 01:27:44 -05:00
|
|
|
IO.popen([RUBY, "-e", "print IO.new(#{r.fileno}, 'r').read", r.fileno=>r.fileno, :close_others=>false]) {|io|
|
2008-12-04 11:39:55 -05:00
|
|
|
assert_equal("haha\n", io.read)
|
|
|
|
}
|
2008-04-30 08:38:47 -04:00
|
|
|
}
|
2008-12-04 11:39:55 -05:00
|
|
|
rescue NotImplementedError
|
|
|
|
skip "IO#close_on_exec= is not supported"
|
|
|
|
end
|
2008-04-30 08:38:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_execopts_duplex_io
|
|
|
|
IO.popen("#{RUBY} -e ''", "r+") {|duplex|
|
|
|
|
assert_raise(ArgumentError) { system("#{RUBY} -e ''", duplex=>STDOUT) }
|
|
|
|
assert_raise(ArgumentError) { system("#{RUBY} -e ''", STDOUT=>duplex) }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-26 08:52:25 -04:00
|
|
|
def test_execopts_modification
|
|
|
|
h = {}
|
2008-04-26 11:39:09 -04:00
|
|
|
Process.wait spawn(*TRUECOMMAND, h)
|
2008-04-26 08:52:25 -04:00
|
|
|
assert_equal({}, h)
|
|
|
|
|
|
|
|
h = {}
|
2008-04-26 11:39:09 -04:00
|
|
|
system(*TRUECOMMAND, h)
|
2008-04-26 08:52:25 -04:00
|
|
|
assert_equal({}, h)
|
|
|
|
|
|
|
|
h = {}
|
2008-04-26 11:39:09 -04:00
|
|
|
io = IO.popen([*TRUECOMMAND, h])
|
2008-04-26 08:52:25 -04:00
|
|
|
io.close
|
|
|
|
assert_equal({}, h)
|
|
|
|
end
|
|
|
|
|
2008-04-27 02:10:35 -04:00
|
|
|
def test_system_noshell
|
|
|
|
str = "echo non existing command name which contains spaces"
|
2008-04-24 10:46:39 -04:00
|
|
|
assert_nil(system([str, str]))
|
|
|
|
end
|
|
|
|
|
2008-04-27 02:10:35 -04:00
|
|
|
def test_spawn_noshell
|
|
|
|
str = "echo non existing command name which contains spaces"
|
|
|
|
assert_raise(Errno::ENOENT) { spawn([str, str]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_popen_noshell
|
|
|
|
str = "echo non existing command name which contains spaces"
|
|
|
|
assert_raise(Errno::ENOENT) { IO.popen([str, str]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exec_noshell
|
2008-04-30 02:17:28 -04:00
|
|
|
with_tmpchdir {|d|
|
2009-03-20 19:28:20 -04:00
|
|
|
write_file("s", <<-"End")
|
2008-04-30 02:17:28 -04:00
|
|
|
str = "echo non existing command name which contains spaces"
|
2009-03-20 19:28:20 -04:00
|
|
|
STDERR.reopen(STDOUT)
|
2008-04-30 02:17:28 -04:00
|
|
|
begin
|
|
|
|
exec [str, str]
|
|
|
|
rescue Errno::ENOENT
|
2009-03-20 19:28:20 -04:00
|
|
|
print "Errno::ENOENT success"
|
2008-04-30 02:17:28 -04:00
|
|
|
end
|
|
|
|
End
|
2009-03-20 19:28:20 -04:00
|
|
|
r = IO.popen([RUBY, "s", :close_others=>false], "r") {|f| f.read}
|
|
|
|
assert_equal("Errno::ENOENT success", r)
|
2008-04-27 02:10:35 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_system_wordsplit
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
write_file("script", <<-'End')
|
|
|
|
File.open("result", "w") {|t| t << "haha pid=#{$$} ppid=#{Process.ppid}" }
|
|
|
|
exit 5
|
|
|
|
End
|
|
|
|
str = "#{RUBY} script"
|
|
|
|
ret = system(str)
|
|
|
|
status = $?
|
|
|
|
assert_equal(false, ret)
|
|
|
|
assert(status.exited?)
|
|
|
|
assert_equal(5, status.exitstatus)
|
|
|
|
assert_equal("haha pid=#{status.pid} ppid=#{$$}", File.read("result"))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_spawn_wordsplit
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
write_file("script", <<-'End')
|
|
|
|
File.open("result", "w") {|t| t << "hihi pid=#{$$} ppid=#{Process.ppid}" }
|
|
|
|
exit 6
|
|
|
|
End
|
|
|
|
str = "#{RUBY} script"
|
|
|
|
pid = spawn(str)
|
|
|
|
Process.wait pid
|
|
|
|
status = $?
|
|
|
|
assert_equal(pid, status.pid)
|
|
|
|
assert(status.exited?)
|
|
|
|
assert_equal(6, status.exitstatus)
|
|
|
|
assert_equal("hihi pid=#{status.pid} ppid=#{$$}", File.read("result"))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_popen_wordsplit
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
write_file("script", <<-'End')
|
|
|
|
print "fufu pid=#{$$} ppid=#{Process.ppid}"
|
|
|
|
exit 7
|
|
|
|
End
|
|
|
|
str = "#{RUBY} script"
|
|
|
|
io = IO.popen(str)
|
|
|
|
pid = io.pid
|
|
|
|
result = io.read
|
|
|
|
io.close
|
|
|
|
status = $?
|
|
|
|
assert_equal(pid, status.pid)
|
|
|
|
assert(status.exited?)
|
|
|
|
assert_equal(7, status.exitstatus)
|
|
|
|
assert_equal("fufu pid=#{status.pid} ppid=#{$$}", result)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exec_wordsplit
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
write_file("script", <<-'End')
|
2008-06-23 22:37:51 -04:00
|
|
|
File.open("result", "w") {|t|
|
|
|
|
if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
t << "hehe ppid=#{Process.ppid}"
|
|
|
|
else
|
|
|
|
t << "hehe pid=#{$$} ppid=#{Process.ppid}"
|
|
|
|
end
|
|
|
|
}
|
2008-04-27 02:10:35 -04:00
|
|
|
exit 6
|
|
|
|
End
|
2008-04-30 02:17:28 -04:00
|
|
|
write_file("s", <<-"End")
|
|
|
|
ruby = #{RUBY.dump}
|
|
|
|
exec "\#{ruby} script"
|
|
|
|
End
|
|
|
|
pid = spawn(RUBY, "s")
|
2008-04-27 02:10:35 -04:00
|
|
|
Process.wait pid
|
|
|
|
status = $?
|
|
|
|
assert_equal(pid, status.pid)
|
|
|
|
assert(status.exited?)
|
|
|
|
assert_equal(6, status.exitstatus)
|
2011-08-01 07:23:44 -04:00
|
|
|
if windows?
|
2008-06-23 22:37:51 -04:00
|
|
|
expected = "hehe ppid=#{status.pid}"
|
|
|
|
else
|
|
|
|
expected = "hehe pid=#{status.pid} ppid=#{$$}"
|
|
|
|
end
|
|
|
|
assert_equal(expected, File.read("result"))
|
2008-04-27 02:10:35 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_system_shell
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
write_file("script1", <<-'End')
|
|
|
|
File.open("result1", "w") {|t| t << "taka pid=#{$$} ppid=#{Process.ppid}" }
|
|
|
|
exit 7
|
|
|
|
End
|
|
|
|
write_file("script2", <<-'End')
|
|
|
|
File.open("result2", "w") {|t| t << "taki pid=#{$$} ppid=#{Process.ppid}" }
|
|
|
|
exit 8
|
|
|
|
End
|
2008-05-20 12:48:32 -04:00
|
|
|
ret = system("#{RUBY} script1 || #{RUBY} script2")
|
2008-04-27 02:10:35 -04:00
|
|
|
status = $?
|
|
|
|
assert_equal(false, ret)
|
|
|
|
assert(status.exited?)
|
|
|
|
result1 = File.read("result1")
|
|
|
|
result2 = File.read("result2")
|
|
|
|
assert_match(/\Ataka pid=\d+ ppid=\d+\z/, result1)
|
|
|
|
assert_match(/\Ataki pid=\d+ ppid=\d+\z/, result2)
|
|
|
|
assert_not_equal(result1[/\d+/].to_i, status.pid)
|
2009-03-22 01:51:58 -04:00
|
|
|
|
2011-08-01 07:23:44 -04:00
|
|
|
if windows?
|
2009-03-22 01:51:58 -04:00
|
|
|
Dir.mkdir(path = "path with space")
|
|
|
|
write_file(bat = path + "/bat test.bat", "@echo %1>out")
|
|
|
|
system(bat, "foo 'bar'")
|
|
|
|
assert_equal(%["foo 'bar'"\n], File.read("out"), '[ruby-core:22960]')
|
|
|
|
system(%[#{bat.dump} "foo 'bar'"])
|
|
|
|
assert_equal(%["foo 'bar'"\n], File.read("out"), '[ruby-core:22960]')
|
|
|
|
end
|
2008-04-27 02:10:35 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_spawn_shell
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
write_file("script1", <<-'End')
|
|
|
|
File.open("result1", "w") {|t| t << "taku pid=#{$$} ppid=#{Process.ppid}" }
|
|
|
|
exit 7
|
|
|
|
End
|
|
|
|
write_file("script2", <<-'End')
|
|
|
|
File.open("result2", "w") {|t| t << "take pid=#{$$} ppid=#{Process.ppid}" }
|
|
|
|
exit 8
|
|
|
|
End
|
2008-05-20 12:48:32 -04:00
|
|
|
pid = spawn("#{RUBY} script1 || #{RUBY} script2")
|
2008-04-27 02:10:35 -04:00
|
|
|
Process.wait pid
|
|
|
|
status = $?
|
|
|
|
assert(status.exited?)
|
|
|
|
assert(!status.success?)
|
|
|
|
result1 = File.read("result1")
|
|
|
|
result2 = File.read("result2")
|
|
|
|
assert_match(/\Ataku pid=\d+ ppid=\d+\z/, result1)
|
|
|
|
assert_match(/\Atake pid=\d+ ppid=\d+\z/, result2)
|
|
|
|
assert_not_equal(result1[/\d+/].to_i, status.pid)
|
2009-03-22 01:51:58 -04:00
|
|
|
|
2011-08-01 07:23:44 -04:00
|
|
|
if windows?
|
2009-03-22 01:51:58 -04:00
|
|
|
Dir.mkdir(path = "path with space")
|
|
|
|
write_file(bat = path + "/bat test.bat", "@echo %1>out")
|
|
|
|
pid = spawn(bat, "foo 'bar'")
|
|
|
|
Process.wait pid
|
|
|
|
status = $?
|
|
|
|
assert(status.exited?)
|
|
|
|
assert(status.success?)
|
|
|
|
assert_equal(%["foo 'bar'"\n], File.read("out"), '[ruby-core:22960]')
|
|
|
|
pid = spawn(%[#{bat.dump} "foo 'bar'"])
|
|
|
|
Process.wait pid
|
|
|
|
status = $?
|
|
|
|
assert(status.exited?)
|
|
|
|
assert(status.success?)
|
|
|
|
assert_equal(%["foo 'bar'"\n], File.read("out"), '[ruby-core:22960]')
|
|
|
|
end
|
2008-04-27 02:10:35 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_popen_shell
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
write_file("script1", <<-'End')
|
|
|
|
puts "tako pid=#{$$} ppid=#{Process.ppid}"
|
|
|
|
exit 7
|
|
|
|
End
|
|
|
|
write_file("script2", <<-'End')
|
|
|
|
puts "tika pid=#{$$} ppid=#{Process.ppid}"
|
|
|
|
exit 8
|
|
|
|
End
|
2008-05-20 12:48:32 -04:00
|
|
|
io = IO.popen("#{RUBY} script1 || #{RUBY} script2")
|
2008-04-27 02:10:35 -04:00
|
|
|
result = io.read
|
|
|
|
io.close
|
|
|
|
status = $?
|
|
|
|
assert(status.exited?)
|
|
|
|
assert(!status.success?)
|
|
|
|
assert_match(/\Atako pid=\d+ ppid=\d+\ntika pid=\d+ ppid=\d+\n\z/, result)
|
|
|
|
assert_not_equal(result[/\d+/].to_i, status.pid)
|
2009-03-20 19:52:43 -04:00
|
|
|
|
2011-08-01 07:23:44 -04:00
|
|
|
if windows?
|
2009-03-20 19:52:43 -04:00
|
|
|
Dir.mkdir(path = "path with space")
|
2009-03-22 01:51:58 -04:00
|
|
|
write_file(bat = path + "/bat test.bat", "@echo %1")
|
2009-03-20 19:52:43 -04:00
|
|
|
r = IO.popen([bat, "foo 'bar'"]) {|f| f.read}
|
|
|
|
assert_equal(%["foo 'bar'"\n], r, '[ruby-core:22960]')
|
2009-03-22 01:51:58 -04:00
|
|
|
r = IO.popen(%[#{bat.dump} "foo 'bar'"]) {|f| f.read}
|
|
|
|
assert_equal(%["foo 'bar'"\n], r, '[ruby-core:22960]')
|
2009-03-20 19:52:43 -04:00
|
|
|
end
|
2008-04-27 02:10:35 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exec_shell
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
write_file("script1", <<-'End')
|
|
|
|
File.open("result1", "w") {|t| t << "tiki pid=#{$$} ppid=#{Process.ppid}" }
|
|
|
|
exit 7
|
|
|
|
End
|
|
|
|
write_file("script2", <<-'End')
|
|
|
|
File.open("result2", "w") {|t| t << "tiku pid=#{$$} ppid=#{Process.ppid}" }
|
|
|
|
exit 8
|
|
|
|
End
|
2008-04-30 02:17:28 -04:00
|
|
|
write_file("s", <<-"End")
|
|
|
|
ruby = #{RUBY.dump}
|
2008-05-20 12:48:32 -04:00
|
|
|
exec("\#{ruby} script1 || \#{ruby} script2")
|
2008-04-30 02:17:28 -04:00
|
|
|
End
|
|
|
|
pid = spawn RUBY, "s"
|
2008-04-27 02:10:35 -04:00
|
|
|
Process.wait pid
|
|
|
|
status = $?
|
|
|
|
assert(status.exited?)
|
|
|
|
assert(!status.success?)
|
|
|
|
result1 = File.read("result1")
|
|
|
|
result2 = File.read("result2")
|
|
|
|
assert_match(/\Atiki pid=\d+ ppid=\d+\z/, result1)
|
|
|
|
assert_match(/\Atiku pid=\d+ ppid=\d+\z/, result2)
|
|
|
|
assert_not_equal(result1[/\d+/].to_i, status.pid)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-30 08:38:47 -04:00
|
|
|
def test_argv0
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
assert_equal(false, system([RUBY, "asdfg"], "-e", "exit false"))
|
|
|
|
assert_equal(true, system([RUBY, "zxcvb"], "-e", "exit true"))
|
|
|
|
|
|
|
|
Process.wait spawn([RUBY, "poiu"], "-e", "exit 4")
|
|
|
|
assert_equal(4, $?.exitstatus)
|
|
|
|
|
|
|
|
assert_equal("1", IO.popen([[RUBY, "qwerty"], "-e", "print 1"]).read)
|
2008-06-04 12:30:03 -04:00
|
|
|
Process.wait
|
2008-04-30 08:38:47 -04:00
|
|
|
|
|
|
|
write_file("s", <<-"End")
|
|
|
|
exec([#{RUBY.dump}, "lkjh"], "-e", "exit 5")
|
|
|
|
End
|
|
|
|
pid = spawn RUBY, "s"
|
|
|
|
Process.wait pid
|
|
|
|
assert_equal(5, $?.exitstatus)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-05-11 05:01:31 -04:00
|
|
|
def with_stdin(filename)
|
|
|
|
open(filename) {|f|
|
|
|
|
begin
|
|
|
|
old = STDIN.dup
|
|
|
|
begin
|
|
|
|
STDIN.reopen(filename)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
STDIN.reopen(old)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
old.close
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-30 08:38:47 -04:00
|
|
|
def test_argv0_noarg
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
open("t", "w") {|f| f.print "exit true" }
|
|
|
|
open("f", "w") {|f| f.print "exit false" }
|
|
|
|
|
2008-05-11 05:01:31 -04:00
|
|
|
with_stdin("t") { assert_equal(true, system([RUBY, "qaz"])) }
|
|
|
|
with_stdin("f") { assert_equal(false, system([RUBY, "wsx"])) }
|
2008-04-30 08:38:47 -04:00
|
|
|
|
2008-05-11 05:01:31 -04:00
|
|
|
with_stdin("t") { Process.wait spawn([RUBY, "edc"]) }
|
2008-04-30 08:38:47 -04:00
|
|
|
assert($?.success?)
|
2008-05-11 05:01:31 -04:00
|
|
|
with_stdin("f") { Process.wait spawn([RUBY, "rfv"]) }
|
2008-04-30 08:38:47 -04:00
|
|
|
assert(!$?.success?)
|
|
|
|
|
2008-05-11 05:01:31 -04:00
|
|
|
with_stdin("t") { IO.popen([[RUBY, "tgb"]]) {|io| assert_equal("", io.read) } }
|
2008-04-30 08:38:47 -04:00
|
|
|
assert($?.success?)
|
2008-05-11 05:01:31 -04:00
|
|
|
with_stdin("f") { IO.popen([[RUBY, "yhn"]]) {|io| assert_equal("", io.read) } }
|
2008-04-30 08:38:47 -04:00
|
|
|
assert(!$?.success?)
|
|
|
|
|
2008-05-11 05:01:31 -04:00
|
|
|
status = run_in_child "STDIN.reopen('t'); exec([#{RUBY.dump}, 'ujm'])"
|
2008-04-30 08:38:47 -04:00
|
|
|
assert(status.success?)
|
2008-05-11 05:01:31 -04:00
|
|
|
status = run_in_child "STDIN.reopen('f'); exec([#{RUBY.dump}, 'ik,'])"
|
2008-04-30 08:38:47 -04:00
|
|
|
assert(!status.success?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-06-04 12:30:03 -04:00
|
|
|
def test_status
|
|
|
|
with_tmpchdir do
|
|
|
|
s = run_in_child("exit 1")
|
|
|
|
assert_equal("#<Process::Status: pid #{ s.pid } exit #{ s.exitstatus }>", s.inspect)
|
|
|
|
|
|
|
|
assert_equal(s, s)
|
|
|
|
assert_equal(s, s.to_i)
|
|
|
|
|
|
|
|
assert_equal(s.to_i & 0x55555555, s & 0x55555555)
|
|
|
|
assert_equal(s.to_i >> 1, s >> 1)
|
|
|
|
assert_equal(false, s.stopped?)
|
|
|
|
assert_equal(nil, s.stopsig)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_status_kill
|
|
|
|
return unless Process.respond_to?(:kill)
|
|
|
|
return unless Signal.list.include?("QUIT")
|
|
|
|
|
|
|
|
with_tmpchdir do
|
|
|
|
write_file("foo", "sleep 30")
|
|
|
|
pid = spawn(RUBY, "foo")
|
|
|
|
Thread.new { sleep 1; Process.kill(:SIGQUIT, pid) }
|
|
|
|
Process.wait(pid)
|
|
|
|
s = $?
|
2010-05-25 10:25:55 -04:00
|
|
|
assert_equal([false, true, false],
|
|
|
|
[s.exited?, s.signaled?, s.stopped?],
|
|
|
|
"[s.exited?, s.signaled?, s.stopped?]")
|
2008-06-28 12:53:56 -04:00
|
|
|
assert_send(
|
|
|
|
[["#<Process::Status: pid #{ s.pid } SIGQUIT (signal #{ s.termsig })>",
|
|
|
|
"#<Process::Status: pid #{ s.pid } SIGQUIT (signal #{ s.termsig }) (core dumped)>"],
|
|
|
|
:include?,
|
|
|
|
s.inspect])
|
2008-06-04 12:30:03 -04:00
|
|
|
assert_equal(false, s.exited?)
|
|
|
|
assert_equal(nil, s.success?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_without_arg
|
|
|
|
with_tmpchdir do
|
|
|
|
write_file("foo", "sleep 0.1")
|
|
|
|
pid = spawn(RUBY, "foo")
|
|
|
|
assert_equal(pid, Process.wait)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait2
|
|
|
|
with_tmpchdir do
|
|
|
|
write_file("foo", "sleep 0.1")
|
|
|
|
pid = spawn(RUBY, "foo")
|
|
|
|
assert_equal([pid, 0], Process.wait2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_waitall
|
|
|
|
with_tmpchdir do
|
|
|
|
write_file("foo", "sleep 0.1")
|
|
|
|
ps = (0...3).map { spawn(RUBY, "foo") }.sort
|
|
|
|
ss = Process.waitall.sort
|
|
|
|
ps.zip(ss) do |p1, (p2, s)|
|
|
|
|
assert_equal(p1, p2)
|
|
|
|
assert_equal(p1, s.pid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_abort
|
|
|
|
with_tmpchdir do
|
|
|
|
s = run_in_child("abort")
|
|
|
|
assert_not_equal(0, s.exitstatus)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sleep
|
|
|
|
assert_raise(ArgumentError) { sleep(1, 1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_getpgid
|
|
|
|
assert_kind_of(Integer, Process.getpgid(Process.ppid))
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_getpriority
|
2008-07-17 08:09:21 -04:00
|
|
|
assert_kind_of(Integer, Process.getpriority(Process::PRIO_PROCESS, $$))
|
2008-06-23 21:26:54 -04:00
|
|
|
rescue NameError, NotImplementedError
|
2008-06-04 12:30:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_setpriority
|
2008-06-23 21:26:54 -04:00
|
|
|
if defined? Process::PRIO_USER
|
|
|
|
assert_nothing_raised do
|
2008-07-17 08:09:21 -04:00
|
|
|
pr = Process.getpriority(Process::PRIO_PROCESS, $$)
|
|
|
|
Process.setpriority(Process::PRIO_PROCESS, $$, pr)
|
2008-06-23 21:26:54 -04:00
|
|
|
end
|
2008-06-04 12:30:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_getuid
|
|
|
|
assert_kind_of(Integer, Process.uid)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_groups
|
|
|
|
gs = Process.groups
|
|
|
|
assert_instance_of(Array, gs)
|
|
|
|
gs.each {|g| assert_kind_of(Integer, g) }
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_maxgroups
|
|
|
|
assert_kind_of(Integer, Process.maxgroups)
|
2011-03-10 11:54:02 -05:00
|
|
|
rescue NotImplementedError
|
2008-06-04 12:30:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_geteuid
|
|
|
|
assert_kind_of(Integer, Process.egid)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_uid_re_exchangeable_p
|
|
|
|
r = Process::UID.re_exchangeable?
|
|
|
|
assert(true == r || false == r)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gid_re_exchangeable_p
|
|
|
|
r = Process::GID.re_exchangeable?
|
|
|
|
assert(true == r || false == r)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_uid_sid_available?
|
|
|
|
r = Process::UID.sid_available?
|
|
|
|
assert(true == r || false == r)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gid_sid_available?
|
|
|
|
r = Process::GID.sid_available?
|
|
|
|
assert(true == r || false == r)
|
|
|
|
end
|
|
|
|
|
2008-08-21 06:18:38 -04:00
|
|
|
def test_pst_inspect
|
|
|
|
assert_nothing_raised { Process::Status.allocate.inspect }
|
|
|
|
end
|
2008-12-29 09:41:22 -05:00
|
|
|
|
|
|
|
def test_wait_and_sigchild
|
2011-07-27 11:30:11 -04:00
|
|
|
if /freebsd|openbsd/ =~ RUBY_PLATFORM
|
|
|
|
# this relates #4173
|
|
|
|
# When ruby can use 2 cores, signal and wait4 may miss the signal.
|
|
|
|
skip "this fails on FreeBSD and OpenBSD on multithreaded environment"
|
2011-02-06 23:27:46 -05:00
|
|
|
end
|
2008-12-29 09:41:22 -05:00
|
|
|
signal_received = []
|
2009-01-02 08:18:57 -05:00
|
|
|
Signal.trap(:CHLD) { signal_received << true }
|
2010-11-16 01:20:06 -05:00
|
|
|
pid = fork { sleep 0.1; exit }
|
2008-12-29 09:41:22 -05:00
|
|
|
Thread.start { raise }
|
|
|
|
Process.wait pid
|
2010-11-16 01:20:06 -05:00
|
|
|
sleep 0.1
|
2008-12-29 09:41:22 -05:00
|
|
|
assert_equal [true], signal_received, " [ruby-core:19744]"
|
2009-01-22 01:27:44 -05:00
|
|
|
rescue NotImplementedError, ArgumentError
|
2008-12-29 09:41:22 -05:00
|
|
|
ensure
|
2009-01-22 01:27:44 -05:00
|
|
|
begin
|
|
|
|
Signal.trap(:CHLD, 'DEFAULT')
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
2008-12-29 09:41:22 -05:00
|
|
|
end
|
2010-01-11 13:15:25 -05:00
|
|
|
|
|
|
|
def test_no_curdir
|
|
|
|
with_tmpchdir {|d|
|
|
|
|
Dir.mkdir("vd")
|
|
|
|
status = nil
|
|
|
|
Dir.chdir("vd") {
|
2010-02-06 06:47:00 -05:00
|
|
|
dir = "#{d}/vd"
|
|
|
|
# OpenSolaris cannot remove the current directory.
|
2011-01-13 21:02:12 -05:00
|
|
|
system(RUBY, "--disable-gems", "-e", "Dir.chdir '..'; Dir.rmdir #{dir.dump}")
|
|
|
|
system({"RUBYLIB"=>nil}, RUBY, "--disable-gems", "-e", "exit true")
|
2010-01-11 13:15:25 -05:00
|
|
|
status = $?
|
|
|
|
}
|
2010-01-11 13:21:47 -05:00
|
|
|
assert(status.success?, "[ruby-dev:38105]")
|
2010-01-11 13:15:25 -05:00
|
|
|
}
|
2011-03-24 22:50:17 -04:00
|
|
|
end unless /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
|
2010-11-04 12:21:38 -04:00
|
|
|
|
|
|
|
def test_fallback_to_sh
|
|
|
|
feature = '[ruby-core:32745]'
|
|
|
|
with_tmpchdir do |d|
|
|
|
|
open("tmp_script.#{$$}", "w") {|f| f.puts ": ;"; f.chmod(0755)}
|
|
|
|
assert_not_nil(pid = Process.spawn("./tmp_script.#{$$}"), feature)
|
|
|
|
wpid, st = Process.waitpid2(pid)
|
|
|
|
assert_equal([pid, true], [wpid, st.success?], feature)
|
|
|
|
|
|
|
|
open("tmp_script.#{$$}", "w") {|f| f.puts "echo $#: $@"; f.chmod(0755)}
|
|
|
|
result = IO.popen(["./tmp_script.#{$$}", "a b", "c"]) {|f| f.read}
|
|
|
|
assert_equal("2: a b c\n", result, feature)
|
|
|
|
end
|
2010-11-04 23:23:10 -04:00
|
|
|
end if File.executable?("/bin/sh")
|
2011-01-26 18:41:47 -05:00
|
|
|
|
|
|
|
def test_too_long_path
|
|
|
|
bug4314 = '[ruby-core:34842]'
|
2011-01-27 21:48:46 -05:00
|
|
|
exs = [Errno::ENOENT]
|
|
|
|
exs << Errno::E2BIG if defined?(Errno::E2BIG)
|
|
|
|
assert_raise(*exs, bug4314) {Process.spawn("a" * 10_000_000)}
|
2011-01-27 11:33:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_too_long_path2
|
|
|
|
bug4315 = '[ruby-core:34833]'
|
2011-01-27 21:48:46 -05:00
|
|
|
exs = [Errno::ENOENT]
|
|
|
|
exs << Errno::E2BIG if defined?(Errno::E2BIG)
|
|
|
|
assert_raise(*exs, bug4315) {Process.spawn('"a"|'*10_000_000)}
|
2011-01-26 18:41:47 -05:00
|
|
|
end
|
2011-05-28 09:52:03 -04:00
|
|
|
|
|
|
|
def test_system_sigpipe
|
2011-08-01 07:23:44 -04:00
|
|
|
return if windows?
|
2011-05-28 09:52:03 -04:00
|
|
|
|
|
|
|
pid = 0
|
|
|
|
|
|
|
|
with_tmpchdir do
|
|
|
|
assert_nothing_raised('[ruby-dev:12261]') do
|
|
|
|
timeout(3) do
|
|
|
|
pid = spawn('yes | ls')
|
|
|
|
Process.waitpid pid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Process.kill(:KILL, pid) if (pid != 0) rescue false
|
|
|
|
end
|
2011-06-24 04:06:38 -04:00
|
|
|
|
|
|
|
if Process.respond_to?(:daemon)
|
|
|
|
def test_daemon_default
|
|
|
|
data = IO.popen("-", "r+") do |f|
|
|
|
|
break f.read if f
|
|
|
|
Process.daemon
|
|
|
|
puts "ng"
|
|
|
|
end
|
|
|
|
assert_equal("", data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_daemon_noclose
|
|
|
|
data = IO.popen("-", "r+") do |f|
|
|
|
|
break f.read if f
|
|
|
|
Process.daemon(false, true)
|
|
|
|
puts "ok", Dir.pwd
|
|
|
|
end
|
|
|
|
assert_equal("ok\n/\n", data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_daemon_nochdir_noclose
|
|
|
|
data = IO.popen("-", "r+") do |f|
|
|
|
|
break f.read if f
|
|
|
|
Process.daemon(true, true)
|
|
|
|
puts "ok", Dir.pwd
|
|
|
|
end
|
|
|
|
assert_equal("ok\n#{Dir.pwd}\n", data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_daemon_readwrite
|
|
|
|
data = IO.popen("-", "r+") do |f|
|
|
|
|
if f
|
|
|
|
f.puts "ok?"
|
|
|
|
break f.read
|
|
|
|
end
|
|
|
|
Process.daemon(true, true)
|
|
|
|
puts STDIN.gets
|
|
|
|
end
|
|
|
|
assert_equal("ok?\n", data)
|
|
|
|
end
|
|
|
|
|
|
|
|
if File.directory?("/proc/self/task")
|
|
|
|
def test_daemon_no_threads
|
|
|
|
pid, data = IO.popen("-", "r+") do |f|
|
|
|
|
break f.pid, f.readlines if f
|
|
|
|
Process.daemon(true, true)
|
|
|
|
puts Dir.entries("/proc/self/task") - %W[. ..]
|
|
|
|
end
|
|
|
|
bug4920 = '[ruby-dev:43873]'
|
|
|
|
assert_equal(2, data.size, bug4920)
|
|
|
|
assert_not_include(data.map(&:to_i), pid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-06-14 23:39:20 -04:00
|
|
|
end
|