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

Commit fed7c4c

Browse files
committed
Refactor handling of the first line of exception report
It allows detail or failure/error line to be used as the first line by omitting preceding lines, rather than specifying content of detail or failure/error as description.
1 parent 13959f0 commit fed7c4c

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

lib/rspec/core/formatters/exception_presenter.rb

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ def initialize(exception, example, options={})
1212
@exception = exception
1313
@example = example
1414
@message_color = options.fetch(:message_color) { RSpec.configuration.failure_color }
15-
@description = options.fetch(:description_formatter) { Proc.new { example.full_description } }.call(self)
15+
@description = options.fetch(:description) { example.full_description }
1616
@detail_formatter = options.fetch(:detail_formatter) { Proc.new {} }
1717
@extra_detail_formatter = options.fetch(:extra_detail_formatter) { Proc.new {} }
1818
@backtrace_formatter = options.fetch(:backtrace_formatter) { RSpec.configuration.backtrace_formatter }
1919
@indentation = options.fetch(:indentation, 2)
2020
@skip_shared_group_trace = options.fetch(:skip_shared_group_trace, false)
2121
@failure_lines = options[:failure_lines]
22-
@extra_failure_lines = Array(example.metadata[:extra_failure_lines])
2322
end
2423

2524
def message_lines
@@ -88,10 +87,6 @@ def fully_formatted_lines(failure_number, colorizer)
8887
lines
8988
end
9089

91-
def failure_slash_error_line
92-
@failure_slash_error_line ||= "Failure/Error: #{read_failed_line.strip}"
93-
end
94-
9590
private
9691

9792
def final_exception(exception)
@@ -143,17 +138,28 @@ def exception_class_name(exception=@exception)
143138
end
144139

145140
def failure_lines
146-
@failure_lines ||= begin
147-
lines = []
148-
lines << failure_slash_error_line unless (description == failure_slash_error_line)
149-
lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/
150-
encoded_string(exception.message.to_s).split("\n").each do |line|
151-
lines << (line.empty? ? line : " #{line}")
152-
end
153-
unless @extra_failure_lines.empty?
154-
lines << ''
155-
lines.concat(@extra_failure_lines)
156-
lines << ''
141+
@failure_lines ||= [failure_slash_error_line] + exception_lines + extra_failure_lines
142+
end
143+
144+
def failure_slash_error_line
145+
"Failure/Error: #{read_failed_line.strip}"
146+
end
147+
148+
def exception_lines
149+
lines = []
150+
lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/
151+
encoded_string(exception.message.to_s).split("\n").each do |line|
152+
lines << (line.empty? ? line : " #{line}")
153+
end
154+
lines
155+
end
156+
157+
def extra_failure_lines
158+
@extra_failure_lines ||= begin
159+
lines = Array(example.metadata[:extra_failure_lines])
160+
unless lines.empty?
161+
lines.unshift('')
162+
lines.push('')
157163
end
158164
lines
159165
end
@@ -239,9 +245,9 @@ def options
239245
def pending_options
240246
if @execution_result.pending_fixed?
241247
{
242-
:description_formatter => Proc.new { "#{@example.full_description} FIXED" },
243-
:message_color => RSpec.configuration.fixed_color,
244-
:failure_lines => [
248+
:description => "#{@example.full_description} FIXED",
249+
:message_color => RSpec.configuration.fixed_color,
250+
:failure_lines => [
245251
"Expected pending '#{@execution_result.pending_message}' to fail. No Error was raised."
246252
]
247253
}
@@ -264,8 +270,6 @@ def with_multiple_error_options_as_needed(exception, options)
264270
options[:message_color])
265271
)
266272

267-
options[:description_formatter] &&= Proc.new {}
268-
269273
return options unless exception.aggregation_metadata[:hide_backtrace]
270274
options[:backtrace_formatter] = EmptyBacktraceFormatter
271275
options
@@ -303,7 +307,7 @@ def sub_failure_list_formatter(exception, message_color)
303307
FlatMap.flat_map(exception.all_exceptions.each_with_index) do |failure, index|
304308
options = with_multiple_error_options_as_needed(
305309
failure,
306-
:description_formatter => :failure_slash_error_line.to_proc,
310+
:description => nil,
307311
:indentation => 0,
308312
:message_color => message_color || RSpec.configuration.failure_color,
309313
:skip_shared_group_trace => true

spec/rspec/core/formatters/exception_presenter_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module RSpec::Core
8181
it 'allows the caller to omit the description' do
8282
the_presenter = Formatters::ExceptionPresenter.new(exception, example,
8383
:detail_formatter => Proc.new { "Detail!" },
84-
:description_formatter => Proc.new { })
84+
:description => nil)
8585

8686
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
8787
|
@@ -94,7 +94,7 @@ module RSpec::Core
9494
end
9595

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

9999
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
100100
|

0 commit comments

Comments
 (0)