Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/rspec/enriched_json/formatters/enriched_json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@ module RSpec
module EnrichedJson
module Formatters
class EnrichedJsonFormatter < RSpec::Core::Formatters::JsonFormatter
ANSI_COLOR_REGEX = /\e\[[0-9;]*[mGKHF]/
EXCEPTION_DETECTOR_REGEX = /(Exception|Error|undefined method|uninitialized constant)/
PATH_AND_LINE_NUMBER_REGEX = /#?(?<path>.+?):(?<line_number>\d+)/
EXCEPTION_CLASS_AND_MESSAGE_REGEX = /^(?<exception_class>[A-Z]\w*Error|Exception):$\n(?<exception_message>(?<extra>^\s\s.*\n?)+)/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXCEPTION_CLASS_AND_MESSAGE_REGEX includes an unused nested capture group 'extra'. Remove it to simplify the regex if it's not required.

Suggested change
EXCEPTION_CLASS_AND_MESSAGE_REGEX = /^(?<exception_class>[A-Z]\w*Error|Exception):$\n(?<exception_message>(?<extra>^\s\s.*\n?)+)/
EXCEPTION_CLASS_AND_MESSAGE_REGEX = /^(?<exception_class>[A-Z]\w*Error|Exception):$\n(?<exception_message>^\s\s.*\n?)+/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This group is required to capture multi-line messages in the exception named capture group. The actual result of the extra capture group is not important by itself, but it's presence in the regular expression is. I added a name to it because of Rubocop warnings.


RSpec::Core::Formatters.register self, :message, :dump_summary, :dump_profile, :stop, :seed, :close

def initialize(output)
super
@output_hash = {
errors: []
}
end

def stop(group_notification)
@output_hash[:examples] = group_notification.notifications.map do |notification|
format_example(notification.example).tap do |hash|
Expand Down Expand Up @@ -36,6 +48,30 @@ def stop(group_notification)
end
end

def message(notification)
super

if notification.message.match?(EXCEPTION_DETECTOR_REGEX)
clean_message = notification.message.gsub(ANSI_COLOR_REGEX, "")

error_info = {
message: clean_message
}

if (match = clean_message.match(PATH_AND_LINE_NUMBER_REGEX))
error_info[:path] = match.named_captures["path"]
error_info[:line_number] = match.named_captures["line_number"]
end

if (match = clean_message.match(EXCEPTION_CLASS_AND_MESSAGE_REGEX))
error_info[:exception_class] = match.named_captures["exception_class"]
error_info[:exception_message] = match.named_captures["exception_message"]
end

@output_hash[:errors] << error_info
end
end

private

def add_metadata(hash, example)
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/enriched_json/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module RSpec
module EnrichedJson
VERSION = "0.8.0"
VERSION = "0.8.1"
end
end
Loading