diff --git a/tests/lib/core_assertions.rb b/tests/lib/core_assertions.rb
index abd0e450..358e7d95 100644
--- a/tests/lib/core_assertions.rb
+++ b/tests/lib/core_assertions.rb
@@ -1,8 +1,49 @@
# frozen_string_literal: true
module Test
+
+ class << self
+ ##
+ # Filter object for backtraces.
+
+ attr_accessor :backtrace_filter
+ end
+
+ class BacktraceFilter # :nodoc:
+ def filter bt
+ return ["No backtrace"] unless bt
+
+ new_bt = []
+ pattern = %r[/(?:lib\/test/|core_assertions\.rb:)]
+
+ unless $DEBUG then
+ bt.each do |line|
+ break if pattern.match?(line)
+ new_bt << line
+ end
+
+ new_bt = bt.reject { |line| pattern.match?(line) } if new_bt.empty?
+ new_bt = bt.dup if new_bt.empty?
+ else
+ new_bt = bt.dup
+ end
+
+ new_bt
+ end
+ end
+
+ self.backtrace_filter = BacktraceFilter.new
+
+ def self.filter_backtrace bt # :nodoc:
+ backtrace_filter.filter bt
+ end
+
module Unit
module Assertions
+ def assert_raises(*exp, &b)
+ raise NoMethodError, "use assert_raise", caller
+ end
+
def _assertions= n # :nodoc:
@_assertions = n
end
@@ -16,31 +57,24 @@ def _assertions # :nodoc:
def message msg = nil, ending = nil, &default
proc {
- msg = msg.call.chomp(".") if Proc === msg
- custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty?
- "#{custom_message}#{default.call}#{ending || "."}"
+ ending ||= (ending_pattern = /(? e
- bt = e.backtrace
- as = e.instance_of?(MiniTest::Assertion)
- if as
- ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o
- bt.reject! {|ln| ans =~ ln}
- end
- if ((args.empty? && !as) ||
- args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
- msg = message(msg) {
- "Exception raised:\n<#{mu_pp(e)}>\n" +
- "Backtrace:\n" +
- e.backtrace.map{|frame| " #{frame}"}.join("\n")
- }
- raise MiniTest::Assertion, msg.call, bt
- else
- raise
- end
+ rescue *(args.empty? ? Exception : args) => e
+ msg = message(msg) {
+ "Exception raised:\n<#{mu_pp(e)}>\n""Backtrace:\n" <<
+ Test.filter_backtrace(e.backtrace).map{|frame| " #{frame}"}.join("\n")
+ }
+ raise Test::Unit::AssertionFailedError, msg.call, e.backtrace
end
end
@@ -259,12 +281,17 @@ def assert_ruby_status(args, test_stdin="", message=nil, **opt)
ABORT_SIGNALS = Signal.list.values_at(*%w"ILL ABRT BUS SEGV TERM")
- def separated_runner(out = nil)
+ def separated_runner(token, out = nil)
+ include(*Test::Unit::TestCase.ancestors.select {|c| !c.is_a?(Class) })
out = out ? IO.new(out, 'w') : STDOUT
at_exit {
- out.puts [Marshal.dump($!)].pack('m'), "assertions=\#{self._assertions}"
+ out.puts "#{token}", [Marshal.dump($!)].pack('m'), "#{token}", "#{token}assertions=#{self._assertions}"
}
- Test::Unit::Runner.class_variable_set(:@@stop_auto_run, true) if defined?(Test::Unit::Runner)
+ if defined?(Test::Unit::Runner)
+ Test::Unit::Runner.class_variable_set(:@@stop_auto_run, true)
+ elsif defined?(Test::Unit::AutoRunner)
+ Test::Unit::AutoRunner.need_auto_run = false
+ end
end
def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **opt)
@@ -274,22 +301,24 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
line ||= loc.lineno
end
capture_stdout = true
- unless /mswin|mingw/ =~ RUBY_PLATFORM
+ unless /mswin|mingw/ =~ RbConfig::CONFIG['host_os']
capture_stdout = false
- opt[:out] = MiniTest::Unit.output if defined?(MiniTest::Unit)
+ opt[:out] = Test::Unit::Runner.output if defined?(Test::Unit::Runner)
res_p, res_c = IO.pipe
- opt[res_c.fileno] = res_c.fileno
+ opt[:ios] = [res_c]
end
+ token_dump, token_re = new_test_token
src = <\n\K.*\n(?=#{token_re}<\/error>$)/m].unpack1("m"))
rescue => marshal_error
ignore_stderr = nil
res = nil
@@ -401,8 +430,8 @@ def assert_raise(*exp, &b)
begin
yield
- rescue MiniTest::Skip => e
- return e if exp.include? MiniTest::Skip
+ rescue Test::Unit::PendedError => e
+ return e if exp.include? Test::Unit::PendedError
raise e
rescue Exception => e
expected = exp.any? { |ex|
@@ -477,7 +506,7 @@ def assert_raise_with_message(exception, expected, msg = nil, &block)
ex
end
- MINI_DIR = File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), "minitest") #:nodoc:
+ TEST_DIR = File.join(__dir__, "test/unit") #:nodoc:
# :call-seq:
# assert(test, [failure_message])
@@ -497,7 +526,7 @@ def assert(test, *msgs)
when nil
msgs.shift
else
- bt = caller.reject { |s| s.start_with?(MINI_DIR) }
+ bt = caller.reject { |s| s.start_with?(TEST_DIR) }
raise ArgumentError, "assertion message must be String or Proc, but #{msg.class} was given.", bt
end unless msgs.empty?
super
@@ -520,7 +549,7 @@ def assert_respond_to(obj, (meth, *priv), msg = nil)
return assert obj.respond_to?(meth, *priv), msg
end
#get rid of overcounting
- if caller_locations(1, 1)[0].path.start_with?(MINI_DIR)
+ if caller_locations(1, 1)[0].path.start_with?(TEST_DIR)
return if obj.respond_to?(meth)
end
super(obj, meth, msg)
@@ -543,17 +572,17 @@ def assert_not_respond_to(obj, (meth, *priv), msg = nil)
return assert !obj.respond_to?(meth, *priv), msg
end
#get rid of overcounting
- if caller_locations(1, 1)[0].path.start_with?(MINI_DIR)
+ if caller_locations(1, 1)[0].path.start_with?(TEST_DIR)
return unless obj.respond_to?(meth)
end
refute_respond_to(obj, meth, msg)
end
- # pattern_list is an array which contains regexp and :*.
+ # pattern_list is an array which contains regexp, string and :*.
# :* means any sequence.
#
# pattern_list is anchored.
- # Use [:*, regexp, :*] for non-anchored match.
+ # Use [:*, regexp/string, :*] for non-anchored match.
def assert_pattern_list(pattern_list, actual, message=nil)
rest = actual
anchored = true
@@ -562,11 +591,13 @@ def assert_pattern_list(pattern_list, actual, message=nil)
anchored = false
else
if anchored
- match = /\A#{pattern}/.match(rest)
+ match = rest.rindex(pattern, 0)
else
- match = pattern.match(rest)
+ match = rest.index(pattern)
end
- unless match
+ if match
+ post_match = $~ ? $~.post_match : rest[match+pattern.size..-1]
+ else
msg = message(msg) {
expect_msg = "Expected #{mu_pp pattern}\n"
if /\n[^\n]/ =~ rest
@@ -583,7 +614,7 @@ def assert_pattern_list(pattern_list, actual, message=nil)
}
assert false, msg
end
- rest = match.post_match
+ rest = post_match
anchored = true
end
}
@@ -610,19 +641,20 @@ def assert_warn(*args)
def assert_deprecated_warning(mesg = /deprecated/)
assert_warning(mesg) do
- Warning[:deprecated] = true
+ Warning[:deprecated] = true if Warning.respond_to?(:[]=)
yield
end
end
def assert_deprecated_warn(mesg = /deprecated/)
assert_warn(mesg) do
- Warning[:deprecated] = true
+ Warning[:deprecated] = true if Warning.respond_to?(:[]=)
yield
end
end
class << (AssertFile = Struct.new(:failure_message).new)
+ include Assertions
include CoreAssertions
def assert_file_predicate(predicate, *args)
if /\Anot_/ =~ predicate
@@ -654,7 +686,7 @@ def initialize
def for(key)
@count += 1
- yield
+ yield key
rescue Exception => e
@failures[key] = [@count, e]
end
@@ -708,15 +740,25 @@ def assert_join_threads(threads, message = nil)
msg = "exceptions on #{errs.length} threads:\n" +
errs.map {|t, err|
"#{t.inspect}:\n" +
- RUBY_VERSION >= "2.5.0" ? err.full_message(highlight: false, order: :top) : err.message
+ (err.respond_to?(:full_message) ? err.full_message(highlight: false, order: :top) : err.message)
}.join("\n---\n")
if message
msg = "#{message}\n#{msg}"
end
- raise MiniTest::Assertion, msg
+ raise Test::Unit::AssertionFailedError, msg
end
end
+ def assert_all?(obj, m = nil, &blk)
+ failed = []
+ obj.each do |*a, &b|
+ unless blk.call(*a, &b)
+ failed << (a.size > 1 ? a : a[0])
+ end
+ end
+ assert(failed.empty?, message(m) {failed.pretty_inspect})
+ end
+
def assert_all_assertions(msg = nil)
all = AllFailures.new
yield all
@@ -725,21 +767,67 @@ def assert_all_assertions(msg = nil)
end
alias all_assertions assert_all_assertions
- def message(msg = nil, *args, &default) # :nodoc:
- if Proc === msg
- super(nil, *args) do
- ary = [msg.call, (default.call if default)].compact.reject(&:empty?)
- if 1 < ary.length
- ary[0...-1] = ary[0...-1].map {|str| str.sub(/(?(n) {n})
+ pend "No PERFORMANCE_CLOCK found" unless defined?(PERFORMANCE_CLOCK)
+
+ # Timeout testing generally doesn't work when RJIT compilation happens.
+ rjit_enabled = defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
+ measure = proc do |arg, message|
+ st = Process.clock_gettime(PERFORMANCE_CLOCK)
+ yield(*arg)
+ t = (Process.clock_gettime(PERFORMANCE_CLOCK) - st)
+ assert_operator 0, :<=, t, message unless rjit_enabled
+ t
+ end
+
+ first = seq.first
+ *arg = pre.call(first)
+ times = (0..(rehearsal || (2 * first))).map do
+ measure[arg, "rehearsal"].nonzero?
+ end
+ times.compact!
+ tmin, tmax = times.minmax
+ tbase = 10 ** Math.log10(tmax * ([(tmax / tmin), 2].max ** 2)).ceil
+ info = "(tmin: #{tmin}, tmax: #{tmax}, tbase: #{tbase})"
+
+ seq.each do |i|
+ next if i == first
+ t = tbase * i.fdiv(first)
+ *arg = pre.call(i)
+ message = "[#{i}]: in #{t}s #{info}"
+ Timeout.timeout(t, Timeout::Error, message) do
+ measure[arg, message]
+ end
end
end
@@ -758,6 +846,11 @@ def diff(exp, act)
end
q.output
end
+
+ def new_test_token
+ token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m"
+ return token.dump, Regexp.quote(token)
+ end
end
end
end
diff --git a/tests/lib/envutil.rb b/tests/lib/envutil.rb
index 937e1128..1ca76d17 100644
--- a/tests/lib/envutil.rb
+++ b/tests/lib/envutil.rb
@@ -15,23 +15,22 @@
module EnvUtil
def rubybin
if ruby = ENV["RUBY"]
- return ruby
- end
- ruby = "ruby"
- exeext = RbConfig::CONFIG["EXEEXT"]
- rubyexe = (ruby + exeext if exeext and !exeext.empty?)
- 3.times do
- if File.exist? ruby and File.executable? ruby and !File.directory? ruby
- return File.expand_path(ruby)
- end
- if rubyexe and File.exist? rubyexe and File.executable? rubyexe
- return File.expand_path(rubyexe)
- end
- ruby = File.join("..", ruby)
- end
- if defined?(RbConfig.ruby)
+ ruby
+ elsif defined?(RbConfig.ruby)
RbConfig.ruby
else
+ ruby = "ruby"
+ exeext = RbConfig::CONFIG["EXEEXT"]
+ rubyexe = (ruby + exeext if exeext and !exeext.empty?)
+ 3.times do
+ if File.exist? ruby and File.executable? ruby and !File.directory? ruby
+ return File.expand_path(ruby)
+ end
+ if rubyexe and File.exist? rubyexe and File.executable? rubyexe
+ return File.expand_path(rubyexe)
+ end
+ ruby = File.join("..", ruby)
+ end
"ruby"
end
end
@@ -125,7 +124,7 @@ def terminate(pid, signal = :TERM, pgroup = nil, reprieve = 1)
def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr = false,
encoding: nil, timeout: 10, reprieve: 1, timeout_error: Timeout::Error,
- stdout_filter: nil, stderr_filter: nil,
+ stdout_filter: nil, stderr_filter: nil, ios: nil,
signal: :TERM,
rubybin: EnvUtil.rubybin, precommand: nil,
**opt)
@@ -141,6 +140,8 @@ def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr =
out_p.set_encoding(encoding) if out_p
err_p.set_encoding(encoding) if err_p
end
+ ios.each {|i, o = i|opt[i] = o} if ios
+
c = "C"
child_env = {}
LANG_ENVS.each {|lc| child_env[lc] = c}
@@ -150,9 +151,14 @@ def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr =
if RUBYLIB and lib = child_env["RUBYLIB"]
child_env["RUBYLIB"] = [lib, RUBYLIB].join(File::PATH_SEPARATOR)
end
- child_env['ASAN_OPTIONS'] = ENV['ASAN_OPTIONS'] if ENV['ASAN_OPTIONS']
+
+ # remain env
+ %w(ASAN_OPTIONS RUBY_ON_BUG).each{|name|
+ child_env[name] = ENV[name] if !child_env.key?(name) and ENV.key?(name)
+ }
+
args = [args] if args.kind_of?(String)
- pid = spawn(child_env, *precommand, rubybin, *args, **opt)
+ pid = spawn(child_env, *precommand, rubybin, *args, opt)
in_c.close
out_c&.close
out_c = nil
@@ -290,16 +296,24 @@ def self.diagnostic_reports(signame, pid, now)
cmd = @ruby_install_name if "ruby-runner#{RbConfig::CONFIG["EXEEXT"]}" == cmd
path = DIAGNOSTIC_REPORTS_PATH
timeformat = DIAGNOSTIC_REPORTS_TIMEFORMAT
- pat = "#{path}/#{cmd}_#{now.strftime(timeformat)}[-_]*.crash"
+ pat = "#{path}/#{cmd}_#{now.strftime(timeformat)}[-_]*.{crash,ips}"
first = true
30.times do
first ? (first = false) : sleep(0.1)
Dir.glob(pat) do |name|
log = File.read(name) rescue next
- if /\AProcess:\s+#{cmd} \[#{pid}\]$/ =~ log
- File.unlink(name)
- File.unlink("#{path}/.#{File.basename(name)}.plist") rescue nil
- return log
+ case name
+ when /\.crash\z/
+ if /\AProcess:\s+#{cmd} \[#{pid}\]$/ =~ log
+ File.unlink(name)
+ File.unlink("#{path}/.#{File.basename(name)}.plist") rescue nil
+ return log
+ end
+ when /\.ips\z/
+ if /^ *"pid" *: *#{pid},/ =~ log
+ File.unlink(name)
+ return log
+ end
end
end
end