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

Commit 236405c

Browse files
authored
Merge pull request #3046 from davidtaylorhq/full-cause-backtrace
Introduce `full_cause_backtrace` configuration
2 parents 3bde989 + fd2c2f5 commit 236405c

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

Changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Enhancements:
1212
* Allow specifying custom ordering strategies via `--order`. (Jon Rowe, #3025)
1313
* Use the improved `syntax_suggest` output for `SyntaxError` when available.
1414
(Richard Schneeman, #3015, #3026)
15+
* Add `full_cause_backtrace` config option to print the entire cause backtrace.
16+
(David Taylor, #3046)
1517

1618
Bug fixes:
1719

lib/rspec/core/configuration.rb

+7
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@ def shared_context_metadata_behavior=(value)
458458
# return [Integer]
459459
add_setting :max_displayed_failure_line_count
460460

461+
# @macro full_cause_backtrace
462+
# Display the full backtrace of causing exceptions
463+
# (default false).
464+
# return [Boolean]
465+
add_setting :full_cause_backtrace
466+
461467
# @macro add_setting
462468
# Format the output for pending examples. Can be set to:
463469
# - :full (default) - pending examples appear similarly to failures
@@ -571,6 +577,7 @@ def initialize
571577
@derived_metadata_blocks = FilterableItemRepository::QueryOptimized.new(:any?)
572578
@threadsafe = true
573579
@max_displayed_failure_line_count = 10
580+
@full_cause_backtrace = false
574581
@world = World::Null
575582
@shared_context_metadata_behavior = :trigger_inclusion
576583
@pending_failure_output = :full

lib/rspec/core/formatters/exception_presenter.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ def formatted_cause(exception)
5656
end
5757

5858
unless last_cause.backtrace.nil? || last_cause.backtrace.empty?
59-
cause << (" #{backtrace_formatter.format_backtrace(last_cause.backtrace, example.metadata).first}")
59+
lines = backtrace_formatter.format_backtrace(last_cause.backtrace, example.metadata)
60+
lines = [lines[0]] unless RSpec.configuration.full_cause_backtrace # rubocop:disable Metrics/BlockNesting
61+
62+
lines.each do |line|
63+
cause << (" #{line}")
64+
end
6065
end
6166
end
6267

spec/rspec/core/formatters/exception_presenter_spec.rb

+27-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def initialize(message, backtrace = [], cause = nil)
181181
end
182182

183183
caused_by_line_num = __LINE__ + 1
184-
let(:first_exception) { FakeException.new("Real\nculprit", ["#{__FILE__}:#{__LINE__}"]) }
184+
let(:first_exception) { FakeException.new("Real\nculprit", ["#{__FILE__}:#{__LINE__}", "#{__FILE__}:#{__LINE__}"]) }
185185

186186
it 'includes the first exception that caused the failure', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
187187
the_presenter = Formatters::ExceptionPresenter.new(the_exception, example)
@@ -202,6 +202,32 @@ def initialize(message, backtrace = [], cause = nil)
202202
EOS
203203
end
204204

205+
context 'with RSpec.configuration.full_cause_backtrace enabled' do
206+
before do
207+
RSpec.configuration.full_cause_backtrace = true
208+
end
209+
210+
it 'prints full cause backtrace', :if => RSpec::Support::RubyFeatures.supports_exception_cause? do
211+
the_presenter = Formatters::ExceptionPresenter.new(the_exception, example)
212+
213+
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
214+
|
215+
| 1) Example
216+
| Failure/Error: # The failure happened here!#{ encoding_check }
217+
|
218+
| Boom
219+
| Bam
220+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
221+
| # ------------------
222+
| # --- Caused by: ---
223+
| # Real
224+
| # culprit
225+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{caused_by_line_num}
226+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{caused_by_line_num}
227+
EOS
228+
end
229+
end
230+
205231
context "when the first exception doesn't have a backgrace" do
206232
let(:first_exception) { FakeException.new("Real\nculprit", backtrace) }
207233

0 commit comments

Comments
 (0)