debian-mirror-gitlab/spec/tooling/rspec_flaky/flaky_example_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

149 lines
4.5 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2021-04-17 20:07:23 +05:30
require_relative '../../support/helpers/stub_env'
2022-01-26 12:08:38 +05:30
require_relative '../../support/time_travel'
2021-04-17 20:07:23 +05:30
require_relative '../../../tooling/rspec_flaky/flaky_example'
2018-03-17 18:26:18 +05:30
2020-07-28 23:09:34 +05:30
RSpec.describe RspecFlaky::FlakyExample, :aggregate_failures do
2021-04-17 20:07:23 +05:30
include ActiveSupport::Testing::TimeHelpers
include StubENV
2023-01-13 00:05:48 +05:30
let(:example_attrs) do
2018-03-17 18:26:18 +05:30
{
example_id: 'spec/foo/bar_spec.rb:2',
file: 'spec/foo/bar_spec.rb',
line: 2,
description: 'hello world',
last_attempts_count: 2,
2023-01-13 00:05:48 +05:30
feature_category: :feature_category
2018-03-17 18:26:18 +05:30
}
end
2020-10-24 23:57:45 +05:30
2018-03-17 18:26:18 +05:30
before do
# Stub these env variables otherwise specs don't behave the same on the CI
2022-01-26 12:08:38 +05:30
stub_env('CI_JOB_URL', nil)
2018-03-17 18:26:18 +05:30
end
2022-01-26 12:08:38 +05:30
describe '#initialize', :freeze_time do
2018-03-17 18:26:18 +05:30
shared_examples 'a valid FlakyExample instance' do
let(:flaky_example) { described_class.new(args) }
it 'returns valid attributes' do
2022-01-26 12:08:38 +05:30
attrs = flaky_example.to_h
2023-01-13 00:05:48 +05:30
expect(attrs[:uid]).to eq(example_attrs[:uid])
expect(attrs[:file]).to eq(example_attrs[:file])
expect(attrs[:line]).to eq(example_attrs[:line])
expect(attrs[:description]).to eq(example_attrs[:description])
expect(attrs[:feature_category]).to eq(example_attrs[:feature_category])
2022-01-26 12:08:38 +05:30
expect(attrs[:first_flaky_at]).to eq(expected_first_flaky_at)
expect(attrs[:last_flaky_at]).to eq(expected_last_flaky_at)
2023-01-13 00:05:48 +05:30
expect(attrs[:last_attempts_count]).to eq(example_attrs[:last_attempts_count])
2022-01-26 12:08:38 +05:30
expect(attrs[:flaky_reports]).to eq(expected_flaky_reports)
2018-03-17 18:26:18 +05:30
end
end
2023-01-13 00:05:48 +05:30
context 'when given an Example.to_h' do
2018-03-17 18:26:18 +05:30
it_behaves_like 'a valid FlakyExample instance' do
2022-01-26 12:08:38 +05:30
let(:args) { example_attrs }
let(:expected_first_flaky_at) { Time.now }
let(:expected_last_flaky_at) { Time.now }
2018-03-17 18:26:18 +05:30
let(:expected_flaky_reports) { 0 }
end
end
end
2023-01-13 00:05:48 +05:30
describe '#update!' do
2018-03-17 18:26:18 +05:30
shared_examples 'an up-to-date FlakyExample instance' do
let(:flaky_example) { described_class.new(args) }
2021-04-17 20:07:23 +05:30
it 'sets the first_flaky_at if none exists' do
args[:first_flaky_at] = nil
2018-03-17 18:26:18 +05:30
2021-04-17 20:07:23 +05:30
freeze_time do
2023-01-13 00:05:48 +05:30
flaky_example.update!(example_attrs)
2021-04-17 20:07:23 +05:30
2022-01-26 12:08:38 +05:30
expect(flaky_example.to_h[:first_flaky_at]).to eq(Time.now)
2021-04-17 20:07:23 +05:30
end
end
it 'maintains the first_flaky_at if exists' do
2023-01-13 00:05:48 +05:30
flaky_example.update!(example_attrs)
2022-01-26 12:08:38 +05:30
expected_first_flaky_at = flaky_example.to_h[:first_flaky_at]
2021-04-17 20:07:23 +05:30
travel_to(Time.now + 42) do
2023-01-13 00:05:48 +05:30
flaky_example.update!(example_attrs)
2022-01-26 12:08:38 +05:30
expect(flaky_example.to_h[:first_flaky_at]).to eq(expected_first_flaky_at)
2021-04-17 20:07:23 +05:30
end
2018-03-17 18:26:18 +05:30
end
it 'updates the last_flaky_at' do
2021-04-17 20:07:23 +05:30
travel_to(Time.now + 42) do
the_future = Time.now
2023-01-13 00:05:48 +05:30
flaky_example.update!(example_attrs)
2018-03-17 18:26:18 +05:30
2022-01-26 12:08:38 +05:30
expect(flaky_example.to_h[:last_flaky_at]).to eq(the_future)
2021-04-17 20:07:23 +05:30
end
2018-03-17 18:26:18 +05:30
end
it 'updates the flaky_reports' do
2022-01-26 12:08:38 +05:30
expected_flaky_reports = flaky_example.to_h[:first_flaky_at] ? flaky_example.to_h[:flaky_reports] + 1 : 1
2018-03-17 18:26:18 +05:30
2023-01-13 00:05:48 +05:30
expect { flaky_example.update!(example_attrs) }.to change { flaky_example.to_h[:flaky_reports] }.by(1)
2022-01-26 12:08:38 +05:30
expect(flaky_example.to_h[:flaky_reports]).to eq(expected_flaky_reports)
2018-03-17 18:26:18 +05:30
end
2023-01-13 00:05:48 +05:30
it 'updates the last_attempts_count' do
example_attrs[:last_attempts_count] = 42
flaky_example.update!(example_attrs)
2018-03-17 18:26:18 +05:30
2023-01-13 00:05:48 +05:30
expect(flaky_example.to_h[:last_attempts_count]).to eq(42)
2018-03-17 18:26:18 +05:30
end
context 'when run on the CI' do
2022-01-26 12:08:38 +05:30
let(:job_url) { 'https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/42' }
2018-03-17 18:26:18 +05:30
before do
2022-01-26 12:08:38 +05:30
stub_env('CI_JOB_URL', job_url)
2018-03-17 18:26:18 +05:30
end
it 'updates the last_flaky_job' do
2023-01-13 00:05:48 +05:30
flaky_example.update!(example_attrs)
2018-03-17 18:26:18 +05:30
2022-01-26 12:08:38 +05:30
expect(flaky_example.to_h[:last_flaky_job]).to eq(job_url)
2018-03-17 18:26:18 +05:30
end
end
end
2022-01-26 12:08:38 +05:30
context 'when given an Example hash' do
2018-03-17 18:26:18 +05:30
it_behaves_like 'an up-to-date FlakyExample instance' do
2022-01-26 12:08:38 +05:30
let(:args) { example_attrs }
2018-03-17 18:26:18 +05:30
end
end
end
2022-01-26 12:08:38 +05:30
describe '#to_h', :freeze_time do
2018-03-17 18:26:18 +05:30
shared_examples 'a valid FlakyExample hash' do
let(:additional_attrs) { {} }
it 'returns a valid hash' do
flaky_example = described_class.new(args)
2023-01-13 00:05:48 +05:30
final_hash = example_attrs.merge(additional_attrs)
2018-03-17 18:26:18 +05:30
expect(flaky_example.to_h).to eq(final_hash)
end
end
2022-01-26 12:08:38 +05:30
context 'when given an Example hash' do
let(:args) { example_attrs }
2018-03-17 18:26:18 +05:30
it_behaves_like 'a valid FlakyExample hash' do
let(:additional_attrs) do
2022-01-26 12:08:38 +05:30
{ first_flaky_at: Time.now, last_flaky_at: Time.now, last_flaky_job: nil, flaky_reports: 0 }
2018-03-17 18:26:18 +05:30
end
end
end
end
end