Skip to content

Commit 4278b3e

Browse files
authored
Merge pull request #35 from ruby/merge-ruby-core
Backport test library from ruby core directly.
2 parents 27a85db + f067f7d commit 4278b3e

File tree

6 files changed

+84
-61
lines changed

6 files changed

+84
-61
lines changed

test/lib/assertions.rb renamed to test/lib/core_assertions.rb

+33-25
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
# frozen_string_literal: true
2-
require 'test/unit'
3-
require 'pp'
4-
require_relative 'envutil'
52

63
module Test
74
module Unit
8-
module Assertions
9-
def mu_pp(obj) #:nodoc:
10-
obj.pretty_inspect.chomp
11-
end
5+
module CoreAssertions
6+
if defined?(MiniTest)
7+
require_relative '../../envutil'
8+
# for ruby core testing
9+
include MiniTest::Assertions
10+
else
11+
require 'pp'
12+
require_relative 'envutil'
13+
include Test::Unit::Assertions
14+
15+
def _assertions= n # :nodoc:
16+
@_assertions = n
17+
end
18+
19+
def _assertions # :nodoc:
20+
@_assertions ||= 0
21+
end
22+
23+
##
24+
# Returns a proc that will output +msg+ along with the default message.
1225

13-
def _assertions= n # :nodoc:
14-
@_assertions = n
26+
def message msg = nil, ending = nil, &default
27+
proc {
28+
msg = msg.call.chomp(".") if Proc === msg
29+
custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty?
30+
"#{custom_message}#{default.call}#{ending || "."}"
31+
}
32+
end
1533
end
1634

17-
def _assertions # :nodoc:
18-
@_assertions ||= 0
35+
def mu_pp(obj) #:nodoc:
36+
obj.pretty_inspect.chomp
1937
end
2038

2139
def assert_file
2240
AssertFile
2341
end
2442

25-
##
26-
# Returns a proc that will output +msg+ along with the default message.
27-
28-
def message msg = nil, ending = nil, &default
29-
proc {
30-
msg = msg.call.chomp(".") if Proc === msg
31-
custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty?
32-
"#{custom_message}#{default.call}#{ending || "."}"
33-
}
34-
end
35-
3643
FailDesc = proc do |status, message = "", out = ""|
3744
pid = status.pid
3845
now = Time.now
@@ -69,7 +76,7 @@ def message msg = nil, ending = nil, &default
6976
end
7077

7178
def assert_in_out_err(args, test_stdin = "", test_stdout = [], test_stderr = [], message = nil,
72-
success: nil, **opt)
79+
success: nil, **opt)
7380
args = Array(args).dup
7481
args.insert((Hash === args[0] ? 1 : 0), '--disable=gems')
7582
stdout, stderr, status = EnvUtil.invoke_ruby(args, test_stdin, true, true, **opt)
@@ -117,7 +124,7 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
117124
end
118125
src = <<eom
119126
# -*- coding: #{line += __LINE__; src.encoding}; -*-
120-
require #{(__dir__ + "/assertions").dump};include Test::Unit::Assertions
127+
require "test/unit";include Test::Unit::Assertions;require #{(__dir__ + "/core_assertions").dump};include Test::Unit::CoreAssertions
121128
END {
122129
puts [Marshal.dump($!)].pack('m'), "assertions=\#{self._assertions}"
123130
}
@@ -160,7 +167,7 @@ class Test::Unit::Runner
160167
end
161168

162169
class << (AssertFile = Struct.new(:failure_message).new)
163-
include Assertions
170+
include CoreAssertions
164171
def assert_file_predicate(predicate, *args)
165172
if /\Anot_/ =~ predicate
166173
predicate = $'
@@ -229,6 +236,7 @@ def assert_all_assertions(msg = nil)
229236
assert(all.pass?, message(msg) {all.message.chomp(".")})
230237
end
231238
alias all_assertions assert_all_assertions
239+
232240
end
233241
end
234242
end

test/lib/envutil.rb

+45-32
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def rubybin
4545
RUBYLIB = ENV["RUBYLIB"]
4646

4747
class << self
48-
attr_accessor :subprocess_timeout_scale
48+
attr_accessor :timeout_scale
4949
attr_reader :original_internal_encoding, :original_external_encoding,
5050
:original_verbose
5151

@@ -57,22 +57,63 @@ def capture_global_values
5757
end
5858

5959
def apply_timeout_scale(t)
60-
if scale = EnvUtil.subprocess_timeout_scale
60+
if scale = EnvUtil.timeout_scale
6161
t * scale
6262
else
6363
t
6464
end
6565
end
6666
module_function :apply_timeout_scale
6767

