Skip to content

Commit

Permalink
Fix BacktraceFormatter to handle nil backtraces.
Browse files Browse the repository at this point in the history
`Exception.new.backtrace` is `nil` so we can't count on
there always being a backtrace. Before now we relied
on the `format_backtrace` caller handling this (passing
`ex.backtrace || []`), but with #2074 now also including
the `exception.cause` in failure output, the code there
did not guard the `format_backtrace` call with `|| []`
and is causing users to get a `NoMethodError`. It's
easiest if `format_backtrace just handles the `nil` case
so each caller does not have to remember to handle it.
  • Loading branch information
myronmarston committed Nov 18, 2015
1 parent 2b0539d commit 52e3dbb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/rspec/core/backtrace_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def filter_gem(gem_name)
end

def format_backtrace(backtrace, options={})
return [] unless backtrace
return backtrace if options[:full_backtrace] || backtrace.empty?

backtrace.map { |l| backtrace_line(l) }.compact.
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/formatters/exception_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def colorized_message_lines(colorizer=::RSpec::Core::Formatters::ConsoleCodes)
end

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

Expand Down
12 changes: 11 additions & 1 deletion spec/rspec/core/backtrace_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,23 @@ def make_backtrace_formatter(exclusion_patterns=nil, inclusion_patterns=nil)
end
end

describe "an empty backtrace" do
describe "for an empty backtrace" do
it "does not add the explanatory message about backtrace filtering" do
formatter = BacktraceFormatter.new
expect(formatter.format_backtrace([])).to eq([])
end
end

describe "for a `nil` backtrace (since exceptions can have no backtrace!)" do
it 'returns a blank array, with no explanatory message' do
exception = Exception.new
expect(exception.backtrace).to be_nil

formatter = BacktraceFormatter.new
expect(formatter.format_backtrace(exception.backtrace)).to eq([])
end
end

context "when rspec is installed in the current working directory" do
it "excludes lines from rspec libs by default", :unless => RSpec::Support::OS.windows? do
backtrace = [
Expand Down

0 comments on commit 52e3dbb

Please sign in to comment.