summaryrefslogtreecommitdiff
path: root/qa/qa/support/json_formatter.rb
blob: 5d2a3e7b75f43ca49b0bd42b691389b684f52439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

require 'rspec/core/formatters'

module QA
  module Support
    class JsonFormatter < RSpec::Core::Formatters::JsonFormatter
      RSpec::Core::Formatters.register self, :message, :dump_summary, :stop, :seed, :close

      def dump_profile(profile)
        # We don't currently use the profile info. This overrides the base
        # implementation so that it's not included.
      end

      def stop(notification)
        # 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
        @output_hash[:examples] = notification.examples.map do |example|
          format_example(example).tap do |hash|
            e = example.exception
            if e
              exceptions = e.respond_to?(:all_exceptions) ? e.all_exceptions : [e]
              hash[:exceptions] = exceptions.map do |exception|
                {
                  class: exception.class.name,
                  message: exception.message,
                  backtrace: exception.backtrace
                }
              end
            end
          end
        end
      end

      private

      def format_example(example)
        {
          id: example.id,
          description: example.description,
          full_description: example.full_description,
          status: example.execution_result.status.to_s,
          file_path: example.metadata[:file_path],
          line_number: example.metadata[:line_number],
          run_time: example.execution_result.run_time,
          pending_message: example.execution_result.pending_message,
          status_issue: example.metadata[:status_issue],
          quarantine: example.metadata[:quarantine],
          screenshot: example.metadata[:screenshot]
        }
      end
    end
  end
end