diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 13:37:47 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 13:37:47 +0000 |
commit | aee0a117a889461ce8ced6fcf73207fe017f1d99 (patch) | |
tree | 891d9ef189227a8445d83f35c1b0fc99573f4380 /spec/tooling/rspec_flaky | |
parent | 8d46af3258650d305f53b819eabf7ab18d22f59e (diff) | |
download | gitlab-ce-aee0a117a889461ce8ced6fcf73207fe017f1d99.tar.gz |
Add latest changes from gitlab-org/gitlab@14-6-stable-eev14.6.0-rc42
Diffstat (limited to 'spec/tooling/rspec_flaky')
-rw-r--r-- | spec/tooling/rspec_flaky/flaky_example_spec.rb | 76 | ||||
-rw-r--r-- | spec/tooling/rspec_flaky/flaky_examples_collection_spec.rb | 28 | ||||
-rw-r--r-- | spec/tooling/rspec_flaky/listener_spec.rb | 7 | ||||
-rw-r--r-- | spec/tooling/rspec_flaky/report_spec.rb | 4 |
4 files changed, 62 insertions, 53 deletions
diff --git a/spec/tooling/rspec_flaky/flaky_example_spec.rb b/spec/tooling/rspec_flaky/flaky_example_spec.rb index ab652662c0b..03436ee1cbd 100644 --- a/spec/tooling/rspec_flaky/flaky_example_spec.rb +++ b/spec/tooling/rspec_flaky/flaky_example_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require 'active_support/testing/time_helpers' require_relative '../../support/helpers/stub_env' +require_relative '../../support/time_travel' require_relative '../../../tooling/rspec_flaky/flaky_example' @@ -36,40 +36,39 @@ RSpec.describe RspecFlaky::FlakyExample, :aggregate_failures do } end - let(:example) { OpenStruct.new(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) + stub_env('CI_JOB_URL', nil) end - describe '#initialize' do + describe '#initialize', :freeze_time 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) + 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 - context 'when given an Rspec::Example' do + context 'when given an Example hash' 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(:args) { example_attrs } + let(:expected_first_flaky_at) { Time.now } + let(:expected_last_flaky_at) { Time.now } let(:expected_flaky_reports) { 0 } end end - context 'when given a hash' do + context 'when given a FlakyExample hash' do it_behaves_like 'a valid FlakyExample instance' do let(:args) { flaky_example_attrs } let(:expected_flaky_reports) { flaky_example_attrs[:flaky_reports] } @@ -89,17 +88,17 @@ RSpec.describe RspecFlaky::FlakyExample, :aggregate_failures do freeze_time do flaky_example.update_flakiness! - expect(flaky_example.first_flaky_at).to eq(Time.now) + expect(flaky_example.to_h[:first_flaky_at]).to eq(Time.now) end end it 'maintains the first_flaky_at if exists' do flaky_example.update_flakiness! - expected_first_flaky_at = flaky_example.first_flaky_at + expected_first_flaky_at = flaky_example.to_h[:first_flaky_at] travel_to(Time.now + 42) do flaky_example.update_flakiness! - expect(flaky_example.first_flaky_at).to eq(expected_first_flaky_at) + expect(flaky_example.to_h[:first_flaky_at]).to eq(expected_first_flaky_at) end end @@ -108,53 +107,54 @@ RSpec.describe RspecFlaky::FlakyExample, :aggregate_failures do the_future = Time.now flaky_example.update_flakiness! - expect(flaky_example.last_flaky_at).to eq(the_future) + expect(flaky_example.to_h[:last_flaky_at]).to eq(the_future) end end it 'updates the flaky_reports' do - expected_flaky_reports = flaky_example.first_flaky_at ? flaky_example.flaky_reports + 1 : 1 + expected_flaky_reports = flaky_example.to_h[:first_flaky_at] ? flaky_example.to_h[: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) + expect { flaky_example.update_flakiness! }.to change { flaky_example.to_h[:flaky_reports] }.by(1) + expect(flaky_example.to_h[: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) + expect(flaky_example.to_h[:last_attempts_count]).to eq(42) end end context 'when run on the CI' do + let(:job_url) { 'https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/42' } + before do - stub_env('CI_PROJECT_URL', 'https://gitlab.com/gitlab-org/gitlab-foss') - stub_env('CI_JOB_ID', 42) + stub_env('CI_JOB_URL', job_url) 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') + expect(flaky_example.to_h[:last_flaky_job]).to eq(job_url) end end end - context 'when given an Rspec::Example' do + context 'when given an Example hash' do it_behaves_like 'an up-to-date FlakyExample instance' do - let(:args) { example } + let(:args) { example_attrs } end end - context 'when given a hash' do + context 'when given a FlakyExample hash' do it_behaves_like 'an up-to-date FlakyExample instance' do let(:args) { flaky_example_attrs } end end end - describe '#to_h' do + describe '#to_h', :freeze_time do shared_examples 'a valid FlakyExample hash' do let(:additional_attrs) { {} } @@ -166,17 +166,17 @@ RSpec.describe RspecFlaky::FlakyExample, :aggregate_failures do end end - context 'when given an Rspec::Example' do - let(:args) { example } + context 'when given an Example hash' do + let(:args) { example_attrs } 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 } + { first_flaky_at: Time.now, last_flaky_at: Time.now, last_flaky_job: nil, flaky_reports: 0 } end end end - context 'when given a hash' do + context 'when given a FlakyExample hash' do let(:args) { flaky_example_attrs } it_behaves_like 'a valid FlakyExample hash' diff --git a/spec/tooling/rspec_flaky/flaky_examples_collection_spec.rb b/spec/tooling/rspec_flaky/flaky_examples_collection_spec.rb index 823459e31b4..e5f985c9596 100644 --- a/spec/tooling/rspec_flaky/flaky_examples_collection_spec.rb +++ b/spec/tooling/rspec_flaky/flaky_examples_collection_spec.rb @@ -1,8 +1,10 @@ # frozen_string_literal: true +require_relative '../../support/time_travel' + require_relative '../../../tooling/rspec_flaky/flaky_examples_collection' -RSpec.describe RspecFlaky::FlakyExamplesCollection, :aggregate_failures do +RSpec.describe RspecFlaky::FlakyExamplesCollection, :aggregate_failures, :freeze_time do let(:collection_hash) do { a: { example_id: 'spec/foo/bar_spec.rb:2' }, @@ -14,15 +16,19 @@ RSpec.describe RspecFlaky::FlakyExamplesCollection, :aggregate_failures do { a: { example_id: 'spec/foo/bar_spec.rb:2', - first_flaky_at: nil, - last_flaky_at: nil, - last_flaky_job: nil + first_flaky_at: Time.now, + last_flaky_at: Time.now, + last_flaky_job: nil, + flaky_reports: 0, + last_attempts_count: nil }, b: { example_id: 'spec/foo/baz_spec.rb:3', - first_flaky_at: nil, - last_flaky_at: nil, - last_flaky_job: nil + first_flaky_at: Time.now, + last_flaky_at: Time.now, + last_flaky_job: nil, + flaky_reports: 0, + last_attempts_count: nil } } end @@ -59,9 +65,11 @@ RSpec.describe RspecFlaky::FlakyExamplesCollection, :aggregate_failures do 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 + first_flaky_at: Time.now, + last_flaky_at: Time.now, + last_flaky_job: nil, + flaky_reports: 0, + last_attempts_count: nil }) end diff --git a/spec/tooling/rspec_flaky/listener_spec.rb b/spec/tooling/rspec_flaky/listener_spec.rb index 429724a20cf..51a815dafbf 100644 --- a/spec/tooling/rspec_flaky/listener_spec.rb +++ b/spec/tooling/rspec_flaky/listener_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require 'active_support/testing/time_helpers' require_relative '../../support/helpers/stub_env' +require_relative '../../support/time_travel' require_relative '../../../tooling/rspec_flaky/listener' @@ -53,8 +53,7 @@ RSpec.describe RspecFlaky::Listener, :aggregate_failures do 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('CI_JOB_URL', nil) stub_env('SUITE_FLAKY_RSPEC_REPORT_PATH', nil) end @@ -217,7 +216,7 @@ RSpec.describe RspecFlaky::Listener, :aggregate_failures do 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(RspecFlaky::Report).to receive(:new).with(listener.__send__(:new_flaky_examples)).and_return(report2) expect(report2).to receive(:write).with(RspecFlaky::Config.new_flaky_examples_report_path) listener.dump_summary(nil) diff --git a/spec/tooling/rspec_flaky/report_spec.rb b/spec/tooling/rspec_flaky/report_spec.rb index 6c364cd5cd3..ffd0cd987aa 100644 --- a/spec/tooling/rspec_flaky/report_spec.rb +++ b/spec/tooling/rspec_flaky/report_spec.rb @@ -2,9 +2,11 @@ require 'tempfile' +require_relative '../../support/time_travel' + require_relative '../../../tooling/rspec_flaky/report' -RSpec.describe RspecFlaky::Report, :aggregate_failures do +RSpec.describe RspecFlaky::Report, :aggregate_failures, :freeze_time do let(:thirty_one_days) { 3600 * 24 * 31 } let(:collection_hash) do { |