2008-04-23 11:22:13 -04:00
|
|
|
require "open3"
|
|
|
|
require "timeout"
|
|
|
|
|
2003-10-04 22:56:42 -04:00
|
|
|
module EnvUtil
|
|
|
|
def rubybin
|
2006-02-03 04:15:42 -05:00
|
|
|
unless ENV["RUBYOPT"]
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-02-03 04:15:42 -05:00
|
|
|
end
|
2004-05-19 10:45:49 -04:00
|
|
|
if ruby = ENV["RUBY"]
|
|
|
|
return ruby
|
|
|
|
end
|
2005-12-29 03:05:26 -05:00
|
|
|
ruby = "ruby"
|
2006-02-03 04:15:42 -05:00
|
|
|
rubyexe = ruby+".exe"
|
2003-10-13 09:05:24 -04:00
|
|
|
3.times do
|
2006-02-03 04:15:42 -05:00
|
|
|
if File.exist? ruby and File.executable? ruby and !File.directory? ruby
|
|
|
|
return File.expand_path(ruby)
|
|
|
|
end
|
|
|
|
if File.exist? rubyexe and File.executable? rubyexe
|
2008-05-12 08:35:37 -04:00
|
|
|
return File.expand_path(rubyexe)
|
2003-10-13 09:05:24 -04:00
|
|
|
end
|
2005-12-29 03:05:26 -05:00
|
|
|
ruby = File.join("..", ruby)
|
2003-10-13 10:59:25 -04:00
|
|
|
end
|
|
|
|
begin
|
|
|
|
require "rbconfig"
|
|
|
|
File.join(
|
* mkconfig.rb: generate RbConfig instead of Config.
* instruby.rb, rubytest.rb, runruby.rb, bcc32/Makefile.sub,
ext/extmk.rb, ext/dl/extconf.rb, ext/iconv/charset_alias.rb,
lib/mkmf.rb, lib/rdoc/ri/ri_paths.rb,
lib/webrick/httpservlet/cgihandler.rb,
test/dbm/test_dbm.rb, test/gdbm/test_gdbm.rb,
test/ruby/envutil.rb, test/soap/calc/test_calc_cgi.rb,
test/soap/header/test_authheader_cgi.rb, test/soap/ssl/test_ssl.rb,
win32/mkexports.rb, win32/resource.rb: Use RbConfig instead of
Config.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9970 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-02-20 03:34:53 -05:00
|
|
|
RbConfig::CONFIG["bindir"],
|
|
|
|
RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"]
|
2003-10-13 10:59:25 -04:00
|
|
|
)
|
|
|
|
rescue LoadError
|
|
|
|
"ruby"
|
2003-10-04 22:56:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :rubybin
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
LANG_ENVS = %w"LANG LC_ALL LC_CTYPE"
|
|
|
|
def rubyexec(*args)
|
|
|
|
ruby = EnvUtil.rubybin
|
|
|
|
c = "C"
|
|
|
|
env = {}
|
|
|
|
LANG_ENVS.each {|lc| env[lc], ENV[lc] = ENV[lc], c}
|
2008-04-24 10:02:15 -04:00
|
|
|
stdin = stdout = stderr = nil
|
2008-04-23 11:22:13 -04:00
|
|
|
Timeout.timeout(10) do
|
2008-04-24 10:02:15 -04:00
|
|
|
stdin, stdout, stderr = Open3.popen3(*([ruby] + args))
|
|
|
|
env.each_pair {|lc, v|
|
|
|
|
if v
|
|
|
|
ENV[lc] = v
|
|
|
|
else
|
|
|
|
ENV.delete(lc)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
env = nil
|
2008-04-23 11:22:13 -04:00
|
|
|
yield(stdin, stdout, stderr)
|
|
|
|
end
|
|
|
|
|
|
|
|
ensure
|
|
|
|
env.each_pair {|lc, v|
|
|
|
|
if v
|
|
|
|
ENV[lc] = v
|
|
|
|
else
|
|
|
|
ENV.delete(lc)
|
|
|
|
end
|
|
|
|
} if env
|
|
|
|
stdin .close unless !stdin || stdin .closed?
|
|
|
|
stdout.close unless !stdout || stdout.closed?
|
|
|
|
stderr.close unless !stderr || stderr.closed?
|
|
|
|
end
|
|
|
|
module_function :rubyexec
|
2003-10-04 22:56:42 -04:00
|
|
|
end
|
2008-05-03 07:57:55 -04:00
|
|
|
|
|
|
|
module Test
|
|
|
|
module Unit
|
|
|
|
module Assertions
|
|
|
|
public
|
|
|
|
def assert_normal_exit(testsrc, message = '')
|
2008-05-21 22:40:50 -04:00
|
|
|
in_c, in_p = IO.pipe
|
|
|
|
out_p, out_c = IO.pipe
|
|
|
|
pid = spawn(EnvUtil.rubybin, '-W0', STDIN=>in_c, STDOUT=>out_c, STDERR=>out_c)
|
|
|
|
in_c.close
|
|
|
|
out_c.close
|
2008-07-10 06:45:51 -04:00
|
|
|
in_p.write testsrc
|
|
|
|
in_p.close
|
2008-05-21 22:40:50 -04:00
|
|
|
msg = out_p.read
|
|
|
|
out_p.close
|
|
|
|
Process.wait pid
|
2008-05-03 07:57:55 -04:00
|
|
|
status = $?
|
|
|
|
faildesc = nil
|
|
|
|
if status.signaled?
|
|
|
|
signo = status.termsig
|
|
|
|
signame = Signal.list.invert[signo]
|
|
|
|
sigdesc = "signal #{signo}"
|
|
|
|
if signame
|
|
|
|
sigdesc = "SIG#{signame} (#{sigdesc})"
|
|
|
|
end
|
2008-05-31 21:32:54 -04:00
|
|
|
if status.coredump?
|
|
|
|
sigdesc << " (core dumped)"
|
|
|
|
end
|
2008-05-21 22:40:50 -04:00
|
|
|
if msg.empty?
|
2008-05-30 23:03:24 -04:00
|
|
|
full_message = build_message(message, "pid ? killed by ?",
|
|
|
|
pid,
|
2008-05-25 12:19:05 -04:00
|
|
|
AssertionMessage::Literal.new(sigdesc))
|
2008-05-21 22:40:50 -04:00
|
|
|
else
|
|
|
|
msg << "\n" if /\n\z/ !~ msg
|
2008-05-30 23:03:24 -04:00
|
|
|
full_message = build_message(message, "pid ? killed by ?\n?",
|
|
|
|
pid,
|
2008-05-25 12:19:05 -04:00
|
|
|
AssertionMessage::Literal.new(sigdesc),
|
2008-05-21 22:40:50 -04:00
|
|
|
AssertionMessage::Literal.new(msg.gsub(/^/, '| ')))
|
|
|
|
end
|
2008-05-03 07:57:55 -04:00
|
|
|
end
|
|
|
|
assert_block(full_message) { !status.signaled? }
|
2008-05-21 22:40:50 -04:00
|
|
|
ensure
|
|
|
|
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?
|
2008-05-03 07:57:55 -04:00
|
|
|
end
|
2008-07-15 11:26:04 -04:00
|
|
|
|
|
|
|
LANG_ENVS = %w"LANG LC_ALL LC_CTYPE"
|
|
|
|
def assert_in_out_err(args, test_stdin = "", test_stdout = "", test_stderr = "", message = nil)
|
|
|
|
in_c, in_p = IO.pipe
|
|
|
|
out_p, out_c = IO.pipe
|
|
|
|
err_p, err_c = IO.pipe
|
|
|
|
c = "C"
|
|
|
|
env = {}
|
|
|
|
LANG_ENVS.each {|lc| env[lc], ENV[lc] = ENV[lc], c}
|
2008-10-26 23:13:54 -04:00
|
|
|
pid = spawn(EnvUtil.rubybin, *args, STDIN=>in_c, STDOUT=>out_c, STDERR=>err_c)
|
2008-07-15 11:26:04 -04:00
|
|
|
in_c.close
|
|
|
|
out_c.close
|
|
|
|
err_c.close
|
|
|
|
in_p.write test_stdin
|
|
|
|
in_p.close
|
|
|
|
th_stdout = Thread.new { out_p.read }
|
|
|
|
th_stderr = Thread.new { err_p.read }
|
|
|
|
if th_stdout.join(10) && th_stderr.join(10)
|
|
|
|
stdout = th_stdout.value
|
|
|
|
stderr = th_stderr.value
|
|
|
|
else
|
|
|
|
flunk("timeout")
|
|
|
|
end
|
|
|
|
out_p.close
|
|
|
|
err_p.close
|
|
|
|
Process.wait pid
|
|
|
|
if block_given?
|
|
|
|
yield(stdout.lines.map {|l| l.chomp }, stderr.lines.map {|l| l.chomp })
|
|
|
|
else
|
|
|
|
if test_stdout.is_a?(Regexp)
|
|
|
|
assert_match(test_stdout, stdout, message)
|
|
|
|
else
|
|
|
|
assert_equal(test_stdout, stdout.lines.map {|l| l.chomp }, message)
|
|
|
|
end
|
|
|
|
if test_stderr.is_a?(Regexp)
|
|
|
|
assert_match(test_stderr, stderr, message)
|
|
|
|
else
|
|
|
|
assert_equal(test_stderr, stderr.lines.map {|l| l.chomp }, message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
2008-05-03 07:57:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|