Commit a1cf1f70 authored by Rémy Coutable's avatar Rémy Coutable

Make FlakyExample#.to_h return a copy of the internal hash

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 3faaf7f6
......@@ -46,14 +46,16 @@ RSpec.describe RspecFlaky::FlakyExample, :aggregate_failures do
let(:flaky_example) { described_class.new(args) }
it 'returns valid attributes' do
expect(flaky_example.to_h[:uid]).to eq(flaky_example_attrs[:uid])
expect(flaky_example.to_h[:file]).to eq(flaky_example_attrs[:file])
expect(flaky_example.to_h[:line]).to eq(flaky_example_attrs[:line])
expect(flaky_example.to_h[:description]).to eq(flaky_example_attrs[:description])
expect(flaky_example.to_h[:first_flaky_at]).to eq(expected_first_flaky_at)
expect(flaky_example.to_h[:last_flaky_at]).to eq(expected_last_flaky_at)
expect(flaky_example.to_h[:last_attempts_count]).to eq(flaky_example_attrs[:last_attempts_count])
expect(flaky_example.to_h[:flaky_reports]).to eq(expected_flaky_reports)
attrs = flaky_example.to_h
expect(attrs[:uid]).to eq(flaky_example_attrs[:uid])
expect(attrs[:file]).to eq(flaky_example_attrs[:file])
expect(attrs[:line]).to eq(flaky_example_attrs[:line])
expect(attrs[:description]).to eq(flaky_example_attrs[:description])
expect(attrs[:first_flaky_at]).to eq(expected_first_flaky_at)
expect(attrs[:last_flaky_at]).to eq(expected_last_flaky_at)
expect(attrs[:last_attempts_count]).to eq(flaky_example_attrs[:last_attempts_count])
expect(attrs[:flaky_reports]).to eq(expected_flaky_reports)
end
end
......
......@@ -17,11 +17,7 @@ module RspecFlaky
# This represents a flaky RSpec example and is mainly meant to be saved in a JSON file
class FlakyExample
attr_reader :attributes
alias_method :to_h, :attributes
def initialize(example_hash)
example_hash[:last_attempts_count] ||= example_hash[:attempts]
@attributes = {
first_flaky_at: Time.now,
last_flaky_at: Time.now,
......@@ -45,5 +41,13 @@ module RspecFlaky
attributes[:last_flaky_job] = "#{ENV['CI_JOB_URL']}"
end
end
def to_h
attributes.dup
end
private
attr_reader :attributes
end
end
......@@ -45,13 +45,13 @@ module RspecFlaky
def prune_outdated(days: OUTDATED_DAYS_THRESHOLD)
outdated_date_threshold = Time.now - (3600 * 24 * days)
updated_hash = flaky_examples.dup
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
end
self.class.new(RspecFlaky::FlakyExamplesCollection.new(updated_hash))
self.class.new(RspecFlaky::FlakyExamplesCollection.new(recent_flaky_examples))
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment