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

60 lines
1.6 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
2023-01-13 00:05:48 +05:30
feature_category
2022-01-26 12:08:38 +05:30
].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],
2023-01-13 00:05:48 +05:30
flaky_reports: 0,
feature_category: example_hash[:feature_category]
2022-01-26 12:08:38 +05:30
}.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
2023-01-13 00:05:48 +05:30
def update!(example_hash)
attributes[:file] = example_hash[:file]
attributes[:line] = example_hash[:line]
attributes[:description] = example_hash[:description]
2022-01-26 12:08:38 +05:30
attributes[:first_flaky_at] ||= Time.now
attributes[:last_flaky_at] = Time.now
attributes[:flaky_reports] += 1
2023-01-13 00:05:48 +05:30
attributes[:feature_category] = example_hash[:feature_category]
attributes[:last_attempts_count] = example_hash[:last_attempts_count] if example_hash[: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