Skip to content

Commit 24fb155

Browse files
JonRowethomashart
authored andcommitted
improve recursion detection
1 parent fcf0cf0 commit 24fb155

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lib/rspec/core/formatters/exception_presenter.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ def fully_formatted_lines(failure_number, colorizer)
9292

9393
private
9494

95-
def final_exception(exception)
96-
if exception.cause && exception != exception.cause
97-
final_exception(exception.cause)
95+
def final_exception(exception, previous=[])
96+
cause = exception.cause
97+
if cause && !previous.include?(cause)
98+
previous << cause
99+
final_exception(cause, previous)
98100
else
99101
exception
100102
end

spec/rspec/core/formatters/exception_presenter_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ module RSpec::Core
179179
EOS
180180
end
181181

182+
it 'wont produce a stack error when the cause is an older exception', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
183+
allow(the_exception).to receive(:cause) do
184+
instance_double(Exception, :cause => the_exception, :message => "A loop", :backtrace => the_exception.backtrace)
185+
end
186+
the_presenter = Formatters::ExceptionPresenter.new(the_exception, example)
187+
188+
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
189+
|
190+
| 1) Example
191+
| Failure/Error: # The failure happened here!#{ encoding_check }
192+
|
193+
| Boom
194+
| Bam
195+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
196+
| # ------------------
197+
| # --- Caused by: ---
198+
| # A loop
199+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
200+
EOS
201+
end
202+
182203
it "adds extra failure lines from the example metadata" do
183204
extra_example = example.clone
184205
failure_line = 'http://www.example.com/job_details/123'

0 commit comments

Comments
 (0)