diff --git a/lib/rspec/enriched_json/formatters/enriched_json_formatter.rb b/lib/rspec/enriched_json/formatters/enriched_json_formatter.rb index c548d79..c48c528 100644 --- a/lib/rspec/enriched_json/formatters/enriched_json_formatter.rb +++ b/lib/rspec/enriched_json/formatters/enriched_json_formatter.rb @@ -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 = /#?(?.+?):(?\d+)/ + EXCEPTION_CLASS_AND_MESSAGE_REGEX = /^(?[A-Z]\w*Error|Exception):$\n(?(?^\s\s.*\n?)+)/ + 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| @@ -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) diff --git a/lib/rspec/enriched_json/version.rb b/lib/rspec/enriched_json/version.rb index 1f1549c..6e7f6d9 100644 --- a/lib/rspec/enriched_json/version.rb +++ b/lib/rspec/enriched_json/version.rb @@ -2,6 +2,6 @@ module RSpec module EnrichedJson - VERSION = "0.8.0" + VERSION = "0.8.1" end end