Commit d09b9de1 authored by Mark Lapierre's avatar Mark Lapierre

Make the RSpec JSON formatter more like JUnit

Include the error and stacktrace as they're presented by the JUnit
RSpec formatter (which is also more similar to the RSpec failure
output we're used to)

This will also help fix the problem of info in the job logs missing
from testcase issue comments
parent 5086bcd9
...@@ -12,19 +12,21 @@ module QA ...@@ -12,19 +12,21 @@ module QA
# implementation so that it's not included. # implementation so that it's not included.
end end
def stop(notification) def stop(example_notification)
# Based on https://github.com/rspec/rspec-core/blob/main/lib/rspec/core/formatters/json_formatter.rb#L35 # Based on https://github.com/rspec/rspec-core/blob/main/lib/rspec/core/formatters/json_formatter.rb#L35
# But modified to include full details of multiple exceptions # But modified to include full details of multiple exceptions and to provide output similar to
@output_hash[:examples] = notification.examples.map do |example| # https://github.com/sj26/rspec_junit_formatter
format_example(example).tap do |hash| @output_hash[:examples] = example_notification.notifications.map do |notification|
e = example.exception format_example(notification.example).tap do |hash|
e = notification.example.exception
if e if e
exceptions = e.respond_to?(:all_exceptions) ? e.all_exceptions : [e] exceptions = e.respond_to?(:all_exceptions) ? e.all_exceptions : [e]
hash[:exceptions] = exceptions.map do |exception| hash[:exceptions] = exceptions.map do |exception|
{ {
class: exception.class.name, class: exception.class.name,
message: exception.message, message: exception.message,
backtrace: exception.backtrace message_lines: strip_ansi_codes(notification.message_lines),
backtrace: notification.formatted_backtrace
} }
end end
end end
...@@ -60,6 +62,12 @@ module QA ...@@ -60,6 +62,12 @@ module QA
metadata[:shared_group_inclusion_backtrace].last.formatted_inclusion_location.split(':') metadata[:shared_group_inclusion_backtrace].last.formatted_inclusion_location.split(':')
end end
end end
def strip_ansi_codes(strings)
# The code below is from https://github.com/piotrmurach/pastel/blob/master/lib/pastel/color.rb
modified = Array(strings).map { |string| string.dup.gsub(/\x1b\[{1,2}[0-9;:?]*m/m, '') }
modified.size == 1 ? modified[0] : modified
end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment