Skip to content

Commit

Permalink
Bump up the latest version of CoreAssertions
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbt committed Sep 11, 2021
1 parent 0da149e commit 0bfdd72
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ end

task :sync_tool do
require 'fileutils'
FileUtils.cp "../ruby/tool/lib/test/unit/core_assertions.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/core_assertions.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
end
Expand Down
67 changes: 36 additions & 31 deletions test/lib/core_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,8 @@ def message msg = nil, ending = nil, &default
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'

def mu_pp(obj) #:nodoc:
obj.pretty_inspect.chomp
Expand Down Expand Up @@ -117,16 +102,16 @@ def assert_no_memory_leak(args, prepare, code, message=nil, limit: 2.0, rss: fal
# TODO: consider choosing some appropriate limit for MJIT and stop skipping this once it does not randomly fail
pend 'assert_no_memory_leak may consider MJIT memory usage as leak' if defined?(RubyVM::JIT) && RubyVM::JIT.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)
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", "-",
]
Expand Down Expand Up @@ -183,11 +168,11 @@ def assert_nothing_raised(*args)
end
begin
line = __LINE__; yield
rescue MiniTest::Skip
rescue Test::Unit::PendedError
raise
rescue Exception => e
bt = e.backtrace
as = e.instance_of?(MiniTest::Assertion)
as = e.instance_of?(Test::Unit::AssertionFailedError)
if as
ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o
bt.reject! {|ln| ans =~ ln}
Expand All @@ -199,7 +184,7 @@ def assert_nothing_raised(*args)
"Backtrace:\n" +
e.backtrace.map{|frame| " #{frame}"}.join("\n")
}
raise MiniTest::Assertion, msg.call, bt
raise Test::Unit::AssertionFailedError, msg.call, bt
else
raise
end
Expand Down Expand Up @@ -260,9 +245,10 @@ 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)
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 [Marshal.dump($!)].pack('m'), "assertions=#{self._assertions}"
}
Test::Unit::Runner.class_variable_set(:@@stop_auto_run, true) if defined?(Test::Unit::Runner)
end
Expand All @@ -276,14 +262,14 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
capture_stdout = true
unless /mswin|mingw/ =~ RUBY_PLATFORM
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
src = <<eom
# -*- coding: #{line += __LINE__; src.encoding}; -*-
BEGIN {
require "test/unit";include Test::Unit::Assertions;require #{(__dir__ + "/core_assertions").dump};include Test::Unit::CoreAssertions
require "test/unit";include Test::Unit::Assertions;require #{__FILE__.dump};include Test::Unit::CoreAssertions
separated_runner #{res_c&.fileno}
}
#{line -= __LINE__; src}
Expand Down Expand Up @@ -401,8 +387,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|
Expand Down Expand Up @@ -477,7 +463,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:
MINI_DIR = File.join(File.dirname(File.expand_path(__FILE__)), "minitest") #:nodoc:

# :call-seq:
# assert(test, [failure_message])
Expand Down Expand Up @@ -623,6 +609,7 @@ def assert_deprecated_warn(mesg = /deprecated/)
end

class << (AssertFile = Struct.new(:failure_message).new)
include Assertions
include CoreAssertions
def assert_file_predicate(predicate, *args)
if /\Anot_/ =~ predicate
Expand Down Expand Up @@ -713,10 +700,20 @@ def assert_join_threads(threads, message = nil)
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
Expand All @@ -725,6 +722,14 @@ def assert_all_assertions(msg = nil)
end
alias all_assertions assert_all_assertions

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

def message(msg = nil, *args, &default) # :nodoc:
if Proc === msg
super(nil, *args) do
Expand Down
6 changes: 4 additions & 2 deletions test/lib/envutil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,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)
Expand All @@ -141,6 +141,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}
Expand All @@ -152,7 +154,7 @@ def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr =
end
child_env['ASAN_OPTIONS'] = ENV['ASAN_OPTIONS'] if ENV['ASAN_OPTIONS']
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
Expand Down

0 comments on commit 0bfdd72

Please sign in to comment.