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

Include exception that raised failure in backtrace #2074

Merged
merged 1 commit into from
Oct 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions lib/rspec/core/formatters/exception_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,36 @@ def colorized_message_lines(colorizer=::RSpec::Core::Formatters::ConsoleCodes)
end
end

def formatted_backtrace
backtrace_formatter.format_backtrace(exception_backtrace, example.metadata)
def formatted_backtrace(exception=@exception)
backtrace_formatter.format_backtrace((exception.backtrace || []), example.metadata) +
formatted_cause(exception)
end

if RSpec::Support::RubyFeatures.supports_exception_cause?
def formatted_cause(exception)
last_cause = final_exception(exception)
cause = []

if exception.cause
cause << '------------------'
cause << '--- Caused by: ---'
cause << "#{exception_class_name(last_cause)}:" unless exception_class_name(last_cause) =~ /RSpec/

encoded_string(last_cause.message.to_s).split("\n").each do |line|
cause << " #{line}"
end

cause << (" #{backtrace_formatter.format_backtrace(last_cause.backtrace, example.metadata).first}")
end

cause
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to only take the last cause, why bother generating the string for all of them? It would seem better to loop over the causes until you've found the last, then do the string generation and add the last line directly to lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll update the pr to do the string generation for the last cause only 👍

else
# :nocov:
def formatted_cause(_)
[]
end
# :nocov:
end

def colorized_formatted_backtrace(colorizer=::RSpec::Core::Formatters::ConsoleCodes)
Expand All @@ -56,6 +84,14 @@ def failure_slash_error_line

private

def final_exception(exception)
if exception.cause
final_exception(exception.cause)
else
exception
end
end

def description_and_detail(colorizer, indentation)
detail = detail_formatter.call(example, colorizer, indentation)
return (description || detail) unless description && detail
Expand All @@ -81,7 +117,7 @@ def encoded_string(string)
# :nocov:
end

def exception_class_name
def exception_class_name(exception=@exception)
name = exception.class.name.to_s
name = "(anonymous error class)" if name == ''
name
Expand Down
50 changes: 39 additions & 11 deletions spec/rspec/core/formatters/exception_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module RSpec::Core
before do
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?
end

describe "#fully_formatted" do
Expand Down Expand Up @@ -49,9 +50,9 @@ module RSpec::Core
end

it "allows the caller to specify additional indentation" do
presenter = Formatters::ExceptionPresenter.new(exception, example, :indentation => 4)
the_presenter = Formatters::ExceptionPresenter.new(exception, example, :indentation => 4)

expect(presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 1) Example
| Failure/Error: # The failure happened here!#{ encoding_check }
Expand All @@ -64,9 +65,9 @@ module RSpec::Core
it 'passes the indentation on to the `:detail_formatter` lambda so it can align things' do
detail_formatter = Proc.new { "Some Detail" }

presenter = Formatters::ExceptionPresenter.new(exception, example, :indentation => 4,
the_presenter = Formatters::ExceptionPresenter.new(exception, example, :indentation => 4,
:detail_formatter => detail_formatter)
expect(presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 1) Example
| Some Detail
Expand All @@ -78,11 +79,11 @@ module RSpec::Core
end

it 'allows the caller to omit the description' do
presenter = Formatters::ExceptionPresenter.new(exception, example,
the_presenter = Formatters::ExceptionPresenter.new(exception, example,
:detail_formatter => Proc.new { "Detail!" },
:description_formatter => Proc.new { })

expect(presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 1) Detail!
| Failure/Error: # The failure happened here!#{ encoding_check }
Expand All @@ -93,9 +94,9 @@ module RSpec::Core
end

it 'allows the failure/error line to be used as the description' do
presenter = Formatters::ExceptionPresenter.new(exception, example, :description_formatter => lambda { |p| p.failure_slash_error_line })
the_presenter = Formatters::ExceptionPresenter.new(exception, example, :description_formatter => lambda { |p| p.failure_slash_error_line })

expect(presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 1) Failure/Error: # The failure happened here!#{ encoding_check }
| Boom
Expand All @@ -105,13 +106,13 @@ module RSpec::Core
end

it 'allows a caller to specify extra details that are added to the bottom' do
presenter = Formatters::ExceptionPresenter.new(
the_presenter = Formatters::ExceptionPresenter.new(
exception, example, :extra_detail_formatter => lambda do |failure_number, colorizer, indentation|
"#{indentation}extra detail for failure: #{failure_number}\n"
end
)

expect(presenter.fully_formatted(2)).to eq(<<-EOS.gsub(/^ +\|/, ''))
expect(the_presenter.fully_formatted(2)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 2) Example
| Failure/Error: # The failure happened here!#{ encoding_check }
Expand All @@ -121,8 +122,35 @@ module RSpec::Core
| extra detail for failure: 2
EOS
end
end

let(:the_exception) { instance_double(Exception, :cause => second_exception, :message => "Boom\nBam", :backtrace => [ "#{__FILE__}:#{line_num}"]) }

let(:second_exception) do
instance_double(Exception, :cause => first_exception, :message => "Second\nexception", :backtrace => ["#{__FILE__}:#{__LINE__}"])
end

let(:first_exception) do
instance_double(Exception, :cause => nil, :message => "Real\nculprit", :backtrace => ["#{__FILE__}:#{__LINE__}"])
end

it 'includes the first exception that caused the failure', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
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: ---
| # Real
| # culprit
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:133
EOS
end
end
describe "#read_failed_line" do
def read_failed_line
presenter.send(:read_failed_line)
Expand Down
1 change: 1 addition & 0 deletions spec/rspec/core/notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
end

it 'provides `colorized_formatted_backtrace`, which formats the backtrace and colorizes it' do
allow(exception).to receive(:cause) if RSpec::Support::RubyFeatures.supports_exception_cause?
allow(RSpec.configuration).to receive(:color_enabled?).and_return(true)
expect(notification.colorized_formatted_backtrace).to eq(["\e[36m# #{RSpec::Core::Metadata.relative_path(__FILE__)}:#{exception_line}\e[0m"])
end
Expand Down