From cdcbc542a087ad80734a34253d74e5feb8b136ec Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Mon, 9 Mar 2020 18:56:45 +0000 Subject: [PATCH] Prevent invalid cause from breaking exception presenter --- .../core/formatters/exception_presenter.rb | 2 ++ .../formatters/exception_presenter_spec.rb | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/rspec/core/formatters/exception_presenter.rb b/lib/rspec/core/formatters/exception_presenter.rb index 9ad6503e17..5d12646f78 100644 --- a/lib/rspec/core/formatters/exception_presenter.rb +++ b/lib/rspec/core/formatters/exception_presenter.rb @@ -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) diff --git a/spec/rspec/core/formatters/exception_presenter_spec.rb b/spec/rspec/core/formatters/exception_presenter_spec.rb index 347524cc26..a749fb9ce4 100644 --- a/spec/rspec/core/formatters/exception_presenter_spec.rb +++ b/spec/rspec/core/formatters/exception_presenter_spec.rb @@ -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 @@ -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'