debian-mirror-gitlab/tooling/rspec_flaky/flaky_example.rb

54 lines
1.3 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2021-04-17 20:07:23 +05:30
require 'ostruct'
2018-03-17 18:26:18 +05:30
module RspecFlaky
2022-01-26 12:08:38 +05:30
ALLOWED_ATTRIBUTES = %i[
example_id
file
line
description
first_flaky_at
last_flaky_at
last_flaky_job
last_attempts_count
flaky_reports
].freeze
2018-03-17 18:26:18 +05:30
# This represents a flaky RSpec example and is mainly meant to be saved in a JSON file
2022-01-26 12:08:38 +05:30
class FlakyExample
def initialize(example_hash)
@attributes = {
first_flaky_at: Time.now,
last_flaky_at: Time.now,
last_flaky_job: nil,
last_attempts_count: example_hash[:attempts],
flaky_reports: 0
}.merge(example_hash.slice(*ALLOWED_ATTRIBUTES))
%i[first_flaky_at last_flaky_at].each do |attr|
attributes[attr] = Time.parse(attributes[attr]) if attributes[attr].is_a?(String)
2018-03-17 18:26:18 +05:30
end
end
def update_flakiness!(last_attempts_count: nil)
2022-01-26 12:08:38 +05:30
attributes[:first_flaky_at] ||= Time.now
attributes[:last_flaky_at] = Time.now
attributes[:flaky_reports] += 1
attributes[:last_attempts_count] = last_attempts_count if last_attempts_count
2018-03-17 18:26:18 +05:30
2022-01-26 12:08:38 +05:30
if ENV['CI_JOB_URL']
attributes[:last_flaky_job] = "#{ENV['CI_JOB_URL']}"
2018-03-17 18:26:18 +05:30
end
end
def to_h
2022-01-26 12:08:38 +05:30
attributes.dup
2018-03-17 18:26:18 +05:30
end
2022-01-26 12:08:38 +05:30
private
attr_reader :attributes
2018-03-17 18:26:18 +05:30
end
end