Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Do not fail if an Exception has no backtrace #2903

Merged
merged 2 commits into from
Jul 16, 2021
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
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Enhancements:
Bug fixes:

* Ensure bisect communication uses consistent encoding. (Mike Jarema, #2852)
* Fix exception presenter when the root cause exception has nil backtrace.
(Zinovyev Ivan, #2903)

### 3.10.1 / 2020-12-27
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.10.0...v3.10.1)
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/formatters/exception_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def formatted_cause(exception)
cause << " #{line}"
end

unless last_cause.backtrace.empty?
unless last_cause.backtrace.nil? || last_cause.backtrace.empty?
cause << (" #{backtrace_formatter.format_backtrace(last_cause.backtrace, example.metadata).first}")
end
end
Expand Down
36 changes: 36 additions & 0 deletions spec/rspec/core/formatters/exception_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,42 @@ def initialize(message, backtrace = [], cause = nil)
EOS
end

context "when the first exception doesn't have a backgrace" do
let(:first_exception) { FakeException.new("Real\nculprit", backtrace) }

shared_examples 'expected result for the case when there is no backtrace' do
it 'wont fail for the exception with a nil backtrace', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
the_presenter = Formatters::ExceptionPresenter.new(the_exception, example)

expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 1) Example
| Failure/Error: # The failure happened here!#{ encoding_check }
|
| Boom
| Bam
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
| # ------------------
| # --- Caused by: ---
| # Real
| # culprit
EOS
end
end

context 'when backtrace is []' do
let(:backtrace) { [] }

it_behaves_like 'expected result for the case when there is no backtrace'
end

context 'when backtrace is nil' do
let(:backtrace) { nil }

it_behaves_like 'expected result for the case when there is no backtrace'
end
end

it 'wont produce a stack error when cause is the exception itself', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
allow(the_exception).to receive(:cause) { the_exception }
the_presenter = Formatters::ExceptionPresenter.new(the_exception, example)
Expand Down