mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
move Test::Unit::Assertions#invoke_ruby to EnvUtil.invoke_ruby.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25867 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
64e6f141fa
commit
34286de2e2
1 changed files with 53 additions and 54 deletions
|
@ -33,6 +33,7 @@ module EnvUtil
|
|||
module_function :rubybin
|
||||
|
||||
LANG_ENVS = %w"LANG LC_ALL LC_CTYPE"
|
||||
|
||||
def rubyexec(*args)
|
||||
ruby = EnvUtil.rubybin
|
||||
c = "C"
|
||||
|
@ -66,6 +67,56 @@ module EnvUtil
|
|||
end
|
||||
module_function :rubyexec
|
||||
|
||||
def invoke_ruby(args, stdin_data="", capture_stdout=false, capture_stderr=false, opt={})
|
||||
begin
|
||||
in_c, in_p = IO.pipe
|
||||
out_p, out_c = IO.pipe if capture_stdout
|
||||
err_p, err_c = IO.pipe if capture_stderr
|
||||
c = "C"
|
||||
env = {}
|
||||
LANG_ENVS.each {|lc| env[lc], ENV[lc] = ENV[lc], c}
|
||||
opt = opt.dup
|
||||
opt[:in] = in_c
|
||||
opt[:out] = out_c if capture_stdout
|
||||
opt[:err] = err_c if capture_stderr
|
||||
pid = spawn(EnvUtil.rubybin, *args, opt)
|
||||
in_c.close
|
||||
out_c.close if capture_stdout
|
||||
err_c.close if capture_stderr
|
||||
in_p.write stdin_data
|
||||
in_p.close
|
||||
th_stdout = Thread.new { out_p.read } if capture_stdout
|
||||
th_stderr = Thread.new { err_p.read } if capture_stderr
|
||||
if (!capture_stdout || th_stdout.join(10)) && (!capture_stderr || th_stderr.join(10))
|
||||
stdout = th_stdout.value if capture_stdout
|
||||
stderr = th_stderr.value if capture_stderr
|
||||
else
|
||||
flunk("timeout")
|
||||
end
|
||||
out_p.close if capture_stdout
|
||||
err_p.close if capture_stderr
|
||||
Process.wait pid
|
||||
status = $?
|
||||
ensure
|
||||
env.each_pair {|lc, v|
|
||||
if v
|
||||
ENV[lc] = v
|
||||
else
|
||||
ENV.delete(lc)
|
||||
end
|
||||
} if env
|
||||
in_c.close if in_c && !in_c.closed?
|
||||
in_p.close if in_p && !in_p.closed?
|
||||
out_c.close if out_c && !out_c.closed?
|
||||
out_p.close if out_p && !out_p.closed?
|
||||
err_c.close if err_c && !err_c.closed?
|
||||
err_p.close if err_p && !err_p.closed?
|
||||
(th_stdout.kill; th_stdout.join) if th_stdout
|
||||
(th_stderr.kill; th_stderr.join) if th_stderr
|
||||
end
|
||||
return stdout, stderr, status
|
||||
end
|
||||
module_function :invoke_ruby
|
||||
|
||||
def verbose_warning
|
||||
class << (stderr = "")
|
||||
|
@ -126,60 +177,8 @@ module Test
|
|||
out_p.close if out_p && !out_p.closed?
|
||||
end
|
||||
|
||||
LANG_ENVS = %w"LANG LC_ALL LC_CTYPE"
|
||||
|
||||
def invoke_ruby(args, stdin_data="", capture_stdout=false, capture_stderr=false, opt={})
|
||||
begin
|
||||
in_c, in_p = IO.pipe
|
||||
out_p, out_c = IO.pipe if capture_stdout
|
||||
err_p, err_c = IO.pipe if capture_stderr
|
||||
c = "C"
|
||||
env = {}
|
||||
LANG_ENVS.each {|lc| env[lc], ENV[lc] = ENV[lc], c}
|
||||
opt = opt.dup
|
||||
opt[:in] = in_c
|
||||
opt[:out] = out_c if capture_stdout
|
||||
opt[:err] = err_c if capture_stderr
|
||||
pid = spawn(EnvUtil.rubybin, *args, opt)
|
||||
in_c.close
|
||||
out_c.close if capture_stdout
|
||||
err_c.close if capture_stderr
|
||||
in_p.write stdin_data
|
||||
in_p.close
|
||||
th_stdout = Thread.new { out_p.read } if capture_stdout
|
||||
th_stderr = Thread.new { err_p.read } if capture_stderr
|
||||
if (!capture_stdout || th_stdout.join(10)) && (!capture_stderr || th_stderr.join(10))
|
||||
stdout = th_stdout.value if capture_stdout
|
||||
stderr = th_stderr.value if capture_stderr
|
||||
else
|
||||
flunk("timeout")
|
||||
end
|
||||
out_p.close if capture_stdout
|
||||
err_p.close if capture_stderr
|
||||
Process.wait pid
|
||||
status = $?
|
||||
ensure
|
||||
env.each_pair {|lc, v|
|
||||
if v
|
||||
ENV[lc] = v
|
||||
else
|
||||
ENV.delete(lc)
|
||||
end
|
||||
} if env
|
||||
in_c.close if in_c && !in_c.closed?
|
||||
in_p.close if in_p && !in_p.closed?
|
||||
out_c.close if out_c && !out_c.closed?
|
||||
out_p.close if out_p && !out_p.closed?
|
||||
err_c.close if err_c && !err_c.closed?
|
||||
err_p.close if err_p && !err_p.closed?
|
||||
(th_stdout.kill; th_stdout.join) if th_stdout
|
||||
(th_stderr.kill; th_stderr.join) if th_stderr
|
||||
end
|
||||
return stdout, stderr, status
|
||||
end
|
||||
|
||||
def assert_in_out_err(args, test_stdin = "", test_stdout = [], test_stderr = [], message = nil, opt={})
|
||||
stdout, stderr, status = invoke_ruby(args, test_stdin, true, true, opt)
|
||||
stdout, stderr, status = EnvUtil.invoke_ruby(args, test_stdin, true, true, opt)
|
||||
if block_given?
|
||||
yield(stdout.lines.map {|l| l.chomp }, stderr.lines.map {|l| l.chomp })
|
||||
else
|
||||
|
@ -197,7 +196,7 @@ module Test
|
|||
end
|
||||
|
||||
def assert_ruby_status(args, test_stdin="", message=nil, opt={})
|
||||
stdout, stderr, status = invoke_ruby(args, test_stdin, false, false, opt)
|
||||
stdout, stderr, status = EnvUtil.invoke_ruby(args, test_stdin, false, false, opt)
|
||||
m = message ? "#{message} (#{status.inspect})" : "ruby exit stauts is not success: #{status.inspect}"
|
||||
assert(status.success?, m)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue