@@ -82,16 +82,35 @@ class ExceptionPresenter
8282 def initialize ( exception , example , options = { } )
8383 @exception = exception
8484 @example = example
85- # Patch to use no color by default
86- # TODO: Only use color if no diff is being printed
87- @message_color = options [ :message_color ]
85+ @message_color = options . fetch ( :message_color ) { RSpec . configuration . failure_color }
8886 @description = options . fetch ( :description ) { example . full_description }
8987 @detail_formatter = options . fetch ( :detail_formatter ) { Proc . new { } }
9088 @extra_detail_formatter = options . fetch ( :extra_detail_formatter ) { Proc . new { } }
9189 @backtrace_formatter = options . fetch ( :backtrace_formatter ) { RSpec . configuration . backtrace_formatter }
9290 @indentation = options . fetch ( :indentation , 2 )
9391 @skip_shared_group_trace = options . fetch ( :skip_shared_group_trace , false )
94- @failure_lines = options [ :failure_lines ]
92+ # Patch to convert options[:failure_lines] to groups
93+ if options . include? ( :failure_lines )
94+ @failure_line_groups = {
95+ lines : options [ :failure_lines ] ,
96+ already_colored : false
97+ }
98+ end
99+ end
100+
101+ # Override to only color uncolored lines in red
102+ def colorized_message_lines ( colorizer = ::RSpec ::Core ::Formatters ::ConsoleCodes )
103+ lines = failure_line_groups . flat_map do |group |
104+ if group [ :already_colored ]
105+ group [ :lines ]
106+ else
107+ group [ :lines ] . map do |line |
108+ colorizer . wrap ( line , message_color )
109+ end
110+ end
111+ end
112+
113+ add_shared_group_lines ( lines , colorizer )
95114 end
96115
97116 private
@@ -107,6 +126,39 @@ def add_shared_group_lines(lines, colorizer)
107126 lines
108127 end
109128
129+ # Considering that `failure_slash_error_lines` is already colored,
130+ # extract this from the other lines so that they, too, can be colored,
131+ # later
132+ def failure_line_groups
133+ @failure_line_groups ||= [ ] . tap do |groups |
134+ groups << {
135+ lines : failure_slash_error_lines ,
136+ already_colored : true
137+ }
138+
139+ sections = [ failure_slash_error_lines , exception_lines ]
140+ separate_groups = (
141+ sections . any? { |section | section . size > 1 } &&
142+ !exception_lines . first . empty?
143+ )
144+ if separate_groups
145+ groups << { lines : [ '' ] , already_colored : true }
146+ end
147+ already_has_coloration = exception_lines . any? do |line |
148+ line . match? ( /\e \[ \d +m/ )
149+ end
150+
151+ groups << {
152+ lines : exception_lines [ 0 ..0 ] ,
153+ already_colored : already_has_coloration
154+ }
155+ groups << {
156+ lines : exception_lines [ 1 ..-1 ] + extra_failure_lines ,
157+ already_colored : true
158+ }
159+ end
160+ end
161+
110162 # Style the first part in white and don't style the snippet of the line
111163 def failure_slash_error_lines
112164 lines = read_failed_lines
0 commit comments