2007-02-25 04:28:14 +03:00
|
|
|
# $Id$
|
2007-02-24 10:47:53 +03:00
|
|
|
|
|
|
|
# NOTE:
|
|
|
|
# Never use optparse in this file.
|
|
|
|
# Never use test/unit in this file.
|
|
|
|
# Never use Ruby extensions in this file.
|
|
|
|
|
2007-07-06 13:23:53 +04:00
|
|
|
begin
|
|
|
|
require 'fileutils'
|
|
|
|
require 'tmpdir'
|
|
|
|
rescue LoadError
|
|
|
|
$:.unshift File.join(File.dirname(__FILE__), '../lib')
|
|
|
|
retry
|
|
|
|
end
|
2007-02-24 10:47:53 +03:00
|
|
|
|
2007-08-17 09:41:10 +04:00
|
|
|
if !Dir.respond_to?(:mktmpdir)
|
|
|
|
# copied from lib/tmpdir.rb
|
|
|
|
def Dir.mktmpdir(prefix="d", tmpdir=nil)
|
|
|
|
tmpdir ||= Dir.tmpdir
|
|
|
|
t = Time.now.strftime("%Y%m%d")
|
|
|
|
n = nil
|
|
|
|
begin
|
|
|
|
path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
|
|
|
|
path << "-#{n}" if n
|
|
|
|
Dir.mkdir(path, 0700)
|
|
|
|
rescue Errno::EEXIST
|
|
|
|
n ||= 0
|
|
|
|
n += 1
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
if block_given?
|
|
|
|
begin
|
|
|
|
yield path
|
|
|
|
ensure
|
|
|
|
FileUtils.remove_entry_secure path
|
|
|
|
end
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-24 10:47:53 +03:00
|
|
|
def main
|
2007-02-25 05:10:38 +03:00
|
|
|
@ruby = File.expand_path('miniruby')
|
2007-02-24 13:28:36 +03:00
|
|
|
@verbose = false
|
2007-08-17 09:41:10 +04:00
|
|
|
dir = nil
|
2007-07-06 09:50:42 +04:00
|
|
|
quiet = false
|
2007-02-24 10:47:53 +03:00
|
|
|
tests = nil
|
|
|
|
ARGV.delete_if {|arg|
|
|
|
|
case arg
|
|
|
|
when /\A--ruby=(.*)/
|
2008-01-30 05:59:59 +03:00
|
|
|
@ruby = $1
|
|
|
|
@ruby.gsub!(/^([^ ]*)/){File.expand_path($1)}
|
2008-05-11 17:54:04 +04:00
|
|
|
@ruby.gsub!(/(\s+-I\s*)((?!(?:\.\/)*-(?:\s|\z))\S+)/){$1+File.expand_path($2)}
|
2008-05-11 17:39:35 +04:00
|
|
|
@ruby.gsub!(/(\s+-r\s*)(\.\.?\/\S+)/){$1+File.expand_path($2)}
|
2007-02-24 10:47:53 +03:00
|
|
|
true
|
|
|
|
when /\A--sets=(.*)/
|
|
|
|
tests = Dir.glob("#{File.dirname($0)}/test_{#{$1}}*.rb")
|
|
|
|
puts tests.map {|path| File.basename(path) }.inspect
|
|
|
|
true
|
2007-06-24 10:52:59 +04:00
|
|
|
when /\A--dir=(.*)/
|
|
|
|
dir = $1
|
|
|
|
true
|
2007-09-14 11:18:23 +04:00
|
|
|
when /\A(--stress|-s)/
|
|
|
|
$stress = true
|
2007-07-06 09:50:42 +04:00
|
|
|
when /\A(-q|--q(uiet))\z/
|
|
|
|
quiet = true
|
|
|
|
true
|
2007-02-24 13:28:36 +03:00
|
|
|
when /\A(-v|--v(erbose))\z/
|
|
|
|
@verbose = true
|
2007-02-24 10:53:14 +03:00
|
|
|
when /\A(-h|--h(elp)?)\z/
|
2007-02-24 10:51:52 +03:00
|
|
|
puts(<<-End)
|
|
|
|
Usage: #{File.basename($0, '.*')} --ruby=PATH [--sets=NAME,NAME,...]
|
2007-02-24 13:28:36 +03:00
|
|
|
--sets=NAME,NAME,... Name of test sets.
|
2007-06-24 10:52:59 +04:00
|
|
|
--dir=DIRECTORY Working directory.
|
|
|
|
default: /tmp/bootstraptest.tmpwd
|
2007-09-14 11:18:23 +04:00
|
|
|
-s, --stress stress test.
|
2007-02-24 13:28:36 +03:00
|
|
|
-v, --verbose Output test name before exec.
|
2007-07-06 09:50:42 +04:00
|
|
|
-q, --quiet Don\'t print header message.
|
2007-02-24 13:28:36 +03:00
|
|
|
-h, --help Print this message and quit.
|
2007-02-24 10:51:52 +03:00
|
|
|
End
|
2008-05-11 17:54:04 +04:00
|
|
|
exit true
|
2007-02-24 10:47:53 +03:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
}
|
|
|
|
if tests and not ARGV.empty?
|
|
|
|
$stderr.puts "--tests and arguments are exclusive"
|
2008-05-11 17:54:04 +04:00
|
|
|
exit false
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
|
|
|
tests ||= ARGV
|
|
|
|
tests = Dir.glob("#{File.dirname($0)}/test_*.rb") if tests.empty?
|
|
|
|
pathes = tests.map {|path| File.expand_path(path) }
|
2007-06-24 10:52:59 +04:00
|
|
|
|
2007-07-06 09:50:42 +04:00
|
|
|
unless quiet
|
|
|
|
puts Time.now
|
2007-07-07 21:33:57 +04:00
|
|
|
patchlevel = defined?(RUBY_PATCHLEVEL) ? " patchlevel #{RUBY_PATCHLEVEL}" : ''
|
2007-07-06 09:50:42 +04:00
|
|
|
puts "Driver is ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}#{patchlevel}) [#{RUBY_PLATFORM}]"
|
2008-05-11 17:54:04 +04:00
|
|
|
puts "Target is #{`#{@ruby} -v`.chomp}"
|
2007-07-06 09:50:42 +04:00
|
|
|
puts
|
|
|
|
$stdout.flush
|
|
|
|
end
|
2007-06-24 10:52:59 +04:00
|
|
|
|
2007-02-24 10:47:53 +03:00
|
|
|
in_temporary_working_directory(dir) {
|
|
|
|
exec_test pathes
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def exec_test(pathes)
|
|
|
|
@count = 0
|
|
|
|
@error = 0
|
|
|
|
@errbuf = []
|
|
|
|
@location = nil
|
|
|
|
pathes.each do |path|
|
2007-07-06 13:23:53 +04:00
|
|
|
$stderr.print "\n#{File.basename(path)} "
|
2007-02-24 10:47:53 +03:00
|
|
|
load File.expand_path(path)
|
|
|
|
end
|
|
|
|
$stderr.puts
|
|
|
|
if @error == 0
|
|
|
|
$stderr.puts "PASS #{@count} tests"
|
2008-05-11 17:54:04 +04:00
|
|
|
exit true
|
2007-02-24 10:47:53 +03:00
|
|
|
else
|
|
|
|
@errbuf.each do |msg|
|
|
|
|
$stderr.puts msg
|
|
|
|
end
|
|
|
|
$stderr.puts "FAIL #{@error}/#{@count} tests failed"
|
2008-05-11 17:54:04 +04:00
|
|
|
exit false
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-17 20:02:30 +03:00
|
|
|
def assert_check(testsrc, message = '', opt = '')
|
2007-02-24 13:28:36 +03:00
|
|
|
$stderr.puts "\##{@count} #{@location}" if @verbose
|
2008-01-17 20:02:30 +03:00
|
|
|
result = get_result_string(testsrc, opt)
|
2007-02-24 10:47:53 +03:00
|
|
|
check_coredump
|
2007-08-14 16:48:12 +04:00
|
|
|
faildesc = yield(result)
|
|
|
|
if !faildesc
|
2007-02-24 10:47:53 +03:00
|
|
|
$stderr.print '.'
|
|
|
|
else
|
|
|
|
$stderr.print 'F'
|
2007-08-14 16:48:12 +04:00
|
|
|
error faildesc, message
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
|
|
|
rescue Exception => err
|
|
|
|
$stderr.print 'E'
|
2007-08-06 09:36:14 +04:00
|
|
|
error err.message, message
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
|
|
|
|
2007-08-14 16:48:12 +04:00
|
|
|
def assert_equal(expected, testsrc, message = '')
|
2007-08-14 20:06:44 +04:00
|
|
|
newtest
|
2007-08-14 16:48:12 +04:00
|
|
|
assert_check(testsrc, message) {|result|
|
|
|
|
if expected == result
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
desc = "#{result.inspect} (expected #{expected.inspect})"
|
|
|
|
pretty(testsrc, desc, result)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_match(expected_pattern, testsrc, message = '')
|
2007-08-14 20:06:44 +04:00
|
|
|
newtest
|
2007-08-14 16:48:12 +04:00
|
|
|
assert_check(testsrc, message) {|result|
|
|
|
|
if expected_pattern =~ result
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
desc = "#{expected_pattern.inspect} expected to be =~\n#{result.inspect}"
|
|
|
|
pretty(testsrc, desc, result)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2007-10-05 11:43:34 +04:00
|
|
|
def assert_not_match(unexpected_pattern, testsrc, message = '')
|
|
|
|
newtest
|
|
|
|
assert_check(testsrc, message) {|result|
|
|
|
|
if unexpected_pattern !~ result
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
desc = "#{unexpected_pattern.inspect} expected to be !~\n#{result.inspect}"
|
|
|
|
pretty(testsrc, desc, result)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-01-17 20:02:30 +03:00
|
|
|
def assert_valid_syntax(testsrc, message = '')
|
|
|
|
newtest
|
|
|
|
assert_check(testsrc, message, '-c') {|result|
|
|
|
|
result if /Syntax OK/ !~ result
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2007-09-29 07:32:57 +04:00
|
|
|
def assert_normal_exit(testsrc, message = '')
|
|
|
|
newtest
|
|
|
|
$stderr.puts "\##{@count} #{@location}" if @verbose
|
|
|
|
faildesc = nil
|
|
|
|
filename = make_srcfile(testsrc)
|
2008-06-11 07:16:28 +04:00
|
|
|
old_stderr = $stderr.dup
|
|
|
|
begin
|
|
|
|
$stderr.reopen("assert_normal_exit_stderr.log", "w")
|
|
|
|
`#{@ruby} -W0 #{filename}`
|
|
|
|
status = $?
|
|
|
|
ensure
|
|
|
|
$stderr.reopen(old_stderr)
|
|
|
|
old_stderr.close
|
|
|
|
end
|
|
|
|
if status.signaled?
|
|
|
|
signo = status.termsig
|
2007-09-29 07:32:57 +04:00
|
|
|
signame = Signal.list.invert[signo]
|
|
|
|
sigdesc = "signal #{signo}"
|
|
|
|
if signame
|
|
|
|
sigdesc = "SIG#{signame} (#{sigdesc})"
|
|
|
|
end
|
|
|
|
faildesc = pretty(testsrc, "killed by #{sigdesc}", nil)
|
2008-06-11 07:16:28 +04:00
|
|
|
stderr_log = File.read("assert_normal_exit_stderr.log")
|
|
|
|
if !stderr_log.empty?
|
|
|
|
faildesc << "\n" if /\n\z/ !~ faildesc
|
|
|
|
stderr_log << "\n" if /\n\z/ !~ stderr_log
|
|
|
|
stderr_log.gsub!(/^.*\n/) { '| ' + $& }
|
|
|
|
faildesc << stderr_log
|
|
|
|
end
|
2007-09-29 07:32:57 +04:00
|
|
|
end
|
|
|
|
if !faildesc
|
|
|
|
$stderr.print '.'
|
|
|
|
else
|
|
|
|
$stderr.print 'F'
|
|
|
|
error faildesc, message
|
|
|
|
end
|
|
|
|
rescue Exception => err
|
|
|
|
$stderr.print 'E'
|
|
|
|
error err.message, message
|
|
|
|
end
|
|
|
|
|
2007-09-27 02:40:44 +04:00
|
|
|
def assert_finish(timeout_seconds, testsrc, message = '')
|
|
|
|
newtest
|
|
|
|
$stderr.puts "\##{@count} #{@location}" if @verbose
|
|
|
|
faildesc = nil
|
|
|
|
filename = make_srcfile(testsrc)
|
|
|
|
io = IO.popen("#{@ruby} -W0 #{filename}")
|
|
|
|
pid = io.pid
|
|
|
|
waited = false
|
|
|
|
tlimit = Time.now + timeout_seconds
|
|
|
|
while Time.now < tlimit
|
|
|
|
if Process.waitpid pid, Process::WNOHANG
|
|
|
|
waited = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
if !waited
|
|
|
|
Process.kill(:KILL, pid)
|
|
|
|
Process.waitpid pid
|
|
|
|
faildesc = pretty(testsrc, "not finished in #{timeout_seconds} seconds", nil)
|
|
|
|
end
|
|
|
|
io.close
|
|
|
|
if !faildesc
|
|
|
|
$stderr.print '.'
|
|
|
|
else
|
|
|
|
$stderr.print 'F'
|
|
|
|
error faildesc, message
|
|
|
|
end
|
|
|
|
rescue Exception => err
|
|
|
|
$stderr.print 'E'
|
|
|
|
error err.message, message
|
|
|
|
end
|
|
|
|
|
2008-01-14 12:45:46 +03:00
|
|
|
def flunk(message = '')
|
|
|
|
newtest
|
|
|
|
$stderr.print 'F'
|
|
|
|
error message, ''
|
|
|
|
end
|
|
|
|
|
2007-08-14 16:48:12 +04:00
|
|
|
def pretty(src, desc, result)
|
2007-09-30 06:52:55 +04:00
|
|
|
src = src.sub(/\A.*\n/, '')
|
2007-08-14 16:48:12 +04:00
|
|
|
(/\n/ =~ src ? "\n#{adjust_indent(src)}" : src) + " #=> #{desc}"
|
2007-02-25 05:10:38 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
INDENT = 27
|
|
|
|
|
|
|
|
def adjust_indent(src)
|
|
|
|
untabify(src).gsub(/^ {#{INDENT}}/o, '').gsub(/^/, ' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def untabify(str)
|
2008-02-12 09:28:23 +03:00
|
|
|
str.gsub(/^\t+/) {' ' * (8 * $&.size) }
|
2007-02-25 05:10:38 +03:00
|
|
|
end
|
|
|
|
|
2007-09-27 02:40:44 +04:00
|
|
|
def make_srcfile(src)
|
|
|
|
filename = 'bootstraptest.tmp.rb'
|
|
|
|
File.open(filename, 'w') {|f|
|
|
|
|
f.puts "GC.stress = true" if $stress
|
|
|
|
f.puts "print(begin; #{src}; end)"
|
|
|
|
}
|
|
|
|
filename
|
|
|
|
end
|
|
|
|
|
2008-01-17 20:02:30 +03:00
|
|
|
def get_result_string(src, opt = '')
|
2007-02-24 10:47:53 +03:00
|
|
|
if @ruby
|
2007-09-27 02:40:44 +04:00
|
|
|
filename = make_srcfile(src)
|
2007-07-20 04:50:42 +04:00
|
|
|
begin
|
2008-01-17 20:02:30 +03:00
|
|
|
`#{@ruby} -W0 #{opt} #{filename}`
|
2007-07-20 04:50:42 +04:00
|
|
|
ensure
|
|
|
|
raise CoreDumpError, "core dumped" if $? and $?.coredump?
|
|
|
|
end
|
2007-02-24 10:47:53 +03:00
|
|
|
else
|
|
|
|
eval(src).to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def newtest
|
|
|
|
@location = File.basename(caller(2).first)
|
|
|
|
@count += 1
|
|
|
|
cleanup_coredump
|
|
|
|
end
|
|
|
|
|
2007-08-06 09:36:14 +04:00
|
|
|
def error(msg, additional_message)
|
|
|
|
@errbuf.push "\##{@count} #{@location}: #{msg} #{additional_message}"
|
2007-02-24 10:47:53 +03:00
|
|
|
@error += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_temporary_working_directory(dir)
|
2007-08-17 09:41:10 +04:00
|
|
|
if dir
|
|
|
|
Dir.mkdir dir
|
|
|
|
Dir.chdir(dir) {
|
|
|
|
yield
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Dir.mktmpdir("bootstraptest.tmpwd") {|d|
|
|
|
|
Dir.chdir(d) {
|
|
|
|
yield
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup_coredump
|
|
|
|
FileUtils.rm_f 'core'
|
2007-02-25 04:28:14 +03:00
|
|
|
FileUtils.rm_f Dir.glob('core.*')
|
2007-07-20 04:50:42 +04:00
|
|
|
FileUtils.rm_f @ruby+'.stackdump' if @ruby
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
class CoreDumpError < StandardError; end
|
|
|
|
|
|
|
|
def check_coredump
|
2007-07-20 04:50:42 +04:00
|
|
|
if File.file?('core') or not Dir.glob('core.*').empty? or
|
|
|
|
(@ruby and File.exist?(@ruby+'.stackdump'))
|
2007-02-24 10:47:53 +03:00
|
|
|
raise CoreDumpError, "core dumped"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
main
|