Skip to content

Commit 28fa02e

Browse files
committed
Sync tools
1 parent 1bddbbf commit 28fa02e

File tree

2 files changed

+58
-63
lines changed

2 files changed

+58
-63
lines changed

test/lib/core_assertions.rb

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
module Test
44
module Unit
55
module Assertions
6+
def assert_raises(*exp, &b)
7+
raise NoMethodError, "use assert_raise", caller
8+
end
9+
610
def _assertions= n # :nodoc:
711
@_assertions = n
812
end
@@ -16,16 +20,24 @@ def _assertions # :nodoc:
1620

1721
def message msg = nil, ending = nil, &default
1822
proc {
19-
msg = msg.call.chomp(".") if Proc === msg
20-
custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty?
21-
"#{custom_message}#{default.call}#{ending || "."}"
23+
ending ||= (ending_pattern = /(?<!\.)\z/; ".")
24+
ending_pattern ||= /(?<!#{Regexp.quote(ending)})\z/
25+
msg = msg.call if Proc === msg
26+
ary = [msg, (default.call if default)].compact.reject(&:empty?)
27+
ary.map! {|str| str.to_s.sub(ending_pattern, ending) }
28+
begin
29+
ary.join("\n")
30+
rescue Encoding::CompatibilityError
31+
ary.map(&:b).join("\n")
32+
end
2233
}
2334
end
2435
end
2536

2637
module CoreAssertions
2738
require_relative 'envutil'
2839
require 'pp'
40+
nil.pretty_inspect
2941

3042
def mu_pp(obj) #:nodoc:
3143
obj.pretty_inspect.chomp
@@ -105,9 +117,7 @@ def assert_no_memory_leak(args, prepare, code, message=nil, limit: 2.0, rss: fal
105117
require_relative 'memory_status'
106118
raise Test::Unit::PendedError, "unsupported platform" unless defined?(Memory::Status)
107119

108-
token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m"
109-
token_dump = token.dump
110-
token_re = Regexp.quote(token)
120+
token_dump, token_re = new_test_token
111121
envs = args.shift if Array === args and Hash === args.first
112122
args = [
113123
"--disable=gems",
@@ -167,27 +177,15 @@ def assert_nothing_raised(*args)
167177
msg = args.pop
168178
end
169179
begin
170-
line = __LINE__; yield
171-
rescue Test::Unit::PendedError
180+
yield
181+
rescue Test::Unit::PendedError, *(Test::Unit::AssertionFailedError if args.empty?)
172182
raise
173-
rescue Exception => e
174-
bt = e.backtrace
175-
as = e.instance_of?(Test::Unit::AssertionFailedError)
176-
if as
177-
ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o
178-
bt.reject! {|ln| ans =~ ln}
179-
end
180-
if ((args.empty? && !as) ||
181-
args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
182-
msg = message(msg) {
183-
"Exception raised:\n<#{mu_pp(e)}>\n" +
184-
"Backtrace:\n" +
185-
e.backtrace.map{|frame| " #{frame}"}.join("\n")
186-
}
187-
raise Test::Unit::AssertionFailedError, msg.call, bt
188-
else
189-
raise
190-
end
183+
rescue *(args.empty? ? Exception : args) => e
184+
msg = message(msg) {
185+
"Exception raised:\n<#{mu_pp(e)}>\n""Backtrace:\n" <<
186+
Test.filter_backtrace(e.backtrace).map{|frame| " #{frame}"}.join("\n")
187+
}
188+
raise Test::Unit::AssertionFailedError, msg.call, e.backtrace
191189
end
192190
end
193191

@@ -244,11 +242,11 @@ def assert_ruby_status(args, test_stdin="", message=nil, **opt)
244242

245243
ABORT_SIGNALS = Signal.list.values_at(*%w"ILL ABRT BUS SEGV TERM")
246244

247-
def separated_runner(out = nil)
245+
def separated_runner(token, out = nil)
248246
include(*Test::Unit::TestCase.ancestors.select {|c| !c.is_a?(Class) })
249247
out = out ? IO.new(out, 'w') : STDOUT
250248
at_exit {
251-
out.puts [Marshal.dump($!)].pack('m'), "assertions=#{self._assertions}"
249+
out.puts "#{token}<error>", [Marshal.dump($!)].pack('m'), "#{token}</error>", "#{token}assertions=#{self._assertions}"
252250
}
253251
Test::Unit::Runner.class_variable_set(:@@stop_auto_run, true) if defined?(Test::Unit::Runner)
254252
end
@@ -260,22 +258,24 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
260258
line ||= loc.lineno
261259
end
262260
capture_stdout = true
263-
unless /mswin|mingw/ =~ RUBY_PLATFORM
261+
unless /mswin|mingw/ =~ RbConfig::CONFIG['host_os']
264262
capture_stdout = false
265263
opt[:out] = Test::Unit::Runner.output if defined?(Test::Unit::Runner)
266264
res_p, res_c = IO.pipe
267265
opt[:ios] = [res_c]
268266
end
267+
token_dump, token_re = new_test_token
269268
src = <<eom
270269
# -*- coding: #{line += __LINE__; src.encoding}; -*-
271270
BEGIN {
272271
require "test/unit";include Test::Unit::Assertions;require #{__FILE__.dump};include Test::Unit::CoreAssertions
273-
separated_runner #{res_c&.fileno}
272+
separated_runner #{token_dump}, #{res_c&.fileno || 'nil'}
274273
}
275274
#{line -= __LINE__; src}
276275
eom
277276
args = args.dup
278277
args.insert((Hash === args.first ? 1 : 0), "-w", "--disable=gems", *$:.map {|l| "-I#{l}"})
278+
args << "--debug" if RUBY_ENGINE == 'jruby' # warning: tracing (e.g. set_trace_func) will not capture all events without --debug flag
279279
stdout, stderr, status = EnvUtil.invoke_ruby(args, src, capture_stdout, true, **opt)
280280
ensure
281281
if res_c
@@ -288,9 +288,9 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
288288
raise if $!
289289
abort = status.coredump? || (status.signaled? && ABORT_SIGNALS.include?(status.termsig))
290290
assert(!abort, FailDesc[status, nil, stderr])
291-
self._assertions += res[/^assertions=(\d+)/, 1].to_i
291+
self._assertions += res[/^#{token_re}assertions=(\d+)/, 1].to_i
292292
begin
293-
res = Marshal.load(res.unpack1("m"))
293+
res = Marshal.load(res[/^#{token_re}<error>\n\K.*\n(?=#{token_re}<\/error>$)/m].unpack1("m"))
294294
rescue => marshal_error
295295
ignore_stderr = nil
296296
res = nil
@@ -463,7 +463,7 @@ def assert_raise_with_message(exception, expected, msg = nil, &block)
463463
ex
464464
end
465465

466-
MINI_DIR = File.join(File.dirname(File.expand_path(__FILE__)), "minitest") #:nodoc:
466+
TEST_DIR = File.join(__dir__, "test/unit") #:nodoc:
467467

468468
# :call-seq:
469469
# assert(test, [failure_message])
@@ -483,7 +483,7 @@ def assert(test, *msgs)
483483
when nil
484484
msgs.shift
485485
else
486-
bt = caller.reject { |s| s.start_with?(MINI_DIR) }
486+
bt = caller.reject { |s| s.start_with?(TEST_DIR) }
487487
raise ArgumentError, "assertion message must be String or Proc, but #{msg.class} was given.", bt
488488
end unless msgs.empty?
489489
super
@@ -506,7 +506,7 @@ def assert_respond_to(obj, (meth, *priv), msg = nil)
506506
return assert obj.respond_to?(meth, *priv), msg
507507
end
508508
#get rid of overcounting
509-
if caller_locations(1, 1)[0].path.start_with?(MINI_DIR)
509+
if caller_locations(1, 1)[0].path.start_with?(TEST_DIR)
510510
return if obj.respond_to?(meth)
511511
end
512512
super(obj, meth, msg)
@@ -529,7 +529,7 @@ def assert_not_respond_to(obj, (meth, *priv), msg = nil)
529529
return assert !obj.respond_to?(meth, *priv), msg
530530
end
531531
#get rid of overcounting
532-
if caller_locations(1, 1)[0].path.start_with?(MINI_DIR)
532+
if caller_locations(1, 1)[0].path.start_with?(TEST_DIR)
533533
return unless obj.respond_to?(meth)
534534
end
535535
refute_respond_to(obj, meth, msg)
@@ -548,11 +548,13 @@ def assert_pattern_list(pattern_list, actual, message=nil)
548548
anchored = false
549549
else
550550
if anchored
551-
match = /\A#{pattern}/.match(rest)
551+
match = rest.rindex(pattern, 0)
552552
else
553-
match = pattern.match(rest)
553+
match = rest.index(pattern)
554554
end
555-
unless match
555+
if match
556+
post_match = $~ ? $~.post_match : rest[match+pattern.size..-1]
557+
else
556558
msg = message(msg) {
557559
expect_msg = "Expected #{mu_pp pattern}\n"
558560
if /\n[^\n]/ =~ rest
@@ -569,7 +571,7 @@ def assert_pattern_list(pattern_list, actual, message=nil)
569571
}
570572
assert false, msg
571573
end
572-
rest = match.post_match
574+
rest = post_match
573575
anchored = true
574576
end
575577
}
@@ -596,14 +598,14 @@ def assert_warn(*args)
596598

597599
def assert_deprecated_warning(mesg = /deprecated/)
598600
assert_warning(mesg) do
599-
Warning[:deprecated] = true
601+
Warning[:deprecated] = true if Warning.respond_to?(:[]=)
600602
yield
601603
end
602604
end
603605

604606
def assert_deprecated_warn(mesg = /deprecated/)
605607
assert_warn(mesg) do
606-
Warning[:deprecated] = true
608+
Warning[:deprecated] = true if Warning.respond_to?(:[]=)
607609
yield
608610
end
609611
end
@@ -641,7 +643,7 @@ def initialize
641643

642644
def for(key)
643645
@count += 1
644-
yield
646+
yield key
645647
rescue Exception => e
646648
@failures[key] = [@count, e]
647649
end
@@ -694,7 +696,8 @@ def assert_join_threads(threads, message = nil)
694696
if !errs.empty?
695697
msg = "exceptions on #{errs.length} threads:\n" +
696698
errs.map {|t, err|
697-
"#{t.inspect}:\n" + err.full_message(highlight: false, order: :top)
699+
"#{t.inspect}:\n" +
700+
err.full_message(highlight: false, order: :top)
698701
}.join("\n---\n")
699702
if message
700703
msg = "#{message}\n#{msg}"
@@ -729,24 +732,6 @@ def assert_all_assertions_foreach(msg = nil, *keys, &block)
729732
end
730733
alias all_assertions_foreach assert_all_assertions_foreach
731734

732-
def message(msg = nil, *args, &default) # :nodoc:
733-
if Proc === msg
734-
super(nil, *args) do
735-
ary = [msg.call, (default.call if default)].compact.reject(&:empty?)
736-
if 1 < ary.length
737-
ary[0...-1] = ary[0...-1].map {|str| str.sub(/(?<!\.)\z/, '.') }
738-
end
739-
begin
740-
ary.join("\n")
741-
rescue Encoding::CompatibilityError
742-
ary.map(&:b).join("\n")
743-
end
744-
end
745-
else
746-
super
747-
end
748-
end
749-
750735
def diff(exp, act)
751736
require 'pp'
752737
q = PP.new(+"")
@@ -762,6 +747,11 @@ def diff(exp, act)
762747
end
763748
q.output
764749
end
750+
751+
def new_test_token
752+
token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m"
753+
return token.dump, Regexp.quote(token)
754+
end
765755
end
766756
end
767757
end

test/lib/envutil.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr =
152152
if RUBYLIB and lib = child_env["RUBYLIB"]
153153
child_env["RUBYLIB"] = [lib, RUBYLIB].join(File::PATH_SEPARATOR)
154154
end
155-
child_env['ASAN_OPTIONS'] = ENV['ASAN_OPTIONS'] if ENV['ASAN_OPTIONS']
155+
156+
# remain env
157+
%w(ASAN_OPTIONS RUBY_ON_BUG).each{|name|
158+
child_env[name] = ENV[name] if ENV[name]
159+
}
160+
156161
args = [args] if args.kind_of?(String)
157162
pid = spawn(child_env, *precommand, rubybin, *args, opt)
158163
in_c.close

0 commit comments

Comments
 (0)