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.rb584
1 files changed, 476 insertions, 108 deletions
diff --git a/tool/lib/test/unit.rb b/tool/lib/test/unit.rb
index bfa1964963..d758b5fb02 100644
--- a/tool/lib/test/unit.rb
+++ b/tool/lib/test/unit.rb
@@ -1,61 +1,117 @@
# frozen_string_literal: true
+# 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_relative '../envutil'
require_relative '../colorize'
-require 'test/unit/testcase'
+require_relative '../leakchecker'
+require_relative '../test/unit/testcase'
require 'optparse'
-require "leakchecker"
# See Test::Unit
module Test
- class << self
+ ##
+ # Test::Unit is an implementation of the xUnit testing framework for Ruby.
+ module Unit
##
- # Filter object for backtraces.
+ # Assertion base class
- attr_accessor :backtrace_filter
- end
+ class AssertionFailedError < Exception; end
+
+ ##
+ # Assertion raised when skipping a test
- class BacktraceFilter # :nodoc:
- def filter bt
- return ["No backtrace"] unless bt
+ class PendedError < AssertionFailedError; end
- new_bt = []
+ module Order
+ class NoSort
+ def initialize(seed)
+ end
- unless $DEBUG then
- bt.each do |line|
- break if line =~ /lib\/test/
- new_bt << line
+ def sort_by_name(list)
+ list
end
- new_bt = bt.reject { |line| line =~ /lib\/test/ } if new_bt.empty?
- new_bt = bt.dup if new_bt.empty?
- else
- new_bt = bt.dup
+ alias sort_by_string sort_by_name
+
+ def group(list)
+ list
+ end
end
- new_bt
- end
- end
+ class Alpha < NoSort
+ def sort_by_name(list)
+ list.sort_by(&:name)
+ end
- self.backtrace_filter = BacktraceFilter.new
+ def sort_by_string(list)
+ list.sort
+ end
- def self.filter_backtrace bt # :nodoc:
- backtrace_filter.filter bt
- end
+ end
- ##
- # Test::Unit is an implementation of the xUnit testing framework for Ruby.
- module Unit
- ##
- # Assertion base class
+ # 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
- class AssertionFailedError < Exception; end
+ salt = [seed].pack("V").unpack1("H*")
+ super(seed, "\n#{salt}".freeze).freeze
+ end
- ##
- # Assertion raised when skipping a test
+ def sort_by_name(list)
+ list.sort_by {|e| randomize_key(e.name)}
+ end
- class PendedError < AssertionFailedError; 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
@@ -103,17 +159,18 @@ module Test
order = options[:test_order]
if seed = options[:seed]
order ||= :random
- srand(seed)
- else
- seed = options[:seed] = srand % 100_000
- srand(seed)
+ 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
@@ -139,7 +196,8 @@ module Test
(options[:filter] ||= []) << a
end
- opts.on '--test-order=random|alpha|sorted|nosort', [:random, :alpha, :sorted, :nosort] do |a|
+ orders = Test::Unit::Order::Types.keys
+ opts.on "--test-order=#{orders.join('|')}", orders do |a|
options[:test_order] = a
end
end
@@ -154,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
@@ -181,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
@@ -192,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
@@ -217,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
@@ -230,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
@@ -243,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)
@@ -251,6 +324,8 @@ module Test
attr_reader :quit_called
attr_accessor :start_time
+ attr_accessor :response_at
+ attr_accessor :current
@@worker_number = 0
@@ -264,6 +339,7 @@ module Test
@loadpath = []
@hooks = {}
@quit_called = false
+ @response_at = nil
end
def name
@@ -283,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
@@ -299,6 +376,7 @@ module Test
def read
res = (@status == :quit) ? @io.read : @io.gets
+ @response_at = Time.now
res && res.chomp
end
@@ -371,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')
@@ -429,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
@@ -439,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)
@@ -486,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]
@@ -501,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]
@@ -511,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
@@ -545,16 +640,7 @@ module Test
# Require needed thing for parallel running
require 'timeout'
- @tasks = @files.dup # Array of filenames.
-
- case Test::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.
@@ -567,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
- while _io = IO.select(@ios)[0]
- break if _io.any? do |io|
+ timeout = [(@workers.filter_map {|w| w.response_at}.min&.-(Time.now) || 0), 0].max + @worker_timeout
+
+ 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
@@ -588,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(", ") + '],'
}
@@ -608,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?(Test::Unit::PendedError)}}
+ 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]
@@ -624,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 ""
@@ -707,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]
@@ -786,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
@@ -805,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
@@ -821,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
@@ -863,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
@@ -919,7 +1065,7 @@ module Test
runner.add_status(" = #$1")
when /\A\.+\z/
runner.succeed
- when /\A\.*[EFS][EFS.]*\z/
+ when /\A\.*[EFST][EFST.]*\z/
runner.failed(s)
else
$stdout.print(s)
@@ -1024,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
@@ -1085,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
@@ -1188,6 +1361,182 @@ module Test
end
end
+ 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:
@@ -1302,6 +1651,8 @@ module Test
suites = Test::Unit::TestCase.send "#{type}_suites"
return if suites.empty?
+ suites = @order.sort_by_name(suites)
+
puts
puts "# Running #{type}s:"
puts
@@ -1356,6 +1707,12 @@ module Test
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"]
@@ -1363,15 +1720,13 @@ module Test
trace = true
end
- assertions = all_test_methods.filter_map { |method|
- if filter
- next unless filter === method || filter === "#{suite}##{method}"
- end
+ assertions = all_test_methods.map { |method|
inst = suite.new method
+ _start_method(inst)
inst._assertions = 0
- print "#{suite}##{method} = " if @verbose
+ print "#{suite}##{method.inspect.sub(/\A:/, '')} = " if @verbose
start_time = Time.now if @verbose
result =
@@ -1386,15 +1741,22 @@ module Test
puts if @verbose
$stdout.flush
- unless defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # compiler process is wrongly considered as leak
+ 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:
@@ -1412,16 +1774,16 @@ module Test
# failure or error in teardown, it will be sent again with the
# error or failure.
- def record suite, method, assertions, time, error
+ 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.
+ return '<empty>' unless e&.backtrace # SystemStackError can return nil.
e.backtrace.reverse_each do |s|
- break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
+ 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 .*$/, '')
@@ -1470,7 +1832,7 @@ module Test
break unless report.empty?
end
- return failures + errors if self.test_count > 0 # or return nil...
+ return (failures + errors).nonzero? # or return nil...
rescue Interrupt
abort 'Interrupted'
end
@@ -1496,12 +1858,14 @@ module Test
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.
@@ -1537,6 +1901,9 @@ module Test
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 "
@@ -1555,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