68+
def timeout(sec, klass = nil, message = nil, &blk)
69+
return yield(sec) if sec == nil or sec.zero?
70+
sec = apply_timeout_scale(sec)
71+
Timeout.timeout(sec, klass, message, &blk)
72+
end
73+
module_function :timeout
74+
75+
def terminate(pid, signal = :TERM, pgroup = nil, reprieve = 1)
76+
reprieve = apply_timeout_scale(reprieve) if reprieve
77+
78+
signals = Array(signal).select do |sig|
79+
DEFAULT_SIGNALS[sig.to_s] or
80+
DEFAULT_SIGNALS[Signal.signame(sig)] rescue false
81+
end
82+
signals |= [:ABRT, :KILL]
83+
case pgroup
84+
when 0, true
85+
pgroup = -pid
86+
when nil, false
87+
pgroup = pid
88+
end
89+
while signal = signals.shift
90+
begin
91+
Process.kill signal, pgroup
92+
rescue Errno::EINVAL
93+
next
94+
rescue Errno::ESRCH
95+
break
96+
end
97+
if signals.empty? or !reprieve
98+
Process.wait(pid)
99+
else
100+
begin
101+
Timeout.timeout(reprieve) {Process.wait(pid)}
102+
rescue Timeout::Error
103+
end
104+
end
105+
end
106+
$?
107+
end
108+
module_function :terminate
109+
68110
def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr = false,
69111
encoding: nil, timeout: 10, reprieve: 1, timeout_error: Timeout::Error,
70112
stdout_filter: nil, stderr_filter: nil,
71113
signal: :TERM,
72114
rubybin: EnvUtil.rubybin, precommand: nil,
73115
**opt)
74116
timeout = apply_timeout_scale(timeout)
75-
reprieve = apply_timeout_scale(reprieve) if reprieve
76117

77118
in_c, in_p = IO.pipe
78119
out_p, out_c = IO.pipe if capture_stdout
@@ -108,35 +149,7 @@ def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr =
108149
if (!th_stdout || th_stdout.join(timeout)) && (!th_stderr || th_stderr.join(timeout))
109150
timeout_error = nil
110151
else
111-
signals = Array(signal).select do |sig|
112-
DEFAULT_SIGNALS[sig.to_s] or
113-
DEFAULT_SIGNALS[Signal.signame(sig)] rescue false
114-
end
115-
signals |= [:ABRT, :KILL]
116-
case pgroup = opt[:pgroup]
117-
when 0, true
118-
pgroup = -pid
119-
when nil, false
120-
pgroup = pid
121-
end
122-
while signal = signals.shift
123-
begin
124-
Process.kill signal, pgroup
125-
rescue Errno::EINVAL
126-
next
127-
rescue Errno::ESRCH
128-
break
129-
end
130-
if signals.empty? or !reprieve
131-
Process.wait(pid)
132-
else
133-
begin
134-
Timeout.timeout(reprieve) {Process.wait(pid)}
135-
rescue Timeout::Error
136-
end
137-
end
138-
end
139-
status = $?
152+
status = terminate(pid, signal, opt[:pgroup], reprieve)
140153
end
141154
stdout = th_stdout.value if capture_stdout
142155
stderr = th_stderr.value if capture_stderr && capture_stderr != :merge_to_stdout

test/helper.rb renamed to test/logger/helper.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
$LOAD_PATH.unshift File.join(ROOT_DIR, 'test', 'lib') # to use custom test-unit in this repo
44
require 'logger'
55
require 'test/unit'
6-
require 'assertions'
6+
require 'core_assertions'
7+
8+
Test::Unit::TestCase.include Test::Unit::CoreAssertions

test/logger/test_logdevice.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: US-ASCII
22
# frozen_string_literal: false
3-
require_relative '../helper'
3+
require_relative 'helper'
44
require 'tempfile'
55
require 'tmpdir'
66

test/logger/test_logger.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: US-ASCII
22
# frozen_string_literal: false
3-
require_relative '../helper'
3+
require_relative 'helper'
44
require 'tempfile'
55

66
class TestLogger < Test::Unit::TestCase

test/logger/test_severity.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: US-ASCII
22
# frozen_string_literal: false
3-
require_relative '../helper'
3+
require_relative 'helper'
44

55
class TestLoggerSeverity < Test::Unit::TestCase
66
def test_enum

0 commit comments

Comments
 (0)