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

Commit a41b7b3

Browse files
committed
Fix BacktraceFormatter to handle nil backtraces.
`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. Fixes #2117.
1 parent 2b0539d commit a41b7b3

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

lib/rspec/core/backtrace_formatter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def filter_gem(gem_name)
3131
end
3232

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

3637
backtrace.map { |l| backtrace_line(l) }.compact.

lib/rspec/core/formatters/exception_presenter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def colorized_message_lines(colorizer=::RSpec::Core::Formatters::ConsoleCodes)
3535
end
3636

3737
def formatted_backtrace(exception=@exception)
38-
backtrace_formatter.format_backtrace((exception.backtrace || []), example.metadata) +
38+
backtrace_formatter.format_backtrace(exception.backtrace, example.metadata) +
3939
formatted_cause(exception)
4040
end
4141

spec/rspec/core/backtrace_formatter_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,23 @@ def make_backtrace_formatter(exclusion_patterns=nil, inclusion_patterns=nil)
132132
end
133133
end
134134

135-
describe "an empty backtrace" do
135+
describe "for an empty backtrace" do
136136
it "does not add the explanatory message about backtrace filtering" do
137137
formatter = BacktraceFormatter.new
138138
expect(formatter.format_backtrace([])).to eq([])
139139
end
140140
end
141141

142+
describe "for a `nil` backtrace (since exceptions can have no backtrace!)" do
143+
it 'returns a blank array, with no explanatory message' do
144+
exception = Exception.new
145+
expect(exception.backtrace).to be_nil
146+
147+
formatter = BacktraceFormatter.new
148+
expect(formatter.format_backtrace(exception.backtrace)).to eq([])
149+
end
150+
end
151+
142152
context "when rspec is installed in the current working directory" do
143153
it "excludes lines from rspec libs by default", :unless => RSpec::Support::OS.windows? do
144154
backtrace = [

0 commit comments

Comments
 (0)