Class: TrunkAnalyticsListener

Inherits:
Object
  • Object
show all
Defined in:
lib/trunk_spec_helper.rb

Overview

TrunkAnalyticsListener is a class that is used to listen to the execution of the Example class it generates and submits the final test reports

Instance Method Summary collapse

Constructor Details

#initializeTrunkAnalyticsListener

Returns a new instance of TrunkAnalyticsListener.



161
162
163
# File 'lib/trunk_spec_helper.rb', line 161

def initialize
  @testreport = $test_report
end

Instance Method Details

#add_test_case(example) ⇒ Object

trunk-ignore(rubocop/Metrics/CyclomaticComplexity,rubocop/Metrics/AbcSize,rubocop/Metrics/MethodLength)



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/trunk_spec_helper.rb', line 189

def add_test_case(example)
  failure_message = example.exception.to_s if example.exception
  failure_message = example.[:quarantined_exception].to_s if example.[:quarantined_exception]
  # TODO: should we use concatenated string or alias when auto-generated description?
  name = example.full_description
  file = escape(example.[:file_path])
  classname = file.sub(%r{\.[^/.]+\Z}, '').gsub('/', '.').gsub(/\A\.+|\.+\Z/, '')
  line = example.[:line_number]
  started_at = example.execution_result.started_at.to_i
  finished_at = example.execution_result.finished_at.to_i
  id = example.generate_trunk_id

  attempt_number = example.[:retry_attempts] || example.[:attempt_number] || 0
  status = example.execution_result.status.to_s
  # set the status to failure, but mark it as quarantined
  is_quarantined = example.[:quarantined_exception] ? true : false
  case example.execution_result.status
  when :passed
    status = is_quarantined ? Status.new('failure') : Status.new('success')
  when :failed
    status = Status.new('failure')
  when :pending
    status = Status.new('skipped')
  end
  parent_name = example.example_group.[:description]
  parent_name = parent_name.empty? ? 'rspec' : parent_name
  @testreport.add_test(id, name, classname, file, parent_name, line, status, attempt_number,
                       started_at, finished_at, failure_message || '', is_quarantined)
end

#close(_notification) ⇒ Object

trunk-ignore(rubocop/Metrics/MethodLength)



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/trunk_spec_helper.rb', line 170

def close(_notification)
  if ENV['TRUNK_LOCAL_UPLOAD_DIR']
    saved = @testreport.try_save(ENV['TRUNK_LOCAL_UPLOAD_DIR'])
    if saved
      puts 'Local Flaky tests report generated'.green
    else
      puts 'Failed to generate local flaky tests report'.red
    end
  else
    published = @testreport.publish
    if published
      puts 'Flaky tests report upload complete'.green
    else
      puts 'Failed to publish flaky tests report'.red
    end
  end
end

#example_finished(notification) ⇒ Object



165
166
167
# File 'lib/trunk_spec_helper.rb', line 165

def example_finished(notification)
  add_test_case(notification.example)
end