2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-10 07:22:19 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe RspecFlaky::Report, :aggregate_failures do
|
2020-01-21 16:08:54 -05:00
|
|
|
let(:thirty_one_days) { 3600 * 24 * 31 }
|
2018-04-10 07:22:19 -04:00
|
|
|
let(:collection_hash) do
|
|
|
|
{
|
|
|
|
a: { example_id: 'spec/foo/bar_spec.rb:2' },
|
2020-01-21 16:08:54 -05:00
|
|
|
b: { example_id: 'spec/foo/baz_spec.rb:3', first_flaky_at: (Time.now - thirty_one_days).to_s, last_flaky_at: (Time.now - thirty_one_days).to_s }
|
2018-04-10 07:22:19 -04:00
|
|
|
}
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2018-04-10 07:22:19 -04:00
|
|
|
let(:suite_flaky_example_report) do
|
|
|
|
{
|
|
|
|
'6e869794f4cfd2badd93eb68719371d1': {
|
|
|
|
example_id: 'spec/foo/bar_spec.rb:2',
|
|
|
|
file: 'spec/foo/bar_spec.rb',
|
|
|
|
line: 2,
|
|
|
|
description: 'hello world',
|
|
|
|
first_flaky_at: 1234,
|
|
|
|
last_flaky_at: 4321,
|
|
|
|
last_attempts_count: 3,
|
|
|
|
flaky_reports: 1,
|
|
|
|
last_flaky_job: nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2018-04-10 07:22:19 -04:00
|
|
|
let(:flaky_examples) { RspecFlaky::FlakyExamplesCollection.new(collection_hash) }
|
|
|
|
let(:report) { described_class.new(flaky_examples) }
|
|
|
|
|
|
|
|
describe '.load' do
|
|
|
|
let!(:report_file) do
|
|
|
|
Tempfile.new(%w[rspec_flaky_report .json]).tap do |f|
|
2020-04-30 20:09:59 -04:00
|
|
|
f.write(Gitlab::Json.pretty_generate(suite_flaky_example_report))
|
2018-04-10 07:22:19 -04:00
|
|
|
f.rewind
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
report_file.close
|
|
|
|
report_file.unlink
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'loads the report file' do
|
|
|
|
expect(described_class.load(report_file.path).flaky_examples.to_h).to eq(suite_flaky_example_report)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.load_json' do
|
|
|
|
let(:report_json) do
|
2020-04-30 20:09:59 -04:00
|
|
|
Gitlab::Json.pretty_generate(suite_flaky_example_report)
|
2018-04-10 07:22:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'loads the report file' do
|
|
|
|
expect(described_class.load_json(report_json).flaky_examples.to_h).to eq(suite_flaky_example_report)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#initialize' do
|
|
|
|
it 'accepts a RspecFlaky::FlakyExamplesCollection' do
|
|
|
|
expect { report }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not accept anything else' do
|
|
|
|
expect { described_class.new([1, 2, 3]) }.to raise_error(ArgumentError, "`flaky_examples` must be a RspecFlaky::FlakyExamplesCollection, Array given!")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'delegates to #flaky_examples using SimpleDelegator' do
|
|
|
|
expect(report.__getobj__).to eq(flaky_examples)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#write' do
|
|
|
|
let(:report_file_path) { Rails.root.join('tmp', 'rspec_flaky_report.json') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
FileUtils.rm(report_file_path) if File.exist?(report_file_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FileUtils.rm(report_file_path) if File.exist?(report_file_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when RspecFlaky::Config.generate_report? is false' do
|
|
|
|
before do
|
|
|
|
allow(RspecFlaky::Config).to receive(:generate_report?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not write any report file' do
|
|
|
|
report.write(report_file_path)
|
|
|
|
|
|
|
|
expect(File.exist?(report_file_path)).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when RspecFlaky::Config.generate_report? is true' do
|
|
|
|
before do
|
|
|
|
allow(RspecFlaky::Config).to receive(:generate_report?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'delegates the writes to RspecFlaky::Report' do
|
|
|
|
report.write(report_file_path)
|
|
|
|
|
|
|
|
expect(File.exist?(report_file_path)).to be(true)
|
|
|
|
expect(File.read(report_file_path))
|
2020-04-30 20:09:59 -04:00
|
|
|
.to eq(Gitlab::Json.pretty_generate(report.flaky_examples.to_h))
|
2018-04-10 07:22:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#prune_outdated' do
|
2020-01-21 16:08:54 -05:00
|
|
|
it 'returns a new collection without the examples older than 30 days by default' do
|
2018-04-10 07:22:19 -04:00
|
|
|
new_report = flaky_examples.to_h.dup.tap { |r| r.delete(:b) }
|
|
|
|
new_flaky_examples = report.prune_outdated
|
|
|
|
|
|
|
|
expect(new_flaky_examples).to be_a(described_class)
|
|
|
|
expect(new_flaky_examples.to_h).to eq(new_report)
|
|
|
|
expect(flaky_examples).to have_key(:b)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts a given number of days' do
|
2020-01-21 16:08:54 -05:00
|
|
|
new_flaky_examples = report.prune_outdated(days: 32)
|
2018-04-10 07:22:19 -04:00
|
|
|
|
|
|
|
expect(new_flaky_examples.to_h).to eq(report.to_h)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|