Skip to content
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

For recent Ruby, show nested errors properly in backtraces #727

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/minitest.rb
Expand Up @@ -836,8 +836,24 @@ def error # :nodoc:
end

def message # :nodoc:
bt = Minitest.filter_backtrace(self.backtrace).join "\n "
"#{self.exception.class}: #{self.exception.message}\n #{bt}"
if self.exception.respond_to?(:cause) && self.exception.cause
# Build a chain of exception causes starting from the outermost
exc = self.exception
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been renamed to error with 5802aa0

cause_chain = []
while exc
cause_chain.push(exc)
exc = exc.cause
end

bt_lines = cause_chain.map { |c|
[c.message] + Minitest.filter_backtrace(c.backtrace)
}.inject() { |acc, bt| acc + ["...Caused By..."] + bt }
bt_out = bt_lines.join "\n "
"#{self.exception.class}: #{self.exception.message}\n #{bt_out}"
else
bt = Minitest.filter_backtrace(self.backtrace).join "\n "
"#{self.exception.class}: #{self.exception.message}\n #{bt}"
end
end

def result_label # :nodoc:
Expand Down
34 changes: 34 additions & 0 deletions test/minitest/test_minitest_test.rb
Expand Up @@ -263,6 +263,40 @@ def test_error
assert_report expected
end

def test_run_nested_error
@tu =
Class.new FakeNamedTest do
def test_something
assert true
end

def test_error
begin
raise "internal exception"
rescue
raise "external exception"
end
end
end

expected = clean <<-EOM
E.

Finished in 0.00

1) Error:
FakeNamedTestXX#test_error:
RuntimeError: external exception
FILE:LINE:in \`rescue in test_error\'
FILE:LINE:in \`test_error\'
FILE:LINE:in \`test_error\'

2 runs, 1 assertions, 0 failures, 1 errors, 0 skips
EOM

assert_report expected
end

def test_run_error_teardown
@tu =
Class.new FakeNamedTest do
Expand Down