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

Commit c56feff

Browse files
zinovyevpirj
authored andcommitted
Do not fail if an Exception has no backtrace
Currently `backtrace` of an exception is checked by the `empty?` method. But it sometimes occur that the Exception class will return `nil` for backtrace: ``` StandardError.new.backtrace.empty? NoMethodError: undefined method `empty?' for nil:NilClass ```
1 parent beca9ba commit c56feff

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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)