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

Commit 9de11e5

Browse files
authored
Merge pull request #2903 from zinovyev/patch-1
Do not fail if an Exception has no backtrace
2 parents beca9ba + c30348c commit 9de11e5

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Enhancements:
1010
Bug fixes:
1111

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

1416
### 3.10.1 / 2020-12-27
1517
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.10.0...v3.10.1)

lib/rspec/core/formatters/exception_presenter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def formatted_cause(exception)
5555
cause << " #{line}"
5656
end
5757

58-
unless last_cause.backtrace.empty?
58+
unless last_cause.backtrace.nil? || last_cause.backtrace.empty?
5959
cause << (" #{backtrace_formatter.format_backtrace(last_cause.backtrace, example.metadata).first}")
6060
end
6161
end

spec/rspec/core/formatters/exception_presenter_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,42 @@ def initialize(message, backtrace = [], cause = nil)
202202
EOS
203203
end
204204

205+
context "when the first exception doesn't have a backgrace" do
206+
let(:first_exception) { FakeException.new("Real\nculprit", backtrace) }
207+
208+
shared_examples 'expected result for the case when there is no backtrace' do
209+
it 'wont fail for the exception with a nil backtrace', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
210+
the_presenter = Formatters::ExceptionPresenter.new(the_exception, example)
211+
212+
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
213+
|
214+
| 1) Example
215+
| Failure/Error: # The failure happened here!#{ encoding_check }
216+
|
217+
| Boom
218+
| Bam
219+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
220+
| # ------------------
221+
| # --- Caused by: ---
222+
| # Real
223+
| # culprit
224+
EOS
225+
end
226+
end
227+
228+
context 'when backtrace is []' do
229+
let(:backtrace) { [] }
230+
231+
it_behaves_like 'expected result for the case when there is no backtrace'
232+
end
233+
234+
context 'when backtrace is nil' do
235+
let(:backtrace) { nil }
236+
237+
it_behaves_like 'expected result for the case when there is no backtrace'
238+
end
239+
end
240+
205241
it 'wont produce a stack error when cause is the exception itself', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
206242
allow(the_exception).to receive(:cause) { the_exception }
207243
the_presenter = Formatters::ExceptionPresenter.new(the_exception, example)

0 commit comments

Comments
 (0)