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

58 lines
1.7 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-10-15 14:42:47 +05:30
require 'json'
require 'time'
2021-04-17 20:07:23 +05:30
require_relative 'config'
require_relative 'flaky_examples_collection'
2018-10-15 14:42:47 +05:30
module RspecFlaky
# This class is responsible for loading/saving JSON reports, and pruning
# outdated examples.
class Report < SimpleDelegator
2022-01-26 12:08:38 +05:30
OUTDATED_DAYS_THRESHOLD = 7
2018-10-15 14:42:47 +05:30
attr_reader :flaky_examples
def self.load(file_path)
load_json(File.read(file_path))
end
def self.load_json(json)
new(RspecFlaky::FlakyExamplesCollection.new(JSON.parse(json)))
end
def initialize(flaky_examples)
unless flaky_examples.is_a?(RspecFlaky::FlakyExamplesCollection)
raise ArgumentError, "`flaky_examples` must be a RspecFlaky::FlakyExamplesCollection, #{flaky_examples.class} given!"
end
@flaky_examples = flaky_examples
super(flaky_examples)
end
def write(file_path)
unless RspecFlaky::Config.generate_report?
2021-04-17 20:07:23 +05:30
Kernel.warn "! Generating reports is disabled. To enable it, please set the `FLAKY_RSPEC_GENERATE_REPORT=1` !"
2018-10-15 14:42:47 +05:30
return
end
report_path_dir = File.dirname(file_path)
FileUtils.mkdir_p(report_path_dir) unless Dir.exist?(report_path_dir)
File.write(file_path, JSON.pretty_generate(flaky_examples.to_h))
end
def prune_outdated(days: OUTDATED_DAYS_THRESHOLD)
outdated_date_threshold = Time.now - (3600 * 24 * days)
2022-01-26 12:08:38 +05:30
recent_flaky_examples = flaky_examples.dup
.delete_if do |_uid, flaky_example|
last_flaky_at = flaky_example.to_h[:last_flaky_at]
last_flaky_at && last_flaky_at.to_i < outdated_date_threshold.to_i
2018-10-15 14:42:47 +05:30
end
2022-01-26 12:08:38 +05:30
self.class.new(RspecFlaky::FlakyExamplesCollection.new(recent_flaky_examples))
2018-10-15 14:42:47 +05:30
end
end
end