From 1af1d4334b4a93de3953602eb539fc05dcfe3442 Mon Sep 17 00:00:00 2001
From: Hiroshi SHIBATA <hsbt@ruby-lang.org>
Date: Fri, 24 Mar 2023 13:00:06 +0900
Subject: [PATCH] Update test libraries from
 https://github.com/ruby/ruby/commit/b4e438d8aabaf4bba2b27f374c787543fae07c58

---
 test/lib/core_assertions.rb | 205 +++++++++++++++++++++---------------
 test/lib/envutil.rb         |  25 +++--
 2 files changed, 137 insertions(+), 93 deletions(-)

diff --git a/test/lib/core_assertions.rb b/test/lib/core_assertions.rb
index f292ce9..1fdc0a3 100644
--- a/test/lib/core_assertions.rb
+++ b/test/lib/core_assertions.rb
@@ -3,6 +3,10 @@
 module Test
   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 +20,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 = /(?<!\.)\z/; ".")
+          ending_pattern ||= /(?<!#{Regexp.quote(ending)})\z/
+          msg = msg.call if Proc === msg
+          ary = [msg, (default.call if default)].compact.reject(&:empty?)
+          ary.map! {|str| str.to_s.sub(ending_pattern, ending) }
+          begin
+            ary.join("\n")
+          rescue Encoding::CompatibilityError
+            ary.map(&:b).join("\n")
+          end
         }
       end
     end
 
     module CoreAssertions
-      if defined?(MiniTest)
-        require_relative '../../envutil'
-        # for ruby core testing
-        include MiniTest::Assertions
-
-        # Compatibility hack for assert_raise
-        Test::Unit::AssertionFailedError = MiniTest::Assertion
-      else
-        module MiniTest
-          class Assertion < Exception; end
-          class Skip < Assertion; end
-        end
-
-        require 'pp'
-        require_relative 'envutil'
-        include Test::Unit::Assertions
-      end
+      require_relative 'envutil'
+      require 'pp'
+      nil.pretty_inspect
 
       def mu_pp(obj) #:nodoc:
         obj.pretty_inspect.chomp
@@ -114,19 +111,19 @@ def syntax_check(code, fname, line)
       end
 
       def assert_no_memory_leak(args, prepare, code, message=nil, limit: 2.0, rss: false, **opt)
-        # TODO: consider choosing some appropriate limit for MJIT and stop skipping this once it does not randomly fail
+        # TODO: consider choosing some appropriate limit for RJIT and stop skipping this once it does not randomly fail
+        pend 'assert_no_memory_leak may consider RJIT memory usage as leak' if defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
+        # For previous versions which implemented MJIT
         pend 'assert_no_memory_leak may consider MJIT memory usage as leak' if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled?
 
-        require_relative '../../memory_status'
-        raise MiniTest::Skip, "unsupported platform" unless defined?(Memory::Status)
+        require_relative 'memory_status'
+        raise Test::Unit::PendedError, "unsupported platform" unless defined?(Memory::Status)
 
-        token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m"
-        token_dump = token.dump
-        token_re = Regexp.quote(token)
+        token_dump, token_re = new_test_token
         envs = args.shift if Array === args and Hash === args.first
         args = [
           "--disable=gems",
-          "-r", File.expand_path("../../../memory_status", __FILE__),
+          "-r", File.expand_path("../memory_status", __FILE__),
           *args,
           "-v", "-",
         ]
@@ -182,27 +179,15 @@ def assert_nothing_raised(*args)
           msg = args.pop
         end
         begin
-          line = __LINE__; yield
-        rescue MiniTest::Skip
+          yield
+        rescue Test::Unit::PendedError, *(Test::Unit::AssertionFailedError if args.empty?)
           raise
-        rescue Exception => 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,13 +244,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}<error>", [Marshal.dump($!)].pack('m'), "#{token}</error>", "#{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)
@@ -275,17 +264,18 @@ 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[:ios] = [res_c]
         end
