diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-23 09:10:45 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-23 09:10:45 +0000 |
commit | cd40f83527ac4ed4751b4b7849f0416996589d18 (patch) | |
tree | 201a36d826fc98f14ee1a7a1dc806b32f51cac00 /spec/lib | |
parent | d5f3372f10b9fefc8cf831515152eee7ae908f69 (diff) | |
download | gitlab-ce-cd40f83527ac4ed4751b4b7849f0416996589d18.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/rspec_flaky/config_spec.rb | 106 | ||||
-rw-r--r-- | spec/lib/rspec_flaky/example_spec.rb | 92 | ||||
-rw-r--r-- | spec/lib/rspec_flaky/flaky_example_spec.rb | 165 | ||||
-rw-r--r-- | spec/lib/rspec_flaky/flaky_examples_collection_spec.rb | 74 | ||||
-rw-r--r-- | spec/lib/rspec_flaky/listener_spec.rb | 219 | ||||
-rw-r--r-- | spec/lib/rspec_flaky/report_spec.rb | 129 |
6 files changed, 0 insertions, 785 deletions
diff --git a/spec/lib/rspec_flaky/config_spec.rb b/spec/lib/rspec_flaky/config_spec.rb deleted file mode 100644 index 6b148599b67..00000000000 --- a/spec/lib/rspec_flaky/config_spec.rb +++ /dev/null @@ -1,106 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe RspecFlaky::Config, :aggregate_failures do - before do - # Stub these env variables otherwise specs don't behave the same on the CI - stub_env('FLAKY_RSPEC_GENERATE_REPORT', nil) - stub_env('SUITE_FLAKY_RSPEC_REPORT_PATH', nil) - stub_env('FLAKY_RSPEC_REPORT_PATH', nil) - stub_env('NEW_FLAKY_RSPEC_REPORT_PATH', nil) - end - - describe '.generate_report?' do - context "when ENV['FLAKY_RSPEC_GENERATE_REPORT'] is not set" do - it 'returns false' do - expect(described_class).not_to be_generate_report - end - end - - context "when ENV['FLAKY_RSPEC_GENERATE_REPORT'] is set" do - using RSpec::Parameterized::TableSyntax - - where(:env_value, :result) do - '1' | true - 'true' | true - 'foo' | false - '0' | false - 'false' | false - end - - with_them do - before do - stub_env('FLAKY_RSPEC_GENERATE_REPORT', env_value) - end - - it 'returns false' do - expect(described_class.generate_report?).to be(result) - end - end - end - end - - describe '.suite_flaky_examples_report_path' do - context "when ENV['SUITE_FLAKY_RSPEC_REPORT_PATH'] is not set" do - it 'returns the default path' do - expect(Rails.root).to receive(:join).with('rspec_flaky/suite-report.json') - .and_return('root/rspec_flaky/suite-report.json') - - expect(described_class.suite_flaky_examples_report_path).to eq('root/rspec_flaky/suite-report.json') - end - end - - context "when ENV['SUITE_FLAKY_RSPEC_REPORT_PATH'] is set" do - before do - stub_env('SUITE_FLAKY_RSPEC_REPORT_PATH', 'foo/suite-report.json') - end - - it 'returns the value of the env variable' do - expect(described_class.suite_flaky_examples_report_path).to eq('foo/suite-report.json') - end - end - end - - describe '.flaky_examples_report_path' do - context "when ENV['FLAKY_RSPEC_REPORT_PATH'] is not set" do - it 'returns the default path' do - expect(Rails.root).to receive(:join).with('rspec_flaky/report.json') - .and_return('root/rspec_flaky/report.json') - - expect(described_class.flaky_examples_report_path).to eq('root/rspec_flaky/report.json') - end - end - - context "when ENV['FLAKY_RSPEC_REPORT_PATH'] is set" do - before do - stub_env('FLAKY_RSPEC_REPORT_PATH', 'foo/report.json') - end - - it 'returns the value of the env variable' do - expect(described_class.flaky_examples_report_path).to eq('foo/report.json') - end - end - end - - describe '.new_flaky_examples_report_path' do - context "when ENV['NEW_FLAKY_RSPEC_REPORT_PATH'] is not set" do - it 'returns the default path' do - expect(Rails.root).to receive(:join).with('rspec_flaky/new-report.json') - .and_return('root/rspec_flaky/new-report.json') - - expect(described_class.new_flaky_examples_report_path).to eq('root/rspec_flaky/new-report.json') - end - end - - context "when ENV['NEW_FLAKY_RSPEC_REPORT_PATH'] is set" do - before do - stub_env('NEW_FLAKY_RSPEC_REPORT_PATH', 'foo/new-report.json') - end - - it 'returns the value of the env variable' do - expect(described_class.new_flaky_examples_report_path).to eq('foo/new-report.json') - end - end - end -end diff --git a/spec/lib/rspec_flaky/example_spec.rb b/spec/lib/rspec_flaky/example_spec.rb deleted file mode 100644 index 4b45a15c463..00000000000 --- a/spec/lib/rspec_flaky/example_spec.rb +++ /dev/null @@ -1,92 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe RspecFlaky::Example do - let(:example_attrs) do - { - id: 'spec/foo/bar_spec.rb:2', - metadata: { - file_path: 'spec/foo/bar_spec.rb', - line_number: 2, - full_description: 'hello world' - }, - execution_result: double(status: 'passed', exception: 'BOOM!'), - attempts: 1 - } - end - - let(:rspec_example) { double(example_attrs) } - - describe '#initialize' do - shared_examples 'a valid Example instance' do - it 'returns valid attributes' do - example = described_class.new(args) - - expect(example.example_id).to eq(example_attrs[:id]) - end - end - - context 'when given an Rspec::Core::Example that responds to #example' do - let(:args) { double(example: rspec_example) } - - it_behaves_like 'a valid Example instance' - end - - context 'when given an Rspec::Core::Example that does not respond to #example' do - let(:args) { rspec_example } - - it_behaves_like 'a valid Example instance' - end - end - - subject { described_class.new(rspec_example) } - - describe '#uid' do - it 'returns a hash of the full description' do - expect(subject.uid).to eq(Digest::MD5.hexdigest("#{subject.description}-#{subject.file}")) - end - end - - describe '#example_id' do - it 'returns the ID of the RSpec::Core::Example' do - expect(subject.example_id).to eq(rspec_example.id) - end - end - - describe '#attempts' do - it 'returns the attempts of the RSpec::Core::Example' do - expect(subject.attempts).to eq(rspec_example.attempts) - end - end - - describe '#file' do - it 'returns the metadata[:file_path] of the RSpec::Core::Example' do - expect(subject.file).to eq(rspec_example.metadata[:file_path]) - end - end - - describe '#line' do - it 'returns the metadata[:line_number] of the RSpec::Core::Example' do - expect(subject.line).to eq(rspec_example.metadata[:line_number]) - end - end - - describe '#description' do - it 'returns the metadata[:full_description] of the RSpec::Core::Example' do - expect(subject.description).to eq(rspec_example.metadata[:full_description]) - end - end - - describe '#status' do - it 'returns the execution_result.status of the RSpec::Core::Example' do - expect(subject.status).to eq(rspec_example.execution_result.status) - end - end - - describe '#exception' do - it 'returns the execution_result.exception of the RSpec::Core::Example' do - expect(subject.exception).to eq(rspec_example.execution_result.exception) - end - end -end diff --git a/spec/lib/rspec_flaky/flaky_example_spec.rb b/spec/lib/rspec_flaky/flaky_example_spec.rb deleted file mode 100644 index b1647d5830a..00000000000 --- a/spec/lib/rspec_flaky/flaky_example_spec.rb +++ /dev/null @@ -1,165 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe RspecFlaky::FlakyExample, :aggregate_failures do - let(:flaky_example_attrs) do - { - 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: 2345, - last_flaky_job: 'https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/12', - last_attempts_count: 2, - flaky_reports: 1 - } - end - - let(:example_attrs) do - { - uid: 'abc123', - example_id: flaky_example_attrs[:example_id], - file: flaky_example_attrs[:file], - line: flaky_example_attrs[:line], - description: flaky_example_attrs[:description], - status: 'passed', - exception: 'BOOM!', - attempts: flaky_example_attrs[:last_attempts_count] - } - end - - let(:example) { double(example_attrs) } - - before do - # Stub these env variables otherwise specs don't behave the same on the CI - stub_env('CI_PROJECT_URL', nil) - stub_env('CI_JOB_ID', nil) - end - - describe '#initialize' do - shared_examples 'a valid FlakyExample instance' do - let(:flaky_example) { described_class.new(args) } - - it 'returns valid attributes' do - expect(flaky_example.uid).to eq(flaky_example_attrs[:uid]) - expect(flaky_example.file).to eq(flaky_example_attrs[:file]) - expect(flaky_example.line).to eq(flaky_example_attrs[:line]) - expect(flaky_example.description).to eq(flaky_example_attrs[:description]) - expect(flaky_example.first_flaky_at).to eq(expected_first_flaky_at) - expect(flaky_example.last_flaky_at).to eq(expected_last_flaky_at) - expect(flaky_example.last_attempts_count).to eq(flaky_example_attrs[:last_attempts_count]) - expect(flaky_example.flaky_reports).to eq(expected_flaky_reports) - end - end - - context 'when given an Rspec::Example' do - it_behaves_like 'a valid FlakyExample instance' do - let(:args) { example } - let(:expected_first_flaky_at) { nil } - let(:expected_last_flaky_at) { nil } - let(:expected_flaky_reports) { 0 } - end - end - - context 'when given a hash' do - it_behaves_like 'a valid FlakyExample instance' do - let(:args) { flaky_example_attrs } - let(:expected_flaky_reports) { flaky_example_attrs[:flaky_reports] } - let(:expected_first_flaky_at) { flaky_example_attrs[:first_flaky_at] } - let(:expected_last_flaky_at) { flaky_example_attrs[:last_flaky_at] } - end - end - end - - describe '#update_flakiness!' do - shared_examples 'an up-to-date FlakyExample instance' do - let(:flaky_example) { described_class.new(args) } - - it 'updates the first_flaky_at' do - now = Time.now - expected_first_flaky_at = flaky_example.first_flaky_at || now - Timecop.freeze(now) { flaky_example.update_flakiness! } - - expect(flaky_example.first_flaky_at).to eq(expected_first_flaky_at) - end - - it 'updates the last_flaky_at' do - now = Time.now - Timecop.freeze(now) { flaky_example.update_flakiness! } - - expect(flaky_example.last_flaky_at).to eq(now) - end - - it 'updates the flaky_reports' do - expected_flaky_reports = flaky_example.first_flaky_at ? flaky_example.flaky_reports + 1 : 1 - - expect { flaky_example.update_flakiness! }.to change { flaky_example.flaky_reports }.by(1) - expect(flaky_example.flaky_reports).to eq(expected_flaky_reports) - end - - context 'when passed a :last_attempts_count' do - it 'updates the last_attempts_count' do - flaky_example.update_flakiness!(last_attempts_count: 42) - - expect(flaky_example.last_attempts_count).to eq(42) - end - end - - context 'when run on the CI' do - before do - stub_env('CI_PROJECT_URL', 'https://gitlab.com/gitlab-org/gitlab-foss') - stub_env('CI_JOB_ID', 42) - end - - it 'updates the last_flaky_job' do - flaky_example.update_flakiness! - - expect(flaky_example.last_flaky_job).to eq('https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/42') - end - end - end - - context 'when given an Rspec::Example' do - it_behaves_like 'an up-to-date FlakyExample instance' do - let(:args) { example } - end - end - - context 'when given a hash' do - it_behaves_like 'an up-to-date FlakyExample instance' do - let(:args) { flaky_example_attrs } - end - end - end - - describe '#to_h' do - shared_examples 'a valid FlakyExample hash' do - let(:additional_attrs) { {} } - - it 'returns a valid hash' do - flaky_example = described_class.new(args) - final_hash = flaky_example_attrs.merge(additional_attrs) - - expect(flaky_example.to_h).to eq(final_hash) - end - end - - context 'when given an Rspec::Example' do - let(:args) { example } - - it_behaves_like 'a valid FlakyExample hash' do - let(:additional_attrs) do - { first_flaky_at: nil, last_flaky_at: nil, last_flaky_job: nil, flaky_reports: 0 } - end - end - end - - context 'when given a hash' do - let(:args) { flaky_example_attrs } - - it_behaves_like 'a valid FlakyExample hash' - end - end -end diff --git a/spec/lib/rspec_flaky/flaky_examples_collection_spec.rb b/spec/lib/rspec_flaky/flaky_examples_collection_spec.rb deleted file mode 100644 index b2fd1d3733a..00000000000 --- a/spec/lib/rspec_flaky/flaky_examples_collection_spec.rb +++ /dev/null @@ -1,74 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe RspecFlaky::FlakyExamplesCollection, :aggregate_failures do - let(:collection_hash) do - { - a: { example_id: 'spec/foo/bar_spec.rb:2' }, - b: { example_id: 'spec/foo/baz_spec.rb:3' } - } - end - - let(:collection_report) do - { - a: { - example_id: 'spec/foo/bar_spec.rb:2', - first_flaky_at: nil, - last_flaky_at: nil, - last_flaky_job: nil - }, - b: { - example_id: 'spec/foo/baz_spec.rb:3', - first_flaky_at: nil, - last_flaky_at: nil, - last_flaky_job: nil - } - } - end - - describe '#initialize' do - it 'accepts no argument' do - expect { described_class.new }.not_to raise_error - end - - it 'accepts a hash' do - expect { described_class.new(collection_hash) }.not_to raise_error - end - - it 'does not accept anything else' do - expect { described_class.new([1, 2, 3]) }.to raise_error(ArgumentError, "`collection` must be a Hash, Array given!") - end - end - - describe '#to_h' do - it 'calls #to_h on the values' do - collection = described_class.new(collection_hash) - - expect(collection.to_h).to eq(collection_report) - end - end - - describe '#-' do - it 'returns only examples that are not present in the given collection' do - collection1 = described_class.new(collection_hash) - collection2 = described_class.new( - a: { example_id: 'spec/foo/bar_spec.rb:2' }, - c: { example_id: 'spec/bar/baz_spec.rb:4' }) - - expect((collection2 - collection1).to_h).to eq( - c: { - example_id: 'spec/bar/baz_spec.rb:4', - first_flaky_at: nil, - last_flaky_at: nil, - last_flaky_job: nil - }) - end - - it 'fails if the given collection does not respond to `#key?`' do - collection = described_class.new(collection_hash) - - expect { collection - [1, 2, 3] }.to raise_error(ArgumentError, "`other` must respond to `#key?`, Array does not!") - end - end -end diff --git a/spec/lib/rspec_flaky/listener_spec.rb b/spec/lib/rspec_flaky/listener_spec.rb deleted file mode 100644 index 10ed724d4de..00000000000 --- a/spec/lib/rspec_flaky/listener_spec.rb +++ /dev/null @@ -1,219 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe RspecFlaky::Listener, :aggregate_failures do - let(:already_flaky_example_uid) { '6e869794f4cfd2badd93eb68719371d1' } - let(:suite_flaky_example_report) do - { - "#{already_flaky_example_uid}": { - 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 - - let(:already_flaky_example_attrs) do - { - id: 'spec/foo/bar_spec.rb:2', - metadata: { - file_path: 'spec/foo/bar_spec.rb', - line_number: 2, - full_description: 'hello world' - }, - execution_result: double(status: 'passed', exception: nil) - } - end - - let(:already_flaky_example) { RspecFlaky::FlakyExample.new(suite_flaky_example_report[already_flaky_example_uid]) } - let(:new_example_attrs) do - { - id: 'spec/foo/baz_spec.rb:3', - metadata: { - file_path: 'spec/foo/baz_spec.rb', - line_number: 3, - full_description: 'hello GitLab' - }, - execution_result: double(status: 'passed', exception: nil) - } - end - - before do - # Stub these env variables otherwise specs don't behave the same on the CI - stub_env('CI_PROJECT_URL', nil) - stub_env('CI_JOB_ID', nil) - stub_env('SUITE_FLAKY_RSPEC_REPORT_PATH', nil) - end - - describe '#initialize' do - shared_examples 'a valid Listener instance' do - let(:expected_suite_flaky_examples) { {} } - - it 'returns a valid Listener instance' do - listener = described_class.new - - expect(listener.suite_flaky_examples.to_h).to eq(expected_suite_flaky_examples) - expect(listener.flaky_examples).to eq({}) - end - end - - context 'when no report file exists' do - it_behaves_like 'a valid Listener instance' - end - - context 'when SUITE_FLAKY_RSPEC_REPORT_PATH is set' do - let(:report_file_path) { 'foo/report.json' } - - before do - stub_env('SUITE_FLAKY_RSPEC_REPORT_PATH', report_file_path) - end - - context 'and report file exists' do - before do - expect(File).to receive(:exist?).with(report_file_path).and_return(true) - end - - it 'delegates the load to RspecFlaky::Report' do - report = RspecFlaky::Report.new(RspecFlaky::FlakyExamplesCollection.new(suite_flaky_example_report)) - - expect(RspecFlaky::Report).to receive(:load).with(report_file_path).and_return(report) - expect(described_class.new.suite_flaky_examples.to_h).to eq(report.flaky_examples.to_h) - end - end - - context 'and report file does not exist' do - before do - expect(File).to receive(:exist?).with(report_file_path).and_return(false) - end - - it 'return an empty hash' do - expect(RspecFlaky::Report).not_to receive(:load) - expect(described_class.new.suite_flaky_examples.to_h).to eq({}) - end - end - end - end - - describe '#example_passed' do - let(:rspec_example) { double(new_example_attrs) } - let(:notification) { double(example: rspec_example) } - let(:listener) { described_class.new(suite_flaky_example_report.to_json) } - - shared_examples 'a non-flaky example' do - it 'does not change the flaky examples hash' do - expect { listener.example_passed(notification) } - .not_to change { listener.flaky_examples } - end - end - - shared_examples 'an existing flaky example' do - let(:expected_flaky_example) do - { - example_id: 'spec/foo/bar_spec.rb:2', - file: 'spec/foo/bar_spec.rb', - line: 2, - description: 'hello world', - first_flaky_at: 1234, - last_attempts_count: 2, - flaky_reports: 2, - last_flaky_job: nil - } - end - - it 'changes the flaky examples hash' do - new_example = RspecFlaky::Example.new(rspec_example) - - now = Time.now - Timecop.freeze(now) do - expect { listener.example_passed(notification) } - .to change { listener.flaky_examples[new_example.uid].to_h } - end - - expect(listener.flaky_examples[new_example.uid].to_h) - .to eq(expected_flaky_example.merge(last_flaky_at: now)) - end - end - - shared_examples 'a new flaky example' do - let(:expected_flaky_example) do - { - example_id: 'spec/foo/baz_spec.rb:3', - file: 'spec/foo/baz_spec.rb', - line: 3, - description: 'hello GitLab', - last_attempts_count: 2, - flaky_reports: 1, - last_flaky_job: nil - } - end - - it 'changes the all flaky examples hash' do - new_example = RspecFlaky::Example.new(rspec_example) - - now = Time.now - Timecop.freeze(now) do - expect { listener.example_passed(notification) } - .to change { listener.flaky_examples[new_example.uid].to_h } - end - - expect(listener.flaky_examples[new_example.uid].to_h) - .to eq(expected_flaky_example.merge(first_flaky_at: now, last_flaky_at: now)) - end - end - - describe 'when the RSpec example does not respond to attempts' do - it_behaves_like 'a non-flaky example' - end - - describe 'when the RSpec example has 1 attempt' do - let(:rspec_example) { double(new_example_attrs.merge(attempts: 1)) } - - it_behaves_like 'a non-flaky example' - end - - describe 'when the RSpec example has 2 attempts' do - let(:rspec_example) { double(new_example_attrs.merge(attempts: 2)) } - - it_behaves_like 'a new flaky example' - - context 'with an existing flaky example' do - let(:rspec_example) { double(already_flaky_example_attrs.merge(attempts: 2)) } - - it_behaves_like 'an existing flaky example' - end - end - end - - describe '#dump_summary' do - let(:listener) { described_class.new(suite_flaky_example_report.to_json) } - let(:new_flaky_rspec_example) { double(new_example_attrs.merge(attempts: 2)) } - let(:already_flaky_rspec_example) { double(already_flaky_example_attrs.merge(attempts: 2)) } - let(:notification_new_flaky_rspec_example) { double(example: new_flaky_rspec_example) } - let(:notification_already_flaky_rspec_example) { double(example: already_flaky_rspec_example) } - - context 'when a report file path is set by FLAKY_RSPEC_REPORT_PATH' do - it 'delegates the writes to RspecFlaky::Report' do - listener.example_passed(notification_new_flaky_rspec_example) - listener.example_passed(notification_already_flaky_rspec_example) - - report1 = double - report2 = double - - expect(RspecFlaky::Report).to receive(:new).with(listener.flaky_examples).and_return(report1) - expect(report1).to receive(:write).with(RspecFlaky::Config.flaky_examples_report_path) - - expect(RspecFlaky::Report).to receive(:new).with(listener.flaky_examples - listener.suite_flaky_examples).and_return(report2) - expect(report2).to receive(:write).with(RspecFlaky::Config.new_flaky_examples_report_path) - - listener.dump_summary(nil) - end - end - end -end diff --git a/spec/lib/rspec_flaky/report_spec.rb b/spec/lib/rspec_flaky/report_spec.rb deleted file mode 100644 index 5cacfdb82fb..00000000000 --- a/spec/lib/rspec_flaky/report_spec.rb +++ /dev/null @@ -1,129 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe RspecFlaky::Report, :aggregate_failures do - let(:thirty_one_days) { 3600 * 24 * 31 } - let(:collection_hash) do - { - a: { example_id: 'spec/foo/bar_spec.rb:2' }, - 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 } - } - end - - 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 - - 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| - f.write(Gitlab::Json.pretty_generate(suite_flaky_example_report)) - 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 - Gitlab::Json.pretty_generate(suite_flaky_example_report) - 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)) - .to eq(Gitlab::Json.pretty_generate(report.flaky_examples.to_h)) - end - end - end - - describe '#prune_outdated' do - it 'returns a new collection without the examples older than 30 days by default' do - 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 - new_flaky_examples = report.prune_outdated(days: 32) - - expect(new_flaky_examples.to_h).to eq(report.to_h) - end - end -end |