summaryrefslogtreecommitdiff
path: root/spec/lib/turbo_tests/json_rows_formatter.rb
blob: d0244caba848a29875a1b73d04ede47f859ff6b4 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# frozen_string_literal: true

require "json"
require "rspec/core"
require "rspec/core/formatters"
require "rspec/core/notifications"

module RSpecExt
  def handle_interrupt
    if RSpec.world.wants_to_quit
      exit!(1)
    else
      RSpec.world.wants_to_quit = true
    end
  end
end

RSpec::Core::Runner.singleton_class.prepend(RSpecExt)

module TurboTests
  # An RSpec formatter used for each subprocess during parallel test execution
  class JsonRowsFormatter
    RSpec::Core::Formatters.register(
      self,
      :start,
      :close,
      :example_failed,
      :example_passed,
      :example_pending,
      :example_group_started,
      :example_group_finished,
      :message,
      :seed
    )

    attr_reader :output

    def initialize(output)
      @output = output
    end

    def start(notification)
      output_row(
        type: :load_summary,
        summary: load_summary_to_json(notification)
      )
    end

    def example_group_started(notification)
      output_row(
        type: :group_started,
        group: group_to_json(notification)
      )
    end

    def example_group_finished(notification)
      output_row(
        type: :group_finished,
        group: group_to_json(notification)
      )
    end

    def example_passed(notification)
      output_row(
        type: :example_passed,
        example: example_to_json(notification.example)
      )
    end

    def example_pending(notification)
      output_row(
        type: :example_pending,
        example: example_to_json(notification.example)
      )
    end

    def example_failed(notification)
      output_row(
        type: :example_failed,
        example: example_to_json(notification.example)
      )
    end

    def seed(notification)
      output_row(
        type: :seed,
        seed: notification.seed
      )
    end

    def close(notification)
      output_row(
        type: :close
      )
    end

    def message(notification)
      output_row(
        type: :message,
        message: notification.message
      )
    end

    private

    def exception_to_json(exception)
      if exception
        {
          class_name: exception.class.name.to_s,
          backtrace: exception.backtrace,
          message: exception.message,
          cause: exception_to_json(exception.cause)
        }
      end
    end

    def execution_result_to_json(result)
      {
        example_skipped?: result.example_skipped?,
        pending_message: result.pending_message,
        status: result.status,
        pending_fixed?: result.pending_fixed?,
        exception: exception_to_json(result.exception || result.pending_exception)
      }
    end

    def stack_frame_to_json(frame)
      {
        shared_group_name: frame.shared_group_name,
        inclusion_location: frame.inclusion_location
      }
    end

    def example_to_json(example)
      {
        execution_result: execution_result_to_json(example.execution_result),
        location: example.location,
        description: example.description,
        full_description: example.full_description,
        metadata: {
          shared_group_inclusion_backtrace:
            example
              .metadata[:shared_group_inclusion_backtrace]
              .map { |frame| stack_frame_to_json(frame) },
          extra_failure_lines: example.metadata[:extra_failure_lines],
        },
        location_rerun_argument: example.location_rerun_argument,
      }
    end

    def load_summary_to_json(notification)
      {
        count: notification.count,
        load_time: notification.load_time,
      }
    end

    def group_to_json(notification)
      {
        group: {
          description: notification.group.description
        }
      }
    end

    def output_row(obj)
      output.puts ENV["RSPEC_FORMATTER_OUTPUT_ID"] + obj.to_json
      output.flush
    end
  end
end