+        token_dump, token_re = new_test_token
         src = <<eom
 # -*- coding: #{line += __LINE__; src.encoding}; -*-
 BEGIN {
-  require "test/unit";include Test::Unit::Assertions;require #{(__dir__ + "/core_assertions").dump};include Test::Unit::CoreAssertions
-  separated_runner #{res_c&.fileno}
+  require "test/unit";include Test::Unit::Assertions;require #{__FILE__.dump};include Test::Unit::CoreAssertions
+  separated_runner #{token_dump}, #{res_c&.fileno || 'nil'}
 }
 #{line -= __LINE__; src}
 eom
@@ -304,9 +294,9 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
         raise if $!
         abort = status.coredump? || (status.signaled? && ABORT_SIGNALS.include?(status.termsig))
         assert(!abort, FailDesc[status, nil, stderr])
-        self._assertions += res[/^assertions=(\d+)/, 1].to_i
+        self._assertions += res[/^#{token_re}assertions=(\d+)/, 1].to_i
         begin
-          res = Marshal.load(res.unpack1("m"))
+          res = Marshal.load(res[/^#{token_re}<error>\n\K.*\n(?=#{token_re}<\/error>$)/m].unpack1("m"))
         rescue => marshal_error
           ignore_stderr = nil
           res = nil
@@ -403,8 +393,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|
@@ -479,7 +469,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])
@@ -499,7 +489,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
@@ -522,7 +512,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)
@@ -545,17 +535,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
@@ -564,11 +554,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
@@ -585,7 +577,7 @@ def assert_pattern_list(pattern_list, actual, message=nil)
               }
               assert false, msg
             end
-            rest = match.post_match
+            rest = post_match
             anchored = true
           end
         }
@@ -612,19 +604,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
@@ -656,7 +649,7 @@ def initialize
 
         def for(key)
           @count += 1
-          yield
+          yield key
         rescue Exception => e
           @failures[key] = [@count, e]
         end
@@ -710,13 +703,23 @@ 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)
@@ -727,21 +730,44 @@ 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(/(?<!\.)\z/, '.') }
-            end
-            begin
-              ary.join("\n")
-            rescue Encoding::CompatibilityError
-              ary.map(&:b).join("\n")
-            end
+      def assert_all_assertions_foreach(msg = nil, *keys, &block)
+        all = AllFailures.new
+        all.foreach(*keys, &block)
+      ensure
+        assert(all.pass?, message(msg) {all.message.chomp(".")})
+      end
+      alias all_assertions_foreach assert_all_assertions_foreach
+
+      # Expect +seq+ to respond to +first+ and +each+ methods, e.g.,
+      # Array, Range, Enumerator::ArithmeticSequence and other
+      # Enumerable-s, and each elements should be size factors.
+      #
+      # :yield: each elements of +seq+.
+      def assert_linear_performance(seq, rehearsal: nil, pre: ->(n) {n})
+        first = seq.first
+        *arg = pre.call(first)
+        times = (0..(rehearsal || (2 * first))).map do
+          st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+          yield(*arg)
+          t = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st)
+          assert_operator 0, :<=, t
+          t.nonzero?
+        end
+        times.compact!
+        tmin, tmax = times.minmax
+        tmax *= tmax / tmin
+        tmax = 10**Math.log10(tmax).ceil
+
+        seq.each do |i|
+          next if i == first
+          t = tmax * i.fdiv(first)
+          *arg = pre.call(i)
+          message = "[#{i}]: in #{t}s"
+          Timeout.timeout(t, Timeout::Error, message) do
+            st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+            yield(*arg)
+            assert_operator (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st), :<=, t, message
           end
-        else
-          super
         end
       end
 
@@ -760,6 +786,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/test/lib/envutil.rb b/test/lib/envutil.rb
index 0391b90..728ca70 100644
--- a/test/lib/envutil.rb
+++ b/test/lib/envutil.rb
@@ -152,7 +152,12 @@ 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 ENV[name]
+    }
+
     args = [args] if args.kind_of?(String)
     pid = spawn(child_env, *precommand, rubybin, *args, opt)
     in_c.close
@@ -292,16 +297,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
