Skip to content

Sync with ruby master #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 120 additions & 7 deletions lib/core_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ def assert_file
end

def assert_in_out_err(args, test_stdin = "", test_stdout = [], test_stderr = [], message = nil,
success: nil, **opt)
success: nil, failed: nil, **opt)
args = Array(args).dup
args.insert((Hash === args[0] ? 1 : 0), '--disable=gems')
stdout, stderr, status = EnvUtil.invoke_ruby(args, test_stdin, true, true, **opt)
desc = FailDesc[status, message, stderr]
desc = failed[status, message, stderr] if failed
desc ||= FailDesc[status, message, stderr]
if block_given?
raise "test_stdout ignored, use block only or without block" if test_stdout != []
raise "test_stderr ignored, use block only or without block" if test_stderr != []
Expand Down Expand Up @@ -495,14 +496,13 @@ def assert_raise_with_message(exception, expected, msg = nil, &block)
case expected
when String
assert = :assert_equal
when Regexp
assert = :assert_match
else
raise TypeError, "Expected #{expected.inspect} to be a kind of String or Regexp, not #{expected.class}"
assert_respond_to(expected, :===)
assert = :assert_match
end

ex = m = nil
EnvUtil.with_default_internal(expected.encoding) do
EnvUtil.with_default_internal(of: expected) do
ex = assert_raise(exception, msg || proc {"Exception(#{exception}) with message matches to #{expected.inspect}"}) do
yield
end
Expand All @@ -520,6 +520,43 @@ def assert_raise_with_message(exception, expected, msg = nil, &block)
ex
end

# :call-seq:
# assert_raise_kind_of(*args, &block)
#
#Tests if the given block raises one of the given exceptions or
#sub exceptions of the given exceptions. If the last argument
#is a String, it will be used as the error message.
#
# assert_raise do #Fails, no Exceptions are raised
# end
#
# assert_raise SystemCallErr do
# Dir.chdir(__FILE__) #Raises Errno::ENOTDIR, so assertion succeeds
# end
def assert_raise_kind_of(*exp, &b)
case exp.last
when String, Proc
msg = exp.pop
end

begin
yield
rescue Test::Unit::PendedError => e
raise e unless exp.include? Test::Unit::PendedError
rescue *exp => e
pass
rescue Exception => e
flunk(message(msg) {"#{mu_pp(exp)} family exception expected, not #{mu_pp(e)}"})
ensure
unless e
exp = exp.first if exp.size == 1

flunk(message(msg) {"#{mu_pp(exp)} family expected but nothing was raised"})
end
end
e
end

TEST_DIR = File.join(__dir__, "test/unit") #:nodoc:

# :call-seq:
Expand Down Expand Up @@ -639,7 +676,7 @@ def assert_pattern_list(pattern_list, actual, message=nil)

def assert_warning(pat, msg = nil)
result = nil
stderr = EnvUtil.with_default_internal(pat.encoding) {
stderr = EnvUtil.with_default_internal(of: pat) {
EnvUtil.verbose_warning {
result = yield
}
Expand Down Expand Up @@ -867,6 +904,82 @@ 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

# Platform predicates

def self.mswin?
defined?(@mswin) ? @mswin : @mswin = RUBY_PLATFORM.include?('mswin')
end
private def mswin?
CoreAssertions.mswin?
end

def self.mingw?
defined?(@mingw) ? @mingw : @mingw = RUBY_PLATFORM.include?('mingw')
end
private def mingw?
CoreAssertions.mingw?
end

module_function def windows?
mswin? or mingw?
end

def self.version_compare(expected, actual)
expected.zip(actual).each {|e, a| z = (e <=> a); return z if z.nonzero?}
0
end

def self.version_match?(expected, actual)
if !actual
false
elsif expected.empty?
true
elsif expected.size == 1 and Range === (range = expected.first)
b, e = range.begin, range.end
return false if b and (c = version_compare(Array(b), actual)) > 0
return false if e and (c = version_compare(Array(e), actual)) < 0
return false if e and range.exclude_end? and c == 0
true
else
version_compare(expected, actual).zero?
end
end

def self.linux?(*ver)
unless defined?(@linux)
@linux = RUBY_PLATFORM.include?('linux') && `uname -r`.scan(/\d+/).map(&:to_i)
end
version_match? ver, @linux
end
private def linux?(*ver)
CoreAssertions.linux?(*ver)
end

def self.glibc?(*ver)
unless defined?(@glibc)
libc = `/usr/bin/ldd /bin/sh`[/^\s*libc.*=> *\K\S*/]
if libc and /version (\d+)\.(\d+)\.$/ =~ IO.popen([libc], &:read)[]
@glibc = [$1.to_i, $2.to_i]
else
@glibc = false
end
end
version_match? ver, @glibc
end
private def glibc?(*ver)
CoreAssertions.glibc?(*ver)
end

def self.macos?(*ver)
unless defined?(@macos)
@macos = RUBY_PLATFORM.include?('darwin') && `sw_vers -productVersion`.scan(/\d+/).map(&:to_i)
end
version_match? ver, @macos
end
private def macos?(*ver)
CoreAssertions.macos?(*ver)
end
end
end
end
6 changes: 4 additions & 2 deletions lib/envutil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,17 @@ def without_gc
end
module_function :without_gc

def with_default_external(enc)
def with_default_external(enc = nil, of: nil)
enc = of.encoding if defined?(of.encoding)
suppress_warning { Encoding.default_external = enc }
yield
ensure
suppress_warning { Encoding.default_external = EnvUtil.original_external_encoding }
end
module_function :with_default_external

def with_default_internal(enc)
def with_default_internal(enc = nil, of: nil)
enc = of.encoding if defined?(of.encoding)
suppress_warning { Encoding.default_internal = enc }
yield
ensure
Expand Down