Skip to content

Commit

Permalink
- Support Ruby 3.4's new error message format. (mame)
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//src/minitest/dev/": change = 14061]
  • Loading branch information
zenspider committed Feb 26, 2024
1 parent d00cc0d commit 93597e5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/minitest.rb
Expand Up @@ -960,7 +960,7 @@ def report # :nodoc:
# Represents run failures.

class Assertion < Exception
RE = /in .(?:assert|refute|flunk|pass|fail|raise|must|wont)/ # :nodoc:
RE = /in [`'](?:[^']+[#.])?(?:assert|refute|flunk|pass|fail|raise|must|wont)/ # :nodoc:

def error # :nodoc:
self
Expand Down
9 changes: 6 additions & 3 deletions test/minitest/metametameta.rb
Expand Up @@ -110,12 +110,15 @@ def normalize_output output

if windows? then
output.gsub!(/\[(?:[A-Za-z]:)?[^\]:]+:\d+\]/, "[FILE:LINE]")
output.gsub!(/^(\s+)(?:[A-Za-z]:)?[^:]+:\d+:in/, '\1FILE:LINE:in')
output.gsub!(/^(\s+)(?:[A-Za-z]:)?[^:]+:\d+:in [`']/, '\1FILE:LINE:in \'')
else
output.gsub!(/\[([^\]:]+):\d+\]/) { "[#{file[$1]}:LINE]" }
output.gsub!(/^(\s+)([^:]+):\d+:in/) { "#{$1}#{file[$2]}:LINE:in" }
output.gsub!(/\[([^\]:]+):\d+\]/) { "[#{file[$1]}:LINE]" }
output.gsub!(/^(\s+)([^:]+):\d+:in [`']/) { "#{$1}#{file[$2]}:LINE:in '" }
end

output.gsub!(/in [`']block in (?:([^']+)[#.])?/, "in 'block in")
output.gsub!(/in [`'](?:([^']+)[#.])?/, "in '")

output.gsub!(/( at )[^:]+:\d+/) { "#{$1}[#{file[$2]}:LINE]" } # eval?

output
Expand Down
12 changes: 8 additions & 4 deletions test/minitest/test_minitest_assertions.rb
Expand Up @@ -762,12 +762,13 @@ def test_assert_raises_default_triggered
Class: <SomeError>
Message: <\"blah\">
---Backtrace---
FILE:LINE:in \`block in test_assert_raises_default_triggered\'
FILE:LINE:in \'block in test_assert_raises_default_triggered\'
---------------
EOM

actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
actual.gsub!(RE_LEVELS, "") unless jruby?
actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")

assert_equal expected, actual
end
Expand Down Expand Up @@ -841,12 +842,13 @@ def test_assert_raises_subclass_triggered
Class: <AnError>
Message: <\"some message\">
---Backtrace---
FILE:LINE:in \`block in test_assert_raises_subclass_triggered\'
FILE:LINE:in \'block in test_assert_raises_subclass_triggered\'
---------------
EOM

actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
actual.gsub!(RE_LEVELS, "") unless jruby?
actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")

assert_equal expected.chomp, actual
end
Expand All @@ -863,12 +865,13 @@ def test_assert_raises_triggered_different
Class: <SyntaxError>
Message: <\"icky\">
---Backtrace---
FILE:LINE:in \`block in test_assert_raises_triggered_different\'
FILE:LINE:in \'block in test_assert_raises_triggered_different\'
---------------
EOM

actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
actual.gsub!(RE_LEVELS, "") unless jruby?
actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")

assert_equal expected, actual
end
Expand All @@ -886,12 +889,13 @@ def test_assert_raises_triggered_different_msg
Class: <SyntaxError>
Message: <\"icky\">
---Backtrace---
FILE:LINE:in \`block in test_assert_raises_triggered_different_msg\'
FILE:LINE:in \'block in test_assert_raises_triggered_different_msg\'
---------------
EOM

actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
actual.gsub!(RE_LEVELS, "") unless jruby?
actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")

assert_equal expected.chomp, actual
end
Expand Down
4 changes: 2 additions & 2 deletions test/minitest/test_minitest_mock.rb
Expand Up @@ -728,7 +728,7 @@ def test_stub_NameError
end

exp = jruby? ? /Undefined method nope_nope_nope for '#{self.class}::Time'/ :
/undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
/undefined method [`']nope_nope_nope' for( class)? [`']#{self.class}::Time'/
assert_match exp, e.message
end

Expand Down Expand Up @@ -1083,7 +1083,7 @@ def test_stub_value_block_args_5__break_if_not_passed
end
end
end
exp = /undefined method `write' for nil/
exp = /undefined method [`']write' for nil/
assert_match exp, e.message
end

Expand Down
24 changes: 12 additions & 12 deletions test/minitest/test_minitest_reporter.rb
Expand Up @@ -324,8 +324,8 @@ def test_report_error
1) Error:
Minitest::Test#woot:
RuntimeError: no
FILE:LINE:in `error_test'
FILE:LINE:in `test_report_error'
FILE:LINE:in 'error_test'
FILE:LINE:in 'test_report_error'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
EOM
Expand Down Expand Up @@ -397,7 +397,7 @@ def test_report_skipped
def test_report_failure_uses_backtrace_filter
filter = Minitest::BacktraceFilter.new
def filter.filter _bt
["foo.rb:123:in `foo'"]
["foo.rb:123:in 'foo'"]
end

with_backtrace_filter filter do
Expand All @@ -413,15 +413,15 @@ def filter.filter _bt

def test_report_failure_uses_backtrace_filter_complex_sorbet
backtrace = <<~EOBT
/Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/assertions.rb:183:in `assert'
example_test.rb:9:in `assert_false'
/Users/user/.gem/ruby/3.2.2/gems/sorbet-runtime-0.5.11068/lib/types/private/methods/call_validation.rb:256:in `bind_call'
/Users/user/.gem/ruby/3.2.2/gems/sorbet-runtime-0.5.11068/lib/types/private/methods/call_validation.rb:256:in `validate_call'
/Users/user/.gem/ruby/3.2.2/gems/sorbet-runtime-0.5.11068/lib/types/private/methods/_methods.rb:275:in `block in _on_method_added'
example_test.rb:25:in `test_something'
/Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/test.rb:94:in `block (3 levels) in run'
/Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/test.rb:191:in `capture_exceptions'
/Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/test.rb:89:in `block (2 levels) in run'
/Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/assertions.rb:183:in 'assert'
example_test.rb:9:in 'assert_false'
/Users/user/.gem/ruby/3.2.2/gems/sorbet-runtime-0.5.11068/lib/types/private/methods/call_validation.rb:256:in 'bind_call'
/Users/user/.gem/ruby/3.2.2/gems/sorbet-runtime-0.5.11068/lib/types/private/methods/call_validation.rb:256:in 'validate_call'
/Users/user/.gem/ruby/3.2.2/gems/sorbet-runtime-0.5.11068/lib/types/private/methods/_methods.rb:275:in 'block in _on_method_added'
example_test.rb:25:in 'test_something'
/Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/test.rb:94:in 'block (3 levels) in run'
/Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/test.rb:191:in 'capture_exceptions'
/Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/test.rb:89:in 'block (2 levels) in run'
... so many lines ...
EOBT

Expand Down
32 changes: 16 additions & 16 deletions test/minitest/test_minitest_test.rb
Expand Up @@ -28,24 +28,24 @@ class TestMinitestUnit < MetaMetaMetaTestCase
basedir = Pathname.new(File.expand_path "lib/minitest") + "mini"
basedir = basedir.relative_path_from(pwd).to_s
MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}"
BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
"#{MINITEST_BASE_DIR}/test.rb:158:in `each'",
"#{MINITEST_BASE_DIR}/test.rb:139:in `run'",
"#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]
BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:161:in 'each'",
"#{MINITEST_BASE_DIR}/test.rb:158:in 'each'",
"#{MINITEST_BASE_DIR}/test.rb:139:in 'run'",
"#{MINITEST_BASE_DIR}/test.rb:106:in 'run'"]

def test_filter_backtrace
# this is a semi-lame mix of relative paths.
# I cheated by making the autotest parts not have ./
bt = (["lib/autotest.rb:571:in `add_exception'",
"test/test_autotest.rb:62:in `test_add_exception'",
"#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
bt = (["lib/autotest.rb:571:in 'add_exception'",
"test/test_autotest.rb:62:in 'test_add_exception'",
"#{MINITEST_BASE_DIR}/test.rb:165:in '__send__'"] +
BT_MIDDLE +
["#{MINITEST_BASE_DIR}/test.rb:29",
"test/test_autotest.rb:422"])
bt = util_expand_bt bt

ex = ["lib/autotest.rb:571:in `add_exception'",
"test/test_autotest.rb:62:in `test_add_exception'"]
ex = ["lib/autotest.rb:571:in 'add_exception'",
"test/test_autotest.rb:62:in 'test_add_exception'"]
ex = util_expand_bt ex

Minitest::Test.io_lock.synchronize do # try not to trounce in parallel
Expand All @@ -56,7 +56,7 @@ def test_filter_backtrace
end

def test_filter_backtrace_all_unit
bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in '__send__'"] +
BT_MIDDLE +
["#{MINITEST_BASE_DIR}/test.rb:29"])
ex = bt.clone
Expand All @@ -65,7 +65,7 @@ def test_filter_backtrace_all_unit
end

def test_filter_backtrace_unit_starts
bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in '__send__'"] +
BT_MIDDLE +
["#{MINITEST_BASE_DIR}/mini/test.rb:29",
"-e:1"])
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_this_is_non_ascii_failure_message
2) Error:
FakeNamedTestXX#test_this_is_non_ascii_failure_message:
RuntimeError: ЁЁЁ
FILE:LINE:in `test_this_is_non_ascii_failure_message'
FILE:LINE:in 'test_this_is_non_ascii_failure_message'
2 runs, 1 assertions, 1 failures, 1 errors, 0 skips
EOM
Expand Down Expand Up @@ -263,7 +263,7 @@ def test_error
1) Error:
FakeNamedTestXX#test_error:
RuntimeError: unhandled exception
FILE:LINE:in \`test_error\'
FILE:LINE:in \'test_error\'
2 runs, 1 assertions, 0 failures, 1 errors, 0 skips
EOM
Expand Down Expand Up @@ -291,7 +291,7 @@ def teardown
1) Error:
FakeNamedTestXX#test_something:
RuntimeError: unhandled exception
FILE:LINE:in \`teardown\'
FILE:LINE:in \'teardown\'
1 runs, 1 assertions, 0 failures, 1 errors, 0 skips
EOM
Expand Down Expand Up @@ -1261,12 +1261,12 @@ def teardown
Error:
FakeNamedTestXX#test_method:
AnError: AnError
FILE:LINE:in `test_method'
FILE:LINE:in 'test_method'
Error:
FakeNamedTestXX#test_method:
RuntimeError: unhandled exception
FILE:LINE:in `teardown'
FILE:LINE:in 'teardown'
"

assert_equal exp.strip, normalize_output(first_reporter.results.first.to_s).strip
Expand Down

0 comments on commit 93597e5

Please sign in to comment.