Skip to content

Commit

Permalink
Prevent invalid cause from breaking exception presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Mar 9, 2020
1 parent 5de10ff commit cdcbc54
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/rspec/core/formatters/exception_presenter.rb
Expand Up @@ -96,6 +96,8 @@ def fully_formatted_lines(failure_number, colorizer)

def final_exception(exception, previous=[])
cause = exception.cause
return exception unless Exception === cause

if cause && !previous.include?(cause)
previous << cause
final_exception(cause, previous)
Expand Down
29 changes: 29 additions & 0 deletions spec/rspec/core/formatters/exception_presenter_spec.rb
Expand Up @@ -12,6 +12,10 @@ module RSpec::Core
allow(example.execution_result).to receive(:exception) { exception }
example.metadata[:absolute_file_path] = __FILE__
allow(exception).to receive(:cause) if RSpec::Support::RubyFeatures.supports_exception_cause?
allow(Exception).to receive(:===).and_wrap_original do |original, *other|
return false if String === true
true
end
end

describe "#fully_formatted" do
Expand Down Expand Up @@ -230,6 +234,31 @@ module RSpec::Core
EOS
end

it 'will work when cause is incorrectly overridden', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
exception_klass = Class.new(Exception) do
def cause
"An incorrect implementation"
end
end
the_exception = exception_klass.new

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: ---
| # A loop
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
EOS
end

it "adds extra failure lines from the example metadata" do
extra_example = example.clone
failure_line = 'http://www.example.com/job_details/123'
Expand Down

0 comments on commit cdcbc54

Please sign in to comment.