Skip to content
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
57 changes: 57 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: CI

on:
- push
- pull_request

jobs:
ruby-versions:
uses: ruby/actions/.github/workflows/ruby_versions.yml@master
with:
engine: cruby
min_version: 2.3

host:
needs: ruby-versions
name: ${{ matrix.os }} ${{ matrix.ruby }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
os:
- ubuntu-latest
- macos-latest
- windows-latest
include:
- { os: macos-15-intel, ruby: 2.3 }
- { os: macos-15-intel, ruby: 2.4 }
- { os: macos-15-intel, ruby: 2.5 }
- { os: windows-latest, ruby: mingw }
- { os: windows-latest, ruby: mswin }
exclude:
- { os: macos-latest, ruby: 2.3 }
- { os: macos-latest, ruby: 2.4 }
- { os: macos-latest, ruby: 2.5 }
- { os: windows-latest, ruby: head }

steps:
- uses: actions/checkout@v7

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- run: bundle exec rake test

- run: bundle exec rake build

- uses: actions/upload-artifact@v7
if: >-
matrix.os == 'ubuntu-latest' &&
(matrix.ruby == needs.ruby-versions.outputs.latest)
with:
name: gem
path: pkg/
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new do |test|
test.libs << "lib"
test.pattern = "test/**/test_*.rb"
end

task default: :test

task :sync_tool do
require 'fileutils'
Expand Down
10 changes: 6 additions & 4 deletions lib/core_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def filter bt

unless $DEBUG then
bt.each do |line|
break if pattern.match?(line)
break if pattern =~ line
new_bt << line
end

new_bt = bt.reject { |line| pattern.match?(line) } if new_bt.empty?
new_bt = bt.reject { |line| pattern =~ line } if new_bt.empty?
new_bt = bt.dup if new_bt.empty?
else
new_bt = bt.dup
Expand Down Expand Up @@ -327,8 +327,8 @@ def separated_runner(token, out = nil)
at_exit {
assertions = assertions_ivar_get.call(:@_assertions)
out_write.call <<~OUT
<error id="#{token}" assertions=#{integer_to_s.bind_call(assertions)}>
#{array_pack.bind_call([marshal_dump.call($!)], 'm0')}
<error id="#{token}" assertions=#{integer_to_s.bind(assertions).call}>
#{array_pack.bind([marshal_dump.call($!)]).call('m0')}
</error id="#{token}">
OUT
}
Expand Down Expand Up @@ -364,6 +364,8 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
args = args.dup
args.insert((Hash === args.first ? 1 : 0), "-w", "--disable=gems", *$:.map {|l| "-I#{l}"})
args << "--debug" if RUBY_ENGINE == 'jruby' # warning: tracing (e.g. set_trace_func) will not capture all events without --debug flag
# power_assert 3 requires ruby 3.1 or later
args << "-W:no-experimental" if ("2.7."..."3.1.").cover?(RUBY_VERSION)
stdout, stderr, status = EnvUtil.invoke_ruby(args, src, capture_stdout, true, **opt)

if sanitizers&.lsan_enabled?
Expand Down
46 changes: 46 additions & 0 deletions test/test_core_assertions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require "test/unit"
require "core_assertions"

class TestCoreAssertions < Test::Unit::TestCase
include Test::Unit::CoreAssertions

def test_backtrace_filter_handles_missing_backtrace
assert_equal(["No backtrace"], Test.filter_backtrace(nil))
end

def test_backtrace_filter_removes_internal_entries
backtrace = [
"/tmp/example.rb:1:in `run'",
"/tmp/lib/test/unit.rb:2:in `assert'",
]

assert_equal([backtrace.first], Test.filter_backtrace(backtrace))
end

def test_message_adds_sentence_endings
object = Object.new
object.extend(Test::Unit::Assertions)

message = object.message("details") { "default" }

assert_equal("details.\ndefault.", message.call)
end

def test_assert_separately_runs_assertions_in_child_ruby
assert_separately([], <<~RUBY)
assert_equal(4, 2 + 2)
RUBY
end

def test_assert_separately_propagates_child_failure
error = assert_raise(Test::Unit::AssertionFailedError) do
assert_separately([], <<~RUBY)
assert_equal(:expected, :actual)
RUBY
end

assert_match(/expected/, error.message)
end
end
30 changes: 30 additions & 0 deletions test/test_envutil.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "test/unit"
require "envutil"

class TestEnvUtil < Test::Unit::TestCase
def test_rubybin_points_to_a_ruby_executable
assert(File.executable?(EnvUtil.rubybin))
end

def test_apply_timeout_scale
original_scale = EnvUtil.timeout_scale
EnvUtil.timeout_scale = 2.5

assert_equal(5.0, EnvUtil.apply_timeout_scale(2))
ensure
EnvUtil.timeout_scale = original_scale
end

def test_invoke_ruby_captures_output_and_status
stdout, stderr, status = EnvUtil.invoke_ruby(
["-e", "STDOUT.print('out'); STDERR.print('err')"],
"", true, true
)

assert_equal("out", stdout)
assert_equal("err", stderr)
assert_predicate(status, :success?)
end
end
26 changes: 26 additions & 0 deletions test/test_find_executable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "test/unit"
require "rbconfig"
require "find_executable"

class TestFindExecutable < Test::Unit::TestCase
def test_find_executable_returns_command_and_arguments
ruby = RbConfig.ruby
command = File.basename(ruby, RbConfig::CONFIG["EXEEXT"])
original_path = ENV["PATH"]
ENV["PATH"] = File.dirname(ruby)

found = EnvUtil.find_executable(command, "--version") do |output|
output.start_with?("ruby ")
end

assert_equal([ruby, "--version"], found)
ensure
ENV["PATH"] = original_path
end

def test_find_executable_returns_nil_for_unknown_command
assert_nil(EnvUtil.find_executable("test-unit-ruby-core-missing") { true })
end
end
23 changes: 23 additions & 0 deletions test/test_memory_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "test/unit"
require "memory_status"

class TestMemoryStatus < Test::Unit::TestCase
def setup
omit("memory status is unsupported") unless defined?(Memory::Status)
end

def test_status_reports_numeric_values
status = Memory::Status.new

assert(status.members.any? { |member| status[member].to_i > 0 })
assert_match(/\A\{[^}]+:\d+(?:,[^}]+:\d+)*\}\z/, status.to_s)
end

def test_parse_round_trips_status
status = Memory::Status.new

assert_equal(status, Memory::Status.parse(status.to_s))
end
end