Skip to content

Commit 6eda06d

Browse files
committed
Update the latest test library from ruby/ruby
1 parent e552e5a commit 6eda06d

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

Rakefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ Rake::TestTask.new(:test) do |t|
77
t.test_files = FileList["test/**/test_*.rb"]
88
end
99

10+
task :sync_tool do
11+
require 'fileutils'
12+
FileUtils.cp "../ruby/tool/lib/test/unit/core_assertions.rb", "./test/lib"
13+
FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
14+
FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
15+
end
16+
1017
task :default => :test

test/lib/core_assertions.rb

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ def assert_nothing_raised(*args)
194194
end
195195
if ((args.empty? && !as) ||
196196
args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
197-
msg = message(msg) { "Exception raised:\n<#{mu_pp(e)}>" }
197+
msg = message(msg) {
198+
"Exception raised:\n<#{mu_pp(e)}>\n" +
199+
"Backtrace:\n" +
200+
e.backtrace.map{|frame| " #{frame}"}.join("\n")
201+
}
198202
raise MiniTest::Assertion, msg.call, bt
199203
else
200204
raise
@@ -287,13 +291,15 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
287291
args = args.dup
288292
args.insert((Hash === args.first ? 1 : 0), "-w", "--disable=gems", *$:.map {|l| "-I#{l}"})
289293
stdout, stderr, status = EnvUtil.invoke_ruby(args, src, capture_stdout, true, **opt)
294+
ensure
290295
if res_c
291296
res_c.close
292297
res = res_p.read
293298
res_p.close
294299
else
295300
res = stdout
296301
end
302+
raise if $!
297303
abort = status.coredump? || (status.signaled? && ABORT_SIGNALS.include?(status.termsig))
298304
assert(!abort, FailDesc[status, nil, stderr])
299305
self._assertions += res[/^assertions=(\d+)/, 1].to_i
@@ -450,6 +456,49 @@ def assert_raise_with_message(exception, expected, msg = nil, &block)
450456
ex
451457
end
452458

459+
# pattern_list is an array which contains regexp and :*.
460+
# :* means any sequence.
461+
#
462+
# pattern_list is anchored.
463+
# Use [:*, regexp, :*] for non-anchored match.
464+
def assert_pattern_list(pattern_list, actual, message=nil)
465+
rest = actual
466+
anchored = true
467+
pattern_list.each_with_index {|pattern, i|
468+
if pattern == :*
469+
anchored = false
470+
else
471+
if anchored
472+
match = /\A#{pattern}/.match(rest)
473+
else
474+
match = pattern.match(rest)
475+
end
476+
unless match
477+
msg = message(msg) {
478+
expect_msg = "Expected #{mu_pp pattern}\n"
479+
if /\n[^\n]/ =~ rest
480+
actual_mesg = +"to match\n"
481+
rest.scan(/.*\n+/) {
482+
actual_mesg << ' ' << $&.inspect << "+\n"
483+
}
484+
actual_mesg.sub!(/\+\n\z/, '')
485+
else
486+
actual_mesg = "to match " + mu_pp(rest)
487+
end
488+
actual_mesg << "\nafter #{i} patterns with #{actual.length - rest.length} characters"
489+
expect_msg + actual_mesg
490+
}
491+
assert false, msg
492+
end
493+
rest = match.post_match
494+
anchored = true
495+
end
496+
}
497+
if anchored
498+
assert_equal("", rest)
499+
end
500+
end
501+
453502
def assert_warning(pat, msg = nil)
454503
result = nil
455504
stderr = EnvUtil.with_default_internal(pat.encoding) {

test/lib/envutil.rb

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,20 @@ def terminate(pid, signal = :TERM, pgroup = nil, reprieve = 1)
8686
when nil, false
8787
pgroup = pid
8888
end
89+
90+
lldb = true if /darwin/ =~ RUBY_PLATFORM
91+
8992
while signal = signals.shift
93+
94+
if lldb and [:ABRT, :KILL].include?(signal)
95+
lldb = false
96+
# sudo -n: --non-interactive
97+
# lldb -p: attach
98+
# -o: run command
99+
system(*%W[sudo -n lldb -p #{pid} --batch -o bt\ all -o call\ rb_vmdebug_stack_dump_all_threads() -o quit])
100+
true
101+
end
102+
90103
begin
91104
Process.kill signal, pgroup
92105
rescue Errno::EINVAL
@@ -100,6 +113,8 @@ def terminate(pid, signal = :TERM, pgroup = nil, reprieve = 1)
100113
begin
101114
Timeout.timeout(reprieve) {Process.wait(pid)}
102115
rescue Timeout::Error
116+
else
117+
break
103118
end
104119
end
105120
end
@@ -137,8 +152,10 @@ def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr =
137152
args = [args] if args.kind_of?(String)
138153
pid = spawn(child_env, *precommand, rubybin, *args, **opt)
139154
in_c.close
140-
out_c.close if capture_stdout
141-
err_c.close if capture_stderr && capture_stderr != :merge_to_stdout
155+
out_c&.close
156+
out_c = nil
157+
err_c&.close
158+
err_c = nil
142159
if block_given?
143160
return yield in_p, out_p, err_p, pid
144161
else
@@ -242,15 +259,23 @@ def with_default_internal(enc)
242259

243260
def labeled_module(name, &block)
244261
Module.new do
245-
singleton_class.class_eval {define_method(:to_s) {name}; alias inspect to_s}
262+
singleton_class.class_eval {
263+
define_method(:to_s) {name}
264+
alias inspect to_s
265+
alias name to_s
266+
}
246267
class_eval(&block) if block
247268
end
248269
end
249270
module_function :labeled_module
250271

251272
def labeled_class(name, superclass = Object, &block)
252273
Class.new(superclass) do
253-
singleton_class.class_eval {define_method(:to_s) {name}; alias inspect to_s}
274+
singleton_class.class_eval {
275+
define_method(:to_s) {name}
276+
alias inspect to_s
277+
alias name to_s
278+
}
254279
class_eval(&block) if block
255280
end
256281
end

0 commit comments

Comments
 (0)