2018-12-27 09:12:09 +03:00
|
|
|
"exec" "${RUBY-ruby}" "-x" "$0" "$@" || true # -*- Ruby -*-
|
2009-08-03 11:20:14 +04:00
|
|
|
#!./ruby
|
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.
|
2018-07-30 11:05:22 +03:00
|
|
|
# Maintain Ruby 1.8 compatibility for now
|
2007-02-24 10:47:53 +03:00
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
$start_time = Time.now
|
|
|
|
|
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
|
2009-08-23 10:15:00 +04:00
|
|
|
def Dir.mktmpdir(prefix_suffix=nil, tmpdir=nil)
|
|
|
|
case prefix_suffix
|
|
|
|
when nil
|
|
|
|
prefix = "d"
|
|
|
|
suffix = ""
|
|
|
|
when String
|
|
|
|
prefix = prefix_suffix
|
|
|
|
suffix = ""
|
|
|
|
when Array
|
|
|
|
prefix = prefix_suffix[0]
|
|
|
|
suffix = prefix_suffix[1]
|
|
|
|
else
|
|
|
|
raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
|
|
|
|
end
|
2007-08-17 09:41:10 +04:00
|
|
|
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
|
2009-08-23 10:15:00 +04:00
|
|
|
path << suffix
|
2007-08-17 09:41:10 +04:00
|
|
|
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
|
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
# Configuration
|
|
|
|
BT = Struct.new(:ruby,
|
|
|
|
:verbose,
|
|
|
|
:color,
|
|
|
|
:tty,
|
|
|
|
:quiet,
|
|
|
|
:wn,
|
|
|
|
:progress,
|
|
|
|
:progress_bs,
|
|
|
|
:passed,
|
|
|
|
:failed,
|
|
|
|
:reset,
|
|
|
|
:columns,
|
|
|
|
:width,
|
2022-02-17 14:12:38 +03:00
|
|
|
:platform,
|
2022-02-04 21:10:15 +03:00
|
|
|
).new
|
|
|
|
|
|
|
|
BT_STATE = Struct.new(:count, :error).new
|
|
|
|
|
2007-02-24 10:47:53 +03:00
|
|
|
def main
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.ruby = File.expand_path('miniruby')
|
|
|
|
BT.verbose = false
|
2016-08-07 13:34:11 +03:00
|
|
|
$VERBOSE = false
|
2011-06-11 13:07:26 +04:00
|
|
|
$stress = false
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.color = nil
|
|
|
|
BT.tty = nil
|
|
|
|
BT.quiet = false
|
|
|
|
BT.wn = 1
|
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=(.*)/
|
2022-02-04 21:10:15 +03:00
|
|
|
ruby = $1
|
|
|
|
ruby.gsub!(/^([^ ]*)/){File.expand_path($1)}
|
|
|
|
ruby.gsub!(/(\s+-I\s*)((?!(?:\.\/)*-(?:\s|\z))\S+)/){$1+File.expand_path($2)}
|
|
|
|
ruby.gsub!(/(\s+-r\s*)(\.\.?\/\S+)/){$1+File.expand_path($2)}
|
|
|
|
BT.ruby = ruby
|
2007-02-24 10:47:53 +03:00
|
|
|
true
|
|
|
|
when /\A--sets=(.*)/
|
2010-02-07 17:09:43 +03:00
|
|
|
tests = Dir.glob("#{File.dirname($0)}/test_{#{$1}}*.rb").sort
|
2007-02-24 10:47:53 +03:00
|
|
|
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
|
2012-06-15 04:56:36 +04:00
|
|
|
when /\A--color(?:=(?:always|(auto)|(never)|(.*)))?\z/
|
|
|
|
warn "unknown --color argument: #$3" if $3
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.color = color = $1 ? nil : !$2
|
2012-05-30 05:24:08 +04:00
|
|
|
true
|
2013-01-15 04:59:19 +04:00
|
|
|
when /\A--tty(=(?:yes|(no)|(.*)))?\z/
|
|
|
|
warn "unknown --tty argument: #$3" if $3
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.tty = !$1 || !$2
|
2013-01-15 04:59:19 +04:00
|
|
|
true
|
2007-07-06 09:50:42 +04:00
|
|
|
when /\A(-q|--q(uiet))\z/
|
|
|
|
quiet = true
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.quiet = true
|
|
|
|
true
|
|
|
|
when /\A-j(\d+)?/
|
|
|
|
wn = $1.to_i
|
|
|
|
if wn <= 0
|
|
|
|
require 'etc'
|
|
|
|
wn = [Etc.nprocessors / 2, 1].max
|
|
|
|
end
|
|
|
|
BT.wn = wn
|
2007-07-06 09:50:42 +04:00
|
|
|
true
|
2007-02-24 13:28:36 +03:00
|
|
|
when /\A(-v|--v(erbose))\z/
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.verbose = true
|
2022-02-17 12:57:33 +03:00
|
|
|
BT.quiet = false
|
|
|
|
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.
|
2009-08-23 10:24:33 +04:00
|
|
|
default: /tmp/bootstraptestXXXXX.tmpwd
|
2012-06-15 04:56:33 +04:00
|
|
|
--color[=WHEN] Colorize the output. WHEN defaults to 'always'
|
|
|
|
or can be 'never' or 'auto'.
|
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
|
2012-06-16 02:12:30 +04:00
|
|
|
when /\A-j/
|
|
|
|
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
|
2010-02-07 17:09:43 +03:00
|
|
|
tests = Dir.glob("#{File.dirname($0)}/test_*.rb").sort if tests.empty?
|
2007-02-24 10:47:53 +03:00
|
|
|
pathes = tests.map {|path| File.expand_path(path) }
|
2007-06-24 10:52:59 +04:00
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.progress = %w[- \\ | /]
|
|
|
|
BT.progress_bs = "\b" * BT.progress[0].size
|
|
|
|
BT.tty = $stderr.tty? if BT.tty.nil?
|
|
|
|
|
|
|
|
case BT.color
|
2012-05-30 05:24:08 +04:00
|
|
|
when nil
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.color = BT.tty && /dumb/ !~ ENV["TERM"]
|
2012-05-30 05:24:08 +04:00
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.tty &&= !BT.verbose
|
|
|
|
if BT.color
|
2012-06-15 04:56:39 +04:00
|
|
|
# dircolors-like style
|
2015-05-17 03:50:11 +03:00
|
|
|
colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:\n]*)/)] : {}
|
|
|
|
begin
|
2019-07-02 03:19:38 +03:00
|
|
|
File.read(File.join(__dir__, "../tool/colors")).scan(/(\w+)=([^:\n]*)/) do |n, c|
|
2015-05-17 03:50:11 +03:00
|
|
|
colors[n] ||= c
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.passed = "\e[;#{colors["pass"] || "32"}m"
|
|
|
|
BT.failed = "\e[;#{colors["fail"] || "31"}m"
|
|
|
|
BT.reset = "\e[m"
|
2012-05-07 22:53:42 +04:00
|
|
|
else
|
2022-02-04 21:10:15 +03:00
|
|
|
BT.passed = BT.failed = BT.reset = ""
|
2012-05-07 22:53:42 +04:00
|
|
|
end
|
2022-02-17 14:12:38 +03:00
|
|
|
target_version = `#{BT.ruby} -v`.chomp
|
|
|
|
BT.platform = target_version[/\[(.*)\]\z/, 1]
|
2007-07-06 09:50:42 +04:00
|
|
|
unless quiet
|
2022-02-04 21:10:15 +03:00
|
|
|
puts $start_time
|
2009-02-18 09:27:47 +03:00
|
|
|
if defined?(RUBY_DESCRIPTION)
|
|
|
|
puts "Driver is #{RUBY_DESCRIPTION}"
|
|
|
|
elsif defined?(RUBY_PATCHLEVEL)
|
|
|
|
puts "Driver is ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}#{RUBY_PLATFORM}) [#{RUBY_PLATFORM}]"
|
|
|
|
else
|
|
|
|
puts "Driver is ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
|
|
|
|
end
|
2022-02-17 14:12:38 +03:00
|
|
|
puts "Target is #{target_version}"
|
2007-07-06 09:50:42 +04:00
|
|
|
puts
|
|
|
|
$stdout.flush
|
|
|
|
end
|
2007-06-24 10:52:59 +04:00
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
in_temporary_working_directory(dir) do
|
2007-02-24 10:47:53 +03:00
|
|
|
exec_test pathes
|
2022-02-04 21:10:15 +03:00
|
|
|
end
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
|
|
|
|
2014-06-10 18:11:57 +04:00
|
|
|
def erase(e = true)
|
2022-02-04 21:10:15 +03:00
|
|
|
if e and BT.columns > 0 and BT.tty and !BT.verbose
|
2019-07-29 17:04:04 +03:00
|
|
|
"\e[1K\r"
|
2014-06-10 18:11:57 +04:00
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
def load_test pathes
|
2007-02-24 10:47:53 +03:00
|
|
|
pathes.each do |path|
|
|
|
|
load File.expand_path(path)
|
2022-02-04 21:10:15 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def concurrent_exec_test
|
|
|
|
aq = Queue.new
|
|
|
|
rq = Queue.new
|
|
|
|
|
|
|
|
ts = BT.wn.times.map do
|
|
|
|
Thread.new do
|
|
|
|
while as = aq.pop
|
|
|
|
as.call
|
|
|
|
rq << as
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
rq << nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Assertion.all.to_a.shuffle.each do |path, assertions|
|
|
|
|
assertions.each do |as|
|
|
|
|
aq << as
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
$stderr.print ' ' unless BT.quiet
|
|
|
|
aq.close
|
|
|
|
i = 1
|
|
|
|
term_wn = 0
|
|
|
|
begin
|
|
|
|
while BT.wn != term_wn
|
|
|
|
if r = rq.pop
|
|
|
|
case
|
|
|
|
when BT.quiet
|
|
|
|
when BT.tty
|
|
|
|
$stderr.print "#{BT.progress_bs}#{BT.progress[(i+=1) % BT.progress.size]}"
|
|
|
|
else
|
|
|
|
$stderr.print '.'
|
|
|
|
end
|
2012-05-07 22:53:42 +04:00
|
|
|
else
|
2022-02-04 21:10:15 +03:00
|
|
|
term_wn += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ts.each(&:kill)
|
|
|
|
ts.each(&:join)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def exec_test(pathes)
|
|
|
|
# setup
|
|
|
|
load_test pathes
|
|
|
|
BT_STATE.count = 0
|
|
|
|
BT_STATE.error = 0
|
|
|
|
BT.columns = 0
|
|
|
|
BT.width = pathes.map {|path| File.basename(path).size}.max + 2
|
|
|
|
|
|
|
|
# execute tests
|
|
|
|
if BT.wn > 1
|
|
|
|
concurrent_exec_test if BT.wn > 1
|
|
|
|
else
|
|
|
|
prev_basename = nil
|
|
|
|
Assertion.all.each do |basename, assertions|
|
|
|
|
if !BT.quiet && basename != prev_basename
|
|
|
|
prev_basename = basename
|
|
|
|
$stderr.printf("%s%-*s ", erase(BT.quiet), BT.width, basename)
|
|
|
|
$stderr.flush
|
|
|
|
end
|
|
|
|
BT.columns = BT.width + 1
|
|
|
|
$stderr.puts if BT.verbose
|
|
|
|
count = BT_STATE.count
|
|
|
|
error = BT_STATE.error
|
|
|
|
|
|
|
|
assertions.each do |assertion|
|
|
|
|
BT_STATE.count += 1
|
|
|
|
assertion.call
|
|
|
|
end
|
|
|
|
|
|
|
|
if BT.tty
|
|
|
|
if BT_STATE.error == error
|
|
|
|
msg = "PASS #{BT_STATE.count-count}"
|
|
|
|
BT.columns += msg.size - 1
|
|
|
|
$stderr.print "#{BT.progress_bs}#{BT.passed}#{msg}#{BT.reset}" unless BT.quiet
|
|
|
|
else
|
|
|
|
msg = "FAIL #{BT_STATE.error-error}/#{BT_STATE.count-count}"
|
|
|
|
$stderr.print "#{BT.progress_bs}#{BT.failed}#{msg}#{BT.reset}"
|
|
|
|
BT.columns = 0
|
|
|
|
end
|
2012-05-07 22:53:42 +04:00
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
$stderr.puts if !BT.quiet and (BT.tty or BT_STATE.error == error)
|
2012-05-07 22:53:42 +04:00
|
|
|
end
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
|
|
|
|
# show results
|
|
|
|
unless BT.quiet
|
|
|
|
$stderr.puts(erase)
|
|
|
|
|
|
|
|
sec = Time.now - $start_time
|
|
|
|
$stderr.puts "Finished in #{'%.2f' % sec} sec\n\n" if Assertion.count > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
Assertion.errbuf.each do |msg|
|
2019-06-19 08:53:52 +03:00
|
|
|
$stderr.puts msg
|
|
|
|
end
|
2022-01-25 19:51:16 +03:00
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
out = BT.quiet ? $stdout : $stderr
|
2022-01-25 19:51:16 +03:00
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
if BT_STATE.error == 0
|
|
|
|
if Assertion.count == 0
|
|
|
|
out.puts "No tests, no problem" unless BT.quiet
|
2009-02-04 15:57:17 +03:00
|
|
|
else
|
2022-02-04 21:10:15 +03:00
|
|
|
out.puts "#{BT.passed}PASS#{BT.reset} all #{Assertion.count} tests"
|
2009-02-04 15:57:17 +03:00
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
true
|
2007-02-24 10:47:53 +03:00
|
|
|
else
|
2022-02-04 21:10:15 +03:00
|
|
|
$stderr.puts "#{BT.failed}FAIL#{BT.reset} #{BT_STATE.error}/#{BT_STATE.count} tests failed"
|
|
|
|
false
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
def target_platform
|
2022-02-17 14:12:38 +03:00
|
|
|
BT.platform or RUBY_PLATFORM
|
2022-02-04 21:10:15 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
class Assertion < Struct.new(:src, :path, :lineno, :proc)
|
|
|
|
@count = 0
|
|
|
|
@all = Hash.new{|h, k| h[k] = []}
|
|
|
|
@errbuf = []
|
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_reader :count, :errbuf
|
|
|
|
|
|
|
|
def all
|
|
|
|
@all
|
2012-07-05 10:15:00 +04:00
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
|
|
|
|
def add as
|
|
|
|
@all[as.path] << as
|
|
|
|
as.id = (@count += 1)
|
2012-07-05 10:15:00 +04:00
|
|
|
end
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
|
|
|
|
attr_accessor :id
|
|
|
|
attr_reader :err, :category
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
self.class.add self
|
|
|
|
@category = self.path.match(/test_(.+)\.rb/)[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
self.proc.call self
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_check(message = '', opt = '', **argh)
|
|
|
|
show_progress(message) {
|
|
|
|
result = get_result_string(opt, **argh)
|
|
|
|
yield(result)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_stderr
|
|
|
|
out = err = nil
|
|
|
|
r, w = IO.pipe
|
|
|
|
@err = w
|
|
|
|
err_reader = Thread.new{ r.read }
|
|
|
|
|
|
|
|
begin
|
|
|
|
out = yield
|
|
|
|
ensure
|
|
|
|
w.close
|
|
|
|
err = err_reader.value
|
|
|
|
r.close rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return out, err
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_error(msg, additional_message)
|
|
|
|
msg = "#{BT.failed}\##{self.id} #{self.path}:#{self.lineno}#{BT.reset}: #{msg} #{additional_message}"
|
|
|
|
if BT.tty
|
|
|
|
$stderr.puts "#{erase}#{msg}"
|
|
|
|
else
|
|
|
|
Assertion.errbuf << msg
|
|
|
|
end
|
|
|
|
BT_STATE.error += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def show_progress(message = '')
|
|
|
|
if BT.quiet || BT.wn > 1
|
|
|
|
# do nothing
|
|
|
|
elsif BT.verbose
|
|
|
|
$stderr.print "\##{@id} #{self.path}:#{self.lineno} "
|
|
|
|
elsif BT.tty
|
|
|
|
$stderr.print "#{BT.progress_bs}#{BT.progress[BT_STATE.count % BT.progress.size]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
t = Time.now if BT.verbose
|
|
|
|
faildesc, errout = with_stderr {yield}
|
|
|
|
t = Time.now - t if BT.verbose
|
|
|
|
|
|
|
|
if !faildesc
|
|
|
|
# success
|
|
|
|
if BT.quiet || BT.wn > 1
|
|
|
|
# do nothing
|
|
|
|
elsif BT.tty
|
|
|
|
$stderr.print "#{BT.progress_bs}#{BT.progress[BT_STATE.count % BT.progress.size]}"
|
|
|
|
elsif BT.verbose
|
|
|
|
$stderr.printf(". %.3f\n", t)
|
|
|
|
else
|
|
|
|
$stderr.print '.'
|
|
|
|
end
|
|
|
|
else
|
|
|
|
$stderr.print "#{BT.failed}F"
|
|
|
|
$stderr.printf(" %.3f", t) if BT.verbose
|
|
|
|
$stderr.print BT.reset
|
|
|
|
$stderr.puts if BT.verbose
|
|
|
|
show_error faildesc, message
|
|
|
|
unless errout.empty?
|
|
|
|
$stderr.print "#{BT.failed}stderr output is not empty#{BT.reset}\n", adjust_indent(errout)
|
|
|
|
end
|
|
|
|
|
|
|
|
if BT.tty and !BT.verbose and BT.wn == 1
|
|
|
|
$stderr.printf("%-*s%s", BT.width, path, BT.progress[BT_STATE.count % BT.progress.size])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Interrupt
|
|
|
|
$stderr.puts "\##{@id} #{path}:#{lineno}"
|
|
|
|
raise
|
|
|
|
rescue Exception => err
|
2020-07-29 12:45:39 +03:00
|
|
|
$stderr.print 'E'
|
2022-02-04 21:10:15 +03:00
|
|
|
$stderr.puts if BT.verbose
|
|
|
|
show_error err.message, message
|
|
|
|
ensure
|
|
|
|
begin
|
|
|
|
check_coredump
|
|
|
|
rescue CoreDumpError => err
|
|
|
|
$stderr.print 'E'
|
|
|
|
$stderr.puts if BT.verbose
|
|
|
|
show_error err.message, message
|
|
|
|
cleanup_coredump
|
|
|
|
end
|
2020-07-29 12:45:39 +03:00
|
|
|
end
|
2007-02-24 10:47:53 +03:00
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
def get_result_string(opt = '', **argh)
|
|
|
|
if BT.ruby
|
|
|
|
filename = make_srcfile(**argh)
|
|
|
|
begin
|
|
|
|
kw = self.err ? {err: self.err} : {}
|
|
|
|
out = IO.popen("#{BT.ruby} -W0 #{opt} #{filename}", **kw)
|
|
|
|
pid = out.pid
|
|
|
|
out.read.tap{ Process.waitpid(pid); out.close }
|
|
|
|
ensure
|
|
|
|
raise Interrupt if $? and $?.signaled? && $?.termsig == Signal.list["INT"]
|
|
|
|
|
|
|
|
begin
|
|
|
|
Process.kill :KILL, pid
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
# OK
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
eval(src).to_s
|
|
|
|
end
|
2022-01-15 16:11:55 +03:00
|
|
|
end
|
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
def make_srcfile(frozen_string_literal: nil)
|
|
|
|
filename = "bootstraptest.#{self.path}_#{self.lineno}_#{self.id}.rb"
|
|
|
|
File.open(filename, 'w') {|f|
|
|
|
|
f.puts "#frozen_string_literal:true" if frozen_string_literal
|
|
|
|
f.puts "GC.stress = true" if $stress
|
|
|
|
f.puts "print(begin; #{self.src}; end)"
|
|
|
|
}
|
|
|
|
filename
|
2019-06-19 08:53:52 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
def add_assertion src, pr
|
|
|
|
loc = caller_locations(2, 1).first
|
|
|
|
lineno = loc.lineno
|
|
|
|
path = File.basename(loc.path)
|
|
|
|
|
|
|
|
Assertion.new(src, path, lineno, pr)
|
2011-06-14 18:07:02 +04:00
|
|
|
end
|
|
|
|
|
2017-01-23 05:47:16 +03:00
|
|
|
def assert_equal(expected, testsrc, message = '', opt = '', **argh)
|
2022-02-04 21:10:15 +03:00
|
|
|
add_assertion testsrc, -> as do
|
|
|
|
as.assert_check(message, opt, **argh) {|result|
|
|
|
|
if expected == result
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
desc = "#{result.inspect} (expected #{expected.inspect})"
|
|
|
|
pretty(testsrc, desc, result)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2007-08-14 16:48:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_match(expected_pattern, testsrc, message = '')
|
2022-02-04 21:10:15 +03:00
|
|
|
add_assertion testsrc, -> as do
|
|
|
|
as.assert_check(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-08-14 16:48:12 +04:00
|
|
|
end
|
|
|
|
|
2007-10-05 11:43:34 +04:00
|
|
|
def assert_not_match(unexpected_pattern, testsrc, message = '')
|
2022-02-04 21:10:15 +03:00
|
|
|
add_assertion testsrc, -> as do
|
|
|
|
as.assert_check(message) {|result|
|
|
|
|
if unexpected_pattern !~ result
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
desc = "#{unexpected_pattern.inspect} expected to be !~\n#{result.inspect}"
|
|
|
|
pretty(testsrc, desc, result)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2007-10-05 11:43:34 +04:00
|
|
|
end
|
|
|
|
|
2008-01-17 20:02:30 +03:00
|
|
|
def assert_valid_syntax(testsrc, message = '')
|
2022-02-04 21:10:15 +03:00
|
|
|
add_assertion testsrc, -> as do
|
|
|
|
as.assert_check(message, '-c') {|result|
|
|
|
|
result if /Syntax OK/ !~ result
|
|
|
|
}
|
|
|
|
end
|
2008-01-17 20:02:30 +03:00
|
|
|
end
|
|
|
|
|
2017-06-21 04:34:33 +03:00
|
|
|
def assert_normal_exit(testsrc, *rest, timeout: nil, **opt)
|
2022-02-04 21:10:15 +03:00
|
|
|
add_assertion testsrc, -> as do
|
|
|
|
message, ignore_signals = rest
|
|
|
|
message ||= ''
|
|
|
|
as.show_progress(message) {
|
|
|
|
faildesc = nil
|
|
|
|
filename = as.make_srcfile
|
|
|
|
timeout_signaled = false
|
|
|
|
logfile = "assert_normal_exit.#{as.path}.#{as.lineno}.log"
|
|
|
|
|
|
|
|
begin
|
|
|
|
err = open(logfile, "w")
|
|
|
|
io = IO.popen("#{BT.ruby} -W0 #{filename}", err: err)
|
|
|
|
pid = io.pid
|
|
|
|
th = Thread.new {
|
|
|
|
io.read
|
|
|
|
io.close
|
|
|
|
$?
|
|
|
|
}
|
|
|
|
if !th.join(timeout)
|
|
|
|
Process.kill :KILL, pid
|
|
|
|
timeout_signaled = true
|
2011-06-14 18:07:02 +04:00
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
status = th.value
|
|
|
|
ensure
|
|
|
|
err.close
|
|
|
|
end
|
|
|
|
if status && status.signaled?
|
|
|
|
signo = status.termsig
|
|
|
|
signame = Signal.list.invert[signo]
|
|
|
|
unless ignore_signals and ignore_signals.include?(signame)
|
|
|
|
sigdesc = "signal #{signo}"
|
|
|
|
if signame
|
|
|
|
sigdesc = "SIG#{signame} (#{sigdesc})"
|
|
|
|
end
|
|
|
|
if timeout_signaled
|
|
|
|
sigdesc << " (timeout)"
|
|
|
|
end
|
|
|
|
faildesc = pretty(testsrc, "killed by #{sigdesc}", nil)
|
|
|
|
stderr_log = File.read(logfile)
|
|
|
|
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
|
2011-06-14 18:07:02 +04:00
|
|
|
end
|
2008-07-11 15:51:39 +04:00
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
faildesc
|
|
|
|
}
|
|
|
|
end
|
2007-09-29 07:32:57 +04:00
|
|
|
end
|
|
|
|
|
2007-09-27 02:40:44 +04:00
|
|
|
def assert_finish(timeout_seconds, testsrc, message = '')
|
2022-02-04 21:10:15 +03:00
|
|
|
add_assertion testsrc, -> as do
|
|
|
|
if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? # for --jit-wait
|
|
|
|
timeout_seconds *= 3
|
|
|
|
end
|
|
|
|
|
|
|
|
as.show_progress(message) {
|
|
|
|
faildesc = nil
|
|
|
|
filename = as.make_srcfile
|
|
|
|
io = IO.popen("#{BT.ruby} -W0 #{filename}", err: as.err)
|
|
|
|
pid = io.pid
|
|
|
|
waited = false
|
|
|
|
tlimit = Time.now + timeout_seconds
|
|
|
|
diff = timeout_seconds
|
|
|
|
while diff > 0
|
|
|
|
if Process.waitpid pid, Process::WNOHANG
|
|
|
|
waited = true
|
|
|
|
break
|
2018-07-30 11:05:22 +03:00
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
if io.respond_to?(:read_nonblock)
|
|
|
|
if IO.select([io], nil, nil, diff)
|
|
|
|
begin
|
|
|
|
io.read_nonblock(1024)
|
|
|
|
rescue Errno::EAGAIN, IO::WaitReadable, EOFError
|
|
|
|
break
|
|
|
|
end while true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
diff = tlimit - Time.now
|
2018-07-30 11:05:22 +03:00
|
|
|
end
|
2022-02-04 21:10:15 +03:00
|
|
|
if !waited
|
|
|
|
Process.kill(:KILL, pid)
|
|
|
|
Process.waitpid pid
|
|
|
|
faildesc = pretty(testsrc, "not finished in #{timeout_seconds} seconds", nil)
|
|
|
|
end
|
|
|
|
io.close
|
|
|
|
faildesc
|
|
|
|
}
|
|
|
|
end
|
2007-09-27 02:40:44 +04:00
|
|
|
end
|
|
|
|
|
2008-01-14 12:45:46 +03:00
|
|
|
def flunk(message = '')
|
2022-02-04 21:10:15 +03:00
|
|
|
add_assertion '', -> as do
|
|
|
|
as.show_progress('') { message }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_limit(testsrc, opt = '', **argh)
|
|
|
|
return if BT.quiet
|
|
|
|
|
|
|
|
add_assertion testsrc, -> as do
|
|
|
|
result = as.get_result_string(opt, **argh)
|
|
|
|
Assertion.errbuf << result
|
|
|
|
end
|
2008-01-14 12:45:46 +03:00
|
|
|
end
|
|
|
|
|
2007-08-14 16:48:12 +04:00
|
|
|
def pretty(src, desc, result)
|
2016-10-11 19:36:14 +03:00
|
|
|
src = src.sub(/\A\s*\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)
|
2014-06-22 03:43:50 +04:00
|
|
|
untabify(src).gsub(/^ {#{INDENT}}/o, '').gsub(/^/, ' ').sub(/\s*\z/, "\n")
|
2007-02-25 05:10:38 +03:00
|
|
|
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-02-24 10:47:53 +03:00
|
|
|
def in_temporary_working_directory(dir)
|
2007-08-17 09:41:10 +04:00
|
|
|
if dir
|
|
|
|
Dir.mkdir dir
|
|
|
|
Dir.chdir(dir) {
|
|
|
|
yield
|
|
|
|
}
|
|
|
|
else
|
2009-08-23 10:15:00 +04:00
|
|
|
Dir.mktmpdir(["bootstraptest", ".tmpwd"]) {|d|
|
2007-08-17 09:41:10 +04:00
|
|
|
Dir.chdir(d) {
|
|
|
|
yield
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2007-02-24 10:47:53 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup_coredump
|
2020-12-11 08:36:42 +03:00
|
|
|
if File.file?('core')
|
|
|
|
require 'time'
|
|
|
|
Dir.glob('/tmp/bootstraptest-core.*').each do |f|
|
|
|
|
if Time.now - File.mtime(f) > 7 * 24 * 60 * 60 # 7 days
|
|
|
|
warn "Deleting an old core file: #{f}"
|
|
|
|
FileUtils.rm(f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
core_path = "/tmp/bootstraptest-core.#{Time.now.utc.iso8601}"
|
|
|
|
warn "A core file is found. Saving it at: #{core_path.dump}"
|
|
|
|
FileUtils.mv('core', core_path)
|
2022-02-04 21:10:15 +03:00
|
|
|
cmd = ['gdb', BT.ruby, '-c', core_path, '-ex', 'bt', '-batch']
|
2020-12-14 10:35:27 +03:00
|
|
|
p cmd # debugging why it's not working
|
|
|
|
system(*cmd)
|
2020-12-11 08:36:42 +03:00
|
|
|
end
|
2007-02-25 04:28:14 +03:00
|
|
|
FileUtils.rm_f Dir.glob('core.*')
|
2022-02-04 21:10:15 +03:00
|
|
|
FileUtils.rm_f BT.ruby+'.stackdump' if BT.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
|
2022-02-04 21:10:15 +03:00
|
|
|
(BT.ruby and File.exist?(BT.ruby+'.stackdump'))
|
2007-02-24 10:47:53 +03:00
|
|
|
raise CoreDumpError, "core dumped"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-04 21:10:15 +03:00
|
|
|
exit main
|