Skip to content

Commit fcf0cf0

Browse files
JonRowethomashart
authored andcommitted
prevent possible infinite recursion
1 parent a138972 commit fcf0cf0

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/rspec/core/formatters/exception_presenter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def fully_formatted_lines(failure_number, colorizer)
9393
private
9494

9595
def final_exception(exception)
96-
if exception.cause
96+
if exception.cause && exception != exception.cause
9797
final_exception(exception.cause)
9898
else
9999
exception

spec/rspec/core/formatters/exception_presenter_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ module RSpec::Core
159159
EOS
160160
end
161161

162+
it 'wont produce a stack error when cause is the exception itself', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
163+
allow(the_exception).to receive(:cause) { the_exception }
164+
the_presenter = Formatters::ExceptionPresenter.new(the_exception, example)
165+
166+
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
167+
|
168+
| 1) Example
169+
| Failure/Error: # The failure happened here!#{ encoding_check }
170+
|
171+
| Boom
172+
| Bam
173+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
174+
| # ------------------
175+
| # --- Caused by: ---
176+
| # Boom
177+
| # Bam
178+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
179+
EOS
180+
end
181+
162182
it "adds extra failure lines from the example metadata" do
163183
extra_example = example.clone
164184
failure_line = 'http://www.example.com/job_details/123'

0 commit comments

Comments
 (0)