summaryrefslogtreecommitdiff
path: root/spec/tooling/rspec_flaky/flaky_example_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/tooling/rspec_flaky/flaky_example_spec.rb')
-rw-r--r--spec/tooling/rspec_flaky/flaky_example_spec.rb76
1 files changed, 38 insertions, 38 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'