summaryrefslogtreecommitdiff
path: root/tool/lib/test/unit.rb
diff options
context:
space:
mode:
Diffstat (limited to 'tool/lib/test/unit.rb')
-rw-r--r--tool/lib/test/unit.rb961
1 files changed, 823 insertions, 138 deletions
diff --git a/tool/lib/test/unit.rb b/tool/lib/test/unit.rb
index f4ee1940f8..d758b5fb02 100644
--- a/tool/lib/test/unit.rb
+++ b/tool/lib/test/unit.rb
@@ -1,26 +1,117 @@
# frozen_string_literal: true
-begin
- gem 'minitest', '< 5.0.0' if defined? Gem
-rescue Gem::LoadError
+
+# Enable deprecation warnings for test-all, so deprecated methods/constants/functions are dealt with early.
+Warning[:deprecated] = true
+
+if ENV['BACKTRACE_FOR_DEPRECATION_WARNINGS']
+ Warning.extend Module.new {
+ def warn(message, category: nil, **kwargs)
+ if category == :deprecated and $stderr.respond_to?(:puts)
+ $stderr.puts nil, message, caller, nil
+ else
+ super
+ end
+ end
+ }
end
-require 'minitest/unit'
-require 'test/unit/assertions'
+
require_relative '../envutil'
require_relative '../colorize'
-require 'test/unit/testcase'
+require_relative '../leakchecker'
+require_relative '../test/unit/testcase'
require 'optparse'
# See Test::Unit
module Test
+
##
# Test::Unit is an implementation of the xUnit testing framework for Ruby.
- #
- # If you are writing new test code, please use MiniTest instead of Test::Unit.
- #
- # Test::Unit has been left in the standard library to support legacy test
- # suites.
module Unit
- TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest' # :nodoc:
+ ##
+ # Assertion base class
+
+ class AssertionFailedError < Exception; end
+
+ ##
+ # Assertion raised when skipping a test
+
+ class PendedError < AssertionFailedError; end
+
+ module Order
+ class NoSort
+ def initialize(seed)
+ end
+
+ def sort_by_name(list)
+ list
+ end
+
+ alias sort_by_string sort_by_name
+
+ def group(list)
+ list
+ end
+ end
+
+ class Alpha < NoSort
+ def sort_by_name(list)
+ list.sort_by(&:name)
+ end
+
+ def sort_by_string(list)
+ list.sort
+ end
+
+ end
+
+ # shuffle test suites based on CRC32 of their names
+ Shuffle = Struct.new(:seed, :salt) do
+ def initialize(seed)
+ self.class::CRC_TBL ||= (0..255).map {|i|
+ (0..7).inject(i) {|c,| (c & 1 == 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1) }
+ }.freeze
+
+ salt = [seed].pack("V").unpack1("H*")
+ super(seed, "\n#{salt}".freeze).freeze
+ end
+
+ def sort_by_name(list)
+ list.sort_by {|e| randomize_key(e.name)}
+ end
+
+ def sort_by_string(list)
+ list.sort_by {|e| randomize_key(e)}
+ end
+
+ def group(list)
+ list
+ end
+
+ private
+
+ def crc32(str, crc32 = 0xffffffff)
+ crc_tbl = self.class::CRC_TBL
+ str.each_byte do |data|
+ crc32 = crc_tbl[(crc32 ^ data) & 0xff] ^ (crc32 >> 8)
+ end
+ crc32
+ end
+
+ def randomize_key(name)
+ crc32(salt, crc32(name)) ^ 0xffffffff
+ end
+ end
+
+ Types = {
+ random: Shuffle,
+ alpha: Alpha,
+ sorted: Alpha,
+ nosort: NoSort,
+ }
+ Types.default_proc = proc {|_, order|
+ raise "Unknown test_order: #{order.inspect}"
+ }
+ end
module RunCount # :nodoc: all
@@run_count = 0
@@ -65,24 +156,27 @@ module Test
non_options(args, options)
@run_options = orig_args
+ order = options[:test_order]
if seed = options[:seed]
- srand(seed)
- else
- seed = options[:seed] = srand % 100_000
- srand(seed)
+ order ||= :random
+ elsif (order ||= :random) == :random
+ seed = options[:seed] = rand(0x10000)
orig_args.unshift "--seed=#{seed}"
end
+ Test::Unit::TestCase.test_order = order if order
+ order = Test::Unit::TestCase.test_order
+ @order = Test::Unit::Order::Types[order].new(seed)
@help = "\n" + orig_args.map { |s|
" " + (s =~ /[\s|&<>$()]/ ? s.inspect : s)
}.join("\n")
+
@options = options
end
private
def setup_options(opts, options)
- opts.separator 'minitest options:'
- opts.version = MiniTest::Unit::VERSION
+ opts.separator 'test-unit options:'
opts.on '-h', '--help', 'Display this help.' do
puts opts
@@ -102,8 +196,9 @@ module Test
(options[:filter] ||= []) << a
end
- opts.on '--test-order=random|alpha|sorted|nosort', [:random, :alpha, :sorted, :nosort] do |a|
- MiniTest::Unit::TestCase.test_order = a
+ orders = Test::Unit::Order::Types.keys
+ opts.on "--test-order=#{orders.join('|')}", orders do |a|
+ options[:test_order] = a
end
end
@@ -117,6 +212,9 @@ module Test
filter = nil
elsif negative.empty? and positive.size == 1 and pos_pat !~ positive[0]
filter = positive[0]
+ unless /\A[A-Z]\w*(?:::[A-Z]\w*)*#/ =~ filter
+ filter = /##{Regexp.quote(filter)}\z/
+ end
else
filter = Regexp.union(*positive.map! {|s| Regexp.new(s[pos_pat, 1] || "\\A#{Regexp.quote(s)}\\z")})
end
@@ -124,13 +222,6 @@ module Test
negative = Regexp.union(*negative.map! {|s| Regexp.new(s[neg_pat, 1])})
filter = /\A(?=.*#{filter})(?!.*#{negative})/
end
- if Regexp === filter
- filter = filter.dup
- # bypass conversion in minitest
- def filter.=~(other) # :nodoc:
- super unless Regexp === other
- end
- end
options[:filter] = filter
end
true
@@ -151,10 +242,16 @@ module Test
@jobserver = nil
makeflags = ENV.delete("MAKEFLAGS")
if !options[:parallel] and
- /(?:\A|\s)--jobserver-(?:auth|fds)=(\d+),(\d+)/ =~ makeflags
+ /(?:\A|\s)--jobserver-(?:auth|fds)=(?:(\d+),(\d+)|fifo:((?:\\.|\S)+))/ =~ makeflags
begin
- r = IO.for_fd($1.to_i(10), "rb", autoclose: false)
- w = IO.for_fd($2.to_i(10), "wb", autoclose: false)
+ if fifo = $3
+ fifo.gsub!(/\\(?=.)/, '')
+ r = File.open(fifo, IO::RDONLY|IO::NONBLOCK|IO::BINARY)
+ w = File.open(fifo, IO::WRONLY|IO::NONBLOCK|IO::BINARY)
+ else
+ r = IO.for_fd($1.to_i(10), "rb", autoclose: false)
+ w = IO.for_fd($2.to_i(10), "wb", autoclose: false)
+ end
rescue
r.close if r
nil
@@ -162,9 +259,10 @@ module Test
r.close_on_exec = true
w.close_on_exec = true
@jobserver = [r, w]
- options[:parallel] ||= 1
+ options[:parallel] ||= 256 # number of tokens to acquire first
end
end
+ @worker_timeout = EnvUtil.apply_timeout_scale(options[:worker_timeout] || 180)
super
end
@@ -187,6 +285,10 @@ module Test
options[:parallel] = a.to_i
end
+ opts.on '--worker-timeout=N', Integer, "Timeout workers not responding in N seconds" do |a|
+ options[:worker_timeout] = a
+ end
+
opts.on '--separate', "Restart job process after one testcase has done" do
options[:parallel] ||= 1
options[:separate] = true
@@ -200,7 +302,8 @@ module Test
options[:retry] = false
end
- opts.on '--ruby VAL', "Path to ruby which is used at -j option" do |a|
+ opts.on '--ruby VAL', "Path to ruby which is used at -j option",
+ "Also used as EnvUtil.rubybin by some assertion methods" do |a|
options[:ruby] = a.split(/ /).reject(&:empty?)
end
@@ -213,7 +316,7 @@ module Test
def self.launch(ruby,args=[])
scale = EnvUtil.timeout_scale
io = IO.popen([*ruby, "-W1",
- "#{File.dirname(__FILE__)}/unit/parallel.rb",
+ "#{__dir__}/unit/parallel.rb",
*("--timeout-scale=#{scale}" if scale),
*args], "rb+")
new(io, io.pid, :waiting)
@@ -221,6 +324,8 @@ module Test
attr_reader :quit_called
attr_accessor :start_time
+ attr_accessor :response_at
+ attr_accessor :current
@@worker_number = 0
@@ -234,6 +339,7 @@ module Test
@loadpath = []
@hooks = {}
@quit_called = false
+ @response_at = nil
end
def name
@@ -253,6 +359,7 @@ module Test
puts "run #{task} #{type}"
@status = :prepare
@start_time = Time.now
+ @response_at = @start_time
rescue Errno::EPIPE
died
rescue IOError
@@ -269,6 +376,7 @@ module Test
def read
res = (@status == :quit) ? @io.read : @io.gets
+ @response_at = Time.now
res && res.chomp
end
@@ -341,8 +449,8 @@ module Test
real_file = worker.real_file and warn "running file: #{real_file}"
@need_quit = true
warn ""
- warn "Some worker was crashed. It seems ruby interpreter's bug"
- warn "or, a bug of test/unit/parallel.rb. try again without -j"
+ warn "A test worker crashed. It might be an interpreter bug or"
+ warn "a bug in test/unit/parallel.rb. Try again without the -j"
warn "option."
warn ""
if File.exist?('core')
@@ -399,9 +507,11 @@ module Test
@ios.delete worker.io
end
- def quit_workers
+ def quit_workers(&cond)
return if @workers.empty?
+ closed = [] if cond
@workers.reject! do |worker|
+ next unless cond&.call(worker)
begin
Timeout.timeout(1) do
worker.quit
@@ -409,20 +519,33 @@ module Test
rescue Errno::EPIPE
rescue Timeout::Error
end
- worker.close
+ closed&.push worker
+ begin
+ Timeout.timeout(0.2) do
+ worker.close
+ end
+ rescue Timeout::Error
+ worker.kill
+ retry
+ end
+ @ios.delete worker.io
end
- return if @workers.empty?
+ return if (closed ||= @workers).empty?
+ pids = closed.map(&:pid)
begin
- Timeout.timeout(0.2 * @workers.size) do
+ Timeout.timeout(0.2 * closed.size) do
Process.waitall
end
rescue Timeout::Error
- @workers.each do |worker|
- worker.kill
+ if pids
+ Process.kill(:KILL, *pids) rescue nil
+ pids = nil
+ retry
end
- @worker.clear
end
+ @workers.clear unless cond
+ closed
end
FakeClass = Struct.new(:name)
@@ -456,11 +579,13 @@ module Test
@test_count += 1
jobs_status(worker)
+ when /^start (.+?)$/
+ worker.current = Marshal.load($1.unpack1("m"))
when /^done (.+?)$/
begin
- r = Marshal.load($1.unpack("m")[0])
+ r = Marshal.load($1.unpack1("m"))
rescue
- print "unknown object: #{$1.unpack("m")[0].dump}"
+ print "unknown object: #{$1.unpack1("m").dump}"
return true
end
result << r[0..1] unless r[0..1] == [nil,nil]
@@ -471,7 +596,7 @@ module Test
return true
when /^record (.+?)$/
begin
- r = Marshal.load($1.unpack("m")[0])
+ r = Marshal.load($1.unpack1("m"))
suite = r.first
key = [worker.name, suite]
@@ -481,18 +606,18 @@ module Test
@records[key] = [worker.start_time, Time.now]
end
rescue => e
- print "unknown record: #{e.message} #{$1.unpack("m")[0].dump}"
+ print "unknown record: #{e.message} #{$1.unpack1("m").dump}"
return true
end
record(fake_class(r[0]), *r[1..-1])
when /^p (.+?)$/
del_jobs_status
- print $1.unpack("m")[0]
+ print $1.unpack1("m")
jobs_status(worker) if @options[:job_status] == :replace
when /^after (.+?)$/
- @warnings << Marshal.load($1.unpack("m")[0])
+ @warnings << Marshal.load($1.unpack1("m"))
when /^bye (.+?)$/
- after_worker_down worker, Marshal.load($1.unpack("m")[0])
+ after_worker_down worker, Marshal.load($1.unpack1("m"))
when /^bye$/, nil
if shutting_down || worker.quit_called
after_worker_quit worker
@@ -515,16 +640,7 @@ module Test
# Require needed thing for parallel running
require 'timeout'
- @tasks = @files.dup # Array of filenames.
-
- case MiniTest::Unit::TestCase.test_order
- when :random
- @tasks.shuffle!
- else
- # JIT first
- ts = @tasks.group_by{|e| /test_jit/ =~ e ? 0 : 1}
- @tasks = ts[0] + ts[1] if ts.size == 2
- end
+ @tasks = @order.group(@order.sort_by_string(@files)) # Array of filenames.
@need_quit = false
@dead_workers = [] # Array of dead workers.
@@ -537,20 +653,34 @@ module Test
@ios = [] # Array of worker IOs
@job_tokens = String.new(encoding: Encoding::ASCII_8BIT) if @jobserver
begin
- [@tasks.size, @options[:parallel]].min.times {launch_worker}
+ while true
+ newjobs = [@tasks.size, @options[:parallel]].min - @workers.size
+ if newjobs > 0
+ if @jobserver
+ t = @jobserver[0].read_nonblock(newjobs, exception: false)
+ @job_tokens << t if String === t
+ newjobs = @job_tokens.size + 1 - @workers.size
+ end
+ newjobs.times {launch_worker}
+ end
+
+ timeout = [(@workers.filter_map {|w| w.response_at}.min&.-(Time.now) || 0), 0].max + @worker_timeout
- while _io = IO.select(@ios)[0]
- break if _io.any? do |io|
+ if !(_io = IO.select(@ios, nil, nil, timeout))
+ timeout = Time.now - @worker_timeout
+ quit_workers {|w| w.response_at&.<(timeout) }&.map {|w|
+ rep << {file: w.real_file, result: nil, testcase: w.current[0], error: w.current}
+ }
+ elsif _io.first.any? {|io|
@need_quit or
(deal(io, type, result, rep).nil? and
!@workers.any? {|x| [:running, :prepare].include? x.status})
+ }
+ break
end
- if @jobserver and @job_tokens and !@tasks.empty? and !@workers.any? {|x| x.status == :ready}
- t = @jobserver[0].read_nonblock([@tasks.size, @options[:parallel]].min, exception: false)
- if String === t
- @job_tokens << t
- t.size.times {launch_worker}
- end
+ if @tasks.empty?
+ break if @workers.empty?
+ next # wait for all workers to finish
end
end
rescue Interrupt => ex
@@ -558,7 +688,7 @@ module Test
return result
ensure
if file = @options[:timetable_data]
- open(file, 'w'){|f|
+ File.open(file, 'w'){|f|
@records.each{|(worker, suite), (st, ed)|
f.puts '[' + [worker.dump, suite.dump, st.to_f * 1_000, ed.to_f * 1_000].join(", ") + '],'
}
@@ -578,15 +708,50 @@ module Test
unless @interrupt || !@options[:retry] || @need_quit
parallel = @options[:parallel]
@options[:parallel] = false
- suites, rep = rep.partition {|r| r[:testcase] && r[:file] && r[:report].any? {|e| !e[2].is_a?(MiniTest::Skip)}}
+ suites, rep = rep.partition {|r|
+ r[:testcase] && r[:file] &&
+ (!r.key?(:report) || r[:report].any? {|e| !e[2].is_a?(Test::Unit::PendedError)})
+ }
suites.map {|r| File.realpath(r[:file])}.uniq.each {|file| require file}
- suites.map! {|r| eval("::"+r[:testcase])}
del_status_line or puts
+ error, suites = suites.partition {|r| r[:error]}
unless suites.empty?
- puts "\n""Retrying..."
+ puts "\n"
+ @failed_output.puts "Failed tests:"
+ suites.each {|r|
+ r[:report].each {|c, m, e|
+ @failed_output.puts "#{c}##{m}: #{e&.class}: #{e&.message&.slice(/\A.*/)}"
+ }
+ }
+ @failed_output.puts "\n"
+ puts "Retrying..."
@verbose = options[:verbose]
+ suites.map! {|r| ::Object.const_get(r[:testcase])}
_run_suites(suites, type)
end
+ unless error.empty?
+ puts "\n""Retrying hung up testcases..."
+ error = error.map do |r|
+ begin
+ ::Object.const_get(r[:testcase])
+ rescue NameError
+ # testcase doesn't specify the correct case, so show `r` for information
+ require 'pp'
+
+ $stderr.puts "Retrying is failed because the file and testcase is not consistent:"
+ PP.pp r, $stderr
+ @errors += 1
+ nil
+ end
+ end.compact
+ verbose = @verbose
+ job_status = options[:job_status]
+ options[:verbose] = @verbose = true
+ options[:job_status] = :normal
+ result.concat _run_suites(error, type)
+ options[:verbose] = @verbose = verbose
+ options[:job_status] = job_status
+ end
@options[:parallel] = parallel
end
unless @options[:retry]
@@ -594,20 +759,28 @@ module Test
end
unless rep.empty?
rep.each do |r|
- r[:report].each do |f|
+ if r[:error]
+ puke(*r[:error], Timeout::Error.new)
+ next
+ end
+ r[:report]&.each do |f|
puke(*f) if f
end
end
if @options[:retry]
- @errors += rep.map{|x| x[:result][0] }.inject(:+)
- @failures += rep.map{|x| x[:result][1] }.inject(:+)
- @skips += rep.map{|x| x[:result][2] }.inject(:+)
+ rep.each do |x|
+ (e, f, s = x[:result]) or next
+ @errors += e
+ @failures += f
+ @skips += s
+ end
end
end
unless @warnings.empty?
warn ""
@warnings.uniq! {|w| w[1].message}
@warnings.each do |w|
+ @errors += 1
warn "#{w[0]}: #{w[1].message} (#{w[1].class})"
end
warn ""
@@ -677,7 +850,7 @@ module Test
end
end
- def record(suite, method, assertions, time, error)
+ def record(suite, method, assertions, time, error, source_location = nil)
if @options.values_at(:longest, :most_asserted).any?
@tops ||= {}
rec = [suite.name, method, assertions, time, error]
@@ -756,7 +929,7 @@ module Test
end
def jobs_status(worker)
- return if !@options[:job_status] or @options[:verbose]
+ return if !@options[:job_status] or @verbose
if @options[:job_status] == :replace
status_line = @workers.map(&:to_s).join(" ")
else
@@ -775,7 +948,7 @@ module Test
end
def _prepare_run(suites, type)
- options[:job_status] ||= :replace if @tty && !@verbose
+ options[:job_status] ||= @tty ? :replace : :normal unless @verbose
case options[:color]
when :always
color = true
@@ -791,11 +964,14 @@ module Test
@output = Output.new(self) unless @options[:testing]
filter = options[:filter]
type = "#{type}_methods"
- total = if filter
- suites.inject(0) {|n, suite| n + suite.send(type).grep(filter).size}
- else
- suites.inject(0) {|n, suite| n + suite.send(type).size}
- end
+ total = suites.sum {|suite|
+ methods = suite.send(type)
+ if filter
+ methods.count {|method| filter === "#{suite}##{method}"}
+ else
+ methods.size
+ end
+ }
@test_count = 0
@total_tests = total.to_s(10)
end
@@ -833,7 +1009,7 @@ module Test
end
first, msg = msg.split(/$/, 2)
first = sprintf("%3d) %s", @report_count += 1, first)
- $stdout.print(sep, @colorize.decorate(first, color), msg, "\n")
+ @failed_output.print(sep, @colorize.decorate(first, color), msg, "\n")
sep = nil
end
report.clear
@@ -889,7 +1065,7 @@ module Test
runner.add_status(" = #$1")
when /\A\.+\z/
runner.succeed
- when /\A[EFS]\z/
+ when /\A\.*[EFST][EFST.]*\z/
runner.failed(s)
else
$stdout.print(s)
@@ -994,6 +1170,28 @@ module Test
end
end
+ module OutputOption # :nodoc: all
+ def setup_options(parser, options)
+ super
+ parser.separator "output options:"
+
+ options[:failed_output] = $stdout
+ parser.on '--stderr-on-failure', 'Use stderr to print failure messages' do
+ options[:failed_output] = $stderr
+ end
+ parser.on '--stdout-on-failure', 'Use stdout to print failure messages', '(default)' do
+ options[:failed_output] = $stdout
+ end
+ end
+
+ def process_args(args = [])
+ return @options if @options
+ options = super
+ @failed_output = options[:failed_output]
+ options
+ end
+ end
+
module GCOption # :nodoc: all
def setup_options(parser, options)
super
@@ -1008,7 +1206,7 @@ module Test
def non_options(files, options)
if options.delete(:gc_stress)
- MiniTest::Unit::TestCase.class_eval do
+ Test::Unit::TestCase.class_eval do
oldrun = instance_method(:run)
define_method(:run) do |runner|
begin
@@ -1021,7 +1219,7 @@ module Test
end
end
if options.delete(:gc_compact)
- MiniTest::Unit::TestCase.class_eval do
+ Test::Unit::TestCase.class_eval do
oldrun = instance_method(:run)
define_method(:run) do |runner|
begin
@@ -1055,8 +1253,13 @@ module Test
puts "#{f}: #{$!}"
end
}
+ @load_failed = errors.size.nonzero?
result
end
+
+ def run(*)
+ super or @load_failed
+ end
end
module RepeatOption # :nodoc: all
@@ -1158,30 +1361,523 @@ module Test
end
end
- class Runner < MiniTest::Unit # :nodoc: all
- include Test::Unit::Options
- include Test::Unit::StatusLine
- include Test::Unit::Parallel
- include Test::Unit::Statistics
- include Test::Unit::Skipping
- include Test::Unit::GlobOption
- include Test::Unit::RepeatOption
- include Test::Unit::LoadPathOption
- include Test::Unit::GCOption
- include Test::Unit::ExcludesOption
- include Test::Unit::TimeoutOption
- include Test::Unit::RunCount
-
- def run(argv)
+ module LaunchableOption
+ module Nothing
+ private
+ def setup_options(opts, options)
+ super
+ opts.define_tail 'Launchable options:'
+ # This is expected to be called by Test::Unit::Worker.
+ opts.on_tail '--launchable-test-reports=PATH', String, 'Do nothing'
+ end
+ end
+
+ def record(suite, method, assertions, time, error, source_location = nil)
+ if writer = @options[:launchable_test_reports]
+ if loc = (source_location || suite.instance_method(method).source_location)
+ path, lineno = loc
+ # Launchable JSON schema is defined at
+ # https://github.com/search?q=repo%3Alaunchableinc%2Fcli+https%3A%2F%2Flaunchableinc.com%2Fschema%2FRecordTestInput&type=code.
+ e = case error
+ when nil
+ status = 'TEST_PASSED'
+ nil
+ when Test::Unit::PendedError
+ status = 'TEST_SKIPPED'
+ "Skipped:\n#{suite.name}##{method} [#{location error}]:\n#{error.message}\n"
+ when Test::Unit::AssertionFailedError
+ status = 'TEST_FAILED'
+ "Failure:\n#{suite.name}##{method} [#{location error}]:\n#{error.message}\n"
+ when Timeout::Error
+ status = 'TEST_FAILED'
+ "Timeout:\n#{suite.name}##{method}\n"
+ else
+ status = 'TEST_FAILED'
+ bt = Test::filter_backtrace(error.backtrace).join "\n "
+ "Error:\n#{suite.name}##{method}:\n#{error.class}: #{error.message.b}\n #{bt}\n"
+ end
+ repo_path = File.expand_path("#{__dir__}/../../../")
+ relative_path = path.delete_prefix("#{repo_path}/")
+ # The test path is a URL-encoded representation.
+ # https://github.com/launchableinc/cli/blob/v1.81.0/launchable/testpath.py#L18
+ test_path = {file: relative_path, class: suite.name, testcase: method}.map{|key, val|
+ "#{encode_test_path_component(key)}=#{encode_test_path_component(val)}"
+ }.join('#')
+ end
+ end
+ super
+ ensure
+ if writer && test_path && status
+ # Occasionally, the file writing operation may be paused, especially when `--repeat-count` is specified.
+ # In such cases, we proceed to execute the operation here.
+ writer.write_object(
+ {
+ testPath: test_path,
+ status: status,
+ duration: time,
+ createdAt: Time.now.to_s,
+ stderr: e,
+ stdout: nil,
+ data: {
+ lineNumber: lineno
+ }
+ }
+ )
+ end
+ end
+
+ private
+ def setup_options(opts, options)
super
+ opts.on_tail '--launchable-test-reports=PATH', String, 'Report test results in Launchable JSON format' do |path|
+ require 'json'
+ require 'uri'
+ options[:launchable_test_reports] = writer = JsonStreamWriter.new(path)
+ writer.write_array('testCases')
+ main_pid = Process.pid
+ at_exit {
+ # This block is executed when the fork block in a test is completed.
+ # Therefore, we need to verify whether all tests have been completed.
+ stack = caller
+ if stack.size == 0 && main_pid == Process.pid && $!.is_a?(SystemExit)
+ writer.close
+ end
+ }
+ end
+
+ def encode_test_path_component component
+ component.to_s.gsub('%', '%25').gsub('=', '%3D').gsub('#', '%23').gsub('&', '%26')
+ end
+ end
+
+ ##
+ # JsonStreamWriter writes a JSON file using a stream.
+ # By utilizing a stream, we can minimize memory usage, especially for large files.
+ class JsonStreamWriter
+ def initialize(path)
+ @file = File.open(path, "w")
+ @file.write("{")
+ @indent_level = 0
+ @is_first_key_val = true
+ @is_first_obj = true
+ write_new_line
+ end
+
+ def write_object obj
+ if @is_first_obj
+ @is_first_obj = false
+ else
+ write_comma
+ write_new_line
+ end
+ @indent_level += 1
+ @file.write(to_json_str(obj))
+ @indent_level -= 1
+ @is_first_key_val = true
+ # Occasionally, invalid JSON will be created as shown below, especially when `--repeat-count` is specified.
+ # {
+ # "testPath": "file=test%2Ftest_timeout.rb&class=TestTimeout&testcase=test_allows_zero_seconds",
+ # "status": "TEST_PASSED",
+ # "duration": 2.7e-05,
+ # "createdAt": "2024-02-09 12:21:07 +0000",
+ # "stderr": null,
+ # "stdout": null
+ # }: null <- here
+ # },
+ # To prevent this, IO#flush is called here.
+ @file.flush
+ end
+
+ def write_array(key)
+ @indent_level += 1
+ @file.write(to_json_str(key))
+ write_colon
+ @file.write(" ", "[")
+ write_new_line
+ end
+
+ def close
+ return if @file.closed?
+ close_array
+ @indent_level -= 1
+ write_new_line
+ @file.write("}", "\n")
+ @file.flush
+ @file.close
+ end
+
+ private
+ def to_json_str(obj)
+ json = JSON.pretty_generate(obj)
+ json.gsub(/^/, ' ' * (2 * @indent_level))
+ end
+
+ def write_indent
+ @file.write(" " * 2 * @indent_level)
+ end
+
+ def write_new_line
+ @file.write("\n")
+ end
+
+ def write_comma
+ @file.write(',')
+ end
+
+ def write_colon
+ @file.write(":")
+ end
+
+ def close_array
+ write_new_line
+ write_indent
+ @file.write("]")
+ @indent_level -= 1
+ end
+ end
+ end
+
+ class Runner # :nodoc: all
+
+ attr_accessor :report, :failures, :errors, :skips # :nodoc:
+ attr_accessor :assertion_count # :nodoc:
+ attr_writer :test_count # :nodoc:
+ attr_accessor :start_time # :nodoc:
+ attr_accessor :help # :nodoc:
+ attr_accessor :verbose # :nodoc:
+ attr_writer :options # :nodoc:
+
+ ##
+ # :attr:
+ #
+ # if true, installs an "INFO" signal handler (only available to BSD and
+ # OS X users) which prints diagnostic information about the test run.
+ #
+ # This is auto-detected by default but may be overridden by custom
+ # runners.
+
+ attr_accessor :info_signal
+
+ ##
+ # Lazy accessor for options.
+
+ def options
+ @options ||= {seed: 42}
+ end
+
+ @@installed_at_exit ||= false
+ @@out = $stdout
+ @@after_tests = []
+ @@current_repeat_count = 0
+
+ ##
+ # A simple hook allowing you to run a block of code after _all_ of
+ # the tests are done. Eg:
+ #
+ # Test::Unit::Runner.after_tests { p $debugging_info }
+
+ def self.after_tests &block
+ @@after_tests << block
+ end
+
+ ##
+ # Returns the stream to use for output.
+
+ def self.output
+ @@out
+ end
+
+ ##
+ # Sets Test::Unit::Runner to write output to +stream+. $stdout is the default
+ # output
+
+ def self.output= stream
+ @@out = stream
+ end
+
+ ##
+ # Tells Test::Unit::Runner to delegate to +runner+, an instance of a
+ # Test::Unit::Runner subclass, when Test::Unit::Runner#run is called.
+
+ def self.runner= runner
+ @@runner = runner
+ end
+
+ ##
+ # Returns the Test::Unit::Runner subclass instance that will be used
+ # to run the tests. A Test::Unit::Runner instance is the default
+ # runner.
+
+ def self.runner
+ @@runner ||= self.new
+ end
+
+ ##
+ # Return all plugins' run methods (methods that start with "run_").
+
+ def self.plugins
+ @@plugins ||= (["run_tests"] +
+ public_instance_methods(false).
+ grep(/^run_/).map { |s| s.to_s }).uniq
+ end
+
+ ##
+ # Return the IO for output.
+
+ def output
+ self.class.output
+ end
+
+ def puts *a # :nodoc:
+ output.puts(*a)
+ end
+
+ def print *a # :nodoc:
+ output.print(*a)
+ end
+
+ def test_count # :nodoc:
+ @test_count ||= 0
+ end
+
+ ##
+ # Runner for a given +type+ (eg, test vs bench).
+
+ def self.current_repeat_count
+ @@current_repeat_count
+ end
+
+ def _run_anything type
+ suites = Test::Unit::TestCase.send "#{type}_suites"
+ return if suites.empty?
+
+ suites = @order.sort_by_name(suites)
+
+ puts
+ puts "# Running #{type}s:"
+ puts
+
+ @test_count, @assertion_count = 0, 0
+ test_count = assertion_count = 0
+ sync = output.respond_to? :"sync=" # stupid emacs
+ old_sync, output.sync = output.sync, true if sync
+
+ @@current_repeat_count = 0
+ begin
+ start = Time.now
+
+ results = _run_suites suites, type
+
+ @test_count = results.inject(0) { |sum, (tc, _)| sum + tc }
+ @assertion_count = results.inject(0) { |sum, (_, ac)| sum + ac }
+ test_count += @test_count
+ assertion_count += @assertion_count
+ t = Time.now - start
+ @@current_repeat_count += 1
+ unless @repeat_count
+ puts
+ puts
+ end
+ puts "Finished%s %ss in %.6fs, %.4f tests/s, %.4f assertions/s.\n" %
+ [(@repeat_count ? "(#{@@current_repeat_count}/#{@repeat_count}) " : ""), type,
+ t, @test_count.fdiv(t), @assertion_count.fdiv(t)]
+ end while @repeat_count && @@current_repeat_count < @repeat_count &&
+ report.empty? && failures.zero? && errors.zero?
+
+ output.sync = old_sync if sync
+
+ report.each_with_index do |msg, i|
+ puts "\n%3d) %s" % [i + 1, msg]
+ end
+
+ puts
+ @test_count = test_count
+ @assertion_count = assertion_count
+
+ status
+ end
+
+ ##
+ # Run a single +suite+ for a given +type+.
+
+ def _run_suite suite, type
+ header = "#{type}_suite_header"
+ puts send(header, suite) if respond_to? header
+
+ filter = options[:filter]
+
+ all_test_methods = suite.send "#{type}_methods"
+ if filter
+ all_test_methods.select! {|method|
+ filter === "#{suite}##{method}"
+ }
+ end
+ all_test_methods = @order.sort_by_name(all_test_methods)
+
+ leakchecker = LeakChecker.new
+ if ENV["LEAK_CHECKER_TRACE_OBJECT_ALLOCATION"]
+ require "objspace"
+ trace = true
+ end
+
+ assertions = all_test_methods.map { |method|
+
+ inst = suite.new method
+ _start_method(inst)
+ inst._assertions = 0
+
+ print "#{suite}##{method.inspect.sub(/\A:/, '')} = " if @verbose
+
+ start_time = Time.now if @verbose
+ result =
+ if trace
+ ObjectSpace.trace_object_allocations {inst.run self}
+ else
+ inst.run self
+ end
+
+ print "%.2f s = " % (Time.now - start_time) if @verbose
+ print result
+ puts if @verbose
+ $stdout.flush
+
+ unless defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled? # compiler process is wrongly considered as leak
+ leakchecker.check("#{inst.class}\##{inst.__name__}")
+ end
+
+ _end_method(inst)
+
+ inst._assertions
+ }
+ return assertions.size, assertions.inject(0) { |sum, n| sum + n }
+ end
+
+ def _start_method(inst)
+ end
+ def _end_method(inst)
+ end
+
+ ##
+ # Record the result of a single test. Makes it very easy to gather
+ # information. Eg:
+ #
+ # class StatisticsRecorder < Test::Unit::Runner
+ # def record suite, method, assertions, time, error
+ # # ... record the results somewhere ...
+ # end
+ # end
+ #
+ # Test::Unit::Runner.runner = StatisticsRecorder.new
+ #
+ # NOTE: record might be sent more than once per test. It will be
+ # sent once with the results from the test itself. If there is a
+ # failure or error in teardown, it will be sent again with the
+ # error or failure.
+
+ def record suite, method, assertions, time, error, source_location = nil
+ end
+
+ def location e # :nodoc:
+ last_before_assertion = ""
+
+ return '<empty>' unless e&.backtrace # SystemStackError can return nil.
+
+ e.backtrace.reverse_each do |s|
+ break if s =~ /in .(?:Test::Unit::(?:Core)?Assertions#)?(assert|refute|flunk|pass|fail|raise|must|wont)/
+ last_before_assertion = s
+ end
+ last_before_assertion.sub(/:in .*$/, '')
+ end
+
+ ##
+ # Writes status for failed test +meth+ in +klass+ which finished with
+ # exception +e+
+
+ def initialize # :nodoc:
+ @report = []
+ @errors = @failures = @skips = 0
+ @verbose = false
+ @mutex = Thread::Mutex.new
+ @info_signal = Signal.list['INFO']
+ @repeat_count = nil
+ end
+
+ def synchronize # :nodoc:
+ if @mutex then
+ @mutex.synchronize { yield }
+ else
+ yield
+ end
+ end
+
+ def inspect
+ "#<#{self.class.name}: " <<
+ instance_variables.filter_map do |var|
+ next if var == :@option_parser # too big
+ "#{var}=#{instance_variable_get(var).inspect}"
+ end.join(", ") << ">"
+ end
+
+ ##
+ # Top level driver, controls all output and filtering.
+
+ def _run args = []
+ args = process_args args # ARGH!! blame test/unit process_args
+ self.options.merge! args
+
+ puts "Run options: #{help}"
+
+ self.class.plugins.each do |plugin|
+ send plugin
+ break unless report.empty?
+ end
+
+ return (failures + errors).nonzero? # or return nil...
+ rescue Interrupt
+ abort 'Interrupted'
+ end
+
+ ##
+ # Runs test suites matching +filter+.
+
+ def run_tests
+ _run_anything :test
+ end
+
+ ##
+ # Writes status to +io+
+
+ def status io = self.output
+ format = "%d tests, %d assertions, %d failures, %d errors, %d skips"
+ io.puts format % [test_count, assertion_count, failures, errors, skips]
+ end
+
+ prepend Test::Unit::Options
+ prepend Test::Unit::StatusLine
+ prepend Test::Unit::Parallel
+ prepend Test::Unit::Statistics
+ prepend Test::Unit::Skipping
+ prepend Test::Unit::GlobOption
+ prepend Test::Unit::OutputOption
+ prepend Test::Unit::RepeatOption
+ prepend Test::Unit::LoadPathOption
+ prepend Test::Unit::GCOption
+ prepend Test::Unit::ExcludesOption
+ prepend Test::Unit::TimeoutOption
+ prepend Test::Unit::RunCount
+ prepend Test::Unit::LaunchableOption::Nothing
+
+ ##
+ # Begins the full test run. Delegates to +runner+'s #_run method.
+
+ def run(argv = [])
+ self.class.runner._run(argv)
rescue NoMemoryError
system("cat /proc/meminfo") if File.exist?("/proc/meminfo")
system("ps x -opid,args,%cpu,%mem,nlwp,rss,vsz,wchan,stat,start,time,etime,blocked,caught,ignored,pending,f") if File.exist?("/bin/ps")
raise
end
- class << self; undef autorun; end
-
@@stop_auto_run = false
def self.autorun
at_exit {
@@ -1192,16 +1888,30 @@ module Test
@@installed_at_exit = true
end
- alias mini_run_suite _run_suite
+ alias orig_run_suite _run_suite
- # Overriding of MiniTest::Unit#puke
+ # Overriding of Test::Unit::Runner#puke
def puke klass, meth, e
- # TODO:
- # this overriding is for minitest feature that skip messages are
- # hidden when not verbose (-v), note this is temporally.
n = report.size
- rep = super
- if MiniTest::Skip === e and /no message given\z/ =~ e.message
+ e = case e
+ when Test::Unit::PendedError then
+ @skips += 1
+ return "S" unless @verbose
+ "Skipped:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n"
+ when Test::Unit::AssertionFailedError then
+ @failures += 1
+ "Failure:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n"
+ when Timeout::Error
+ @errors += 1
+ "Timeout:\n#{klass}##{meth}\n"
+ else
+ @errors += 1
+ bt = Test::filter_backtrace(e.backtrace).join "\n "
+ "Error:\n#{klass}##{meth}:\n#{e.class}: #{e.message.b}\n #{bt}\n"
+ end
+ @report << e
+ rep = e[0, 1]
+ if Test::Unit::PendedError === e and /no message given\z/ =~ e.message
report.slice!(n..-1)
rep = "."
end
@@ -1212,6 +1922,7 @@ module Test
class AutoRunner # :nodoc: all
class Runner < Test::Unit::Runner
include Test::Unit::RequireFiles
+ include Test::Unit::LaunchableOption
end
attr_accessor :to_run, :options
@@ -1262,30 +1973,4 @@ module Test
end
end
-module MiniTest # :nodoc: all
- class Unit
- end
-end
-
-class MiniTest::Unit::TestCase # :nodoc: all
- test_order = self.test_order
- class << self
- attr_writer :test_order
- undef test_order
- end
- def self.test_order
- defined?(@test_order) ? @test_order : superclass.test_order
- end
- self.test_order = test_order
- undef run_test
- RUN_TEST_TRACE = "#{__FILE__}:#{__LINE__+3}:in `run_test'".freeze
- def run_test(name)
- progname, $0 = $0, "#{$0}: #{self.class}##{name}"
- self.__send__(name)
- ensure
- $@.delete(RUN_TEST_TRACE) if $@
- $0 = progname
- end
-end
-
Test::Unit::Runner.autorun