2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-02-23 04:10:45 -05:00
|
|
|
require_relative '../../support/helpers/stub_env'
|
2021-12-13 16:14:32 -05:00
|
|
|
require_relative '../../support/time_travel'
|
2021-02-23 04:10:45 -05:00
|
|
|
|
|
|
|
require_relative '../../../tooling/rspec_flaky/flaky_example'
|
2017-07-19 13:51:59 -04:00
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe RspecFlaky::FlakyExample, :aggregate_failures do
|
2021-02-23 04:10:45 -05:00
|
|
|
include ActiveSupport::Testing::TimeHelpers
|
|
|
|
include StubENV
|
|
|
|
|
2017-07-19 13:51:59 -04:00
|
|
|
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,
|
2019-09-18 10:02:45 -04:00
|
|
|
last_flaky_job: 'https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/12',
|
2017-07-19 13:51:59 -04:00
|
|
|
last_attempts_count: 2,
|
|
|
|
flaky_reports: 1
|
|
|
|
}
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2017-07-19 13:51:59 -04:00
|
|
|
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
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
before do
|
|
|
|
# Stub these env variables otherwise specs don't behave the same on the CI
|
2021-12-13 16:14:32 -05:00
|
|
|
stub_env('CI_JOB_URL', nil)
|
2017-10-04 07:01:48 -04:00
|
|
|
end
|
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
describe '#initialize', :freeze_time do
|
2017-07-19 13:51:59 -04:00
|
|
|
shared_examples 'a valid FlakyExample instance' do
|
2017-10-04 07:01:48 -04:00
|
|
|
let(:flaky_example) { described_class.new(args) }
|
2017-07-19 13:51:59 -04:00
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
it 'returns valid attributes' do
|
2021-12-13 16:14:32 -05:00
|
|
|
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)
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
context 'when given an Example hash' do
|
2017-10-04 07:01:48 -04:00
|
|
|
it_behaves_like 'a valid FlakyExample instance' do
|
2021-12-13 16:14:32 -05:00
|
|
|
let(:args) { example_attrs }
|
|
|
|
let(:expected_first_flaky_at) { Time.now }
|
|
|
|
let(:expected_last_flaky_at) { Time.now }
|
2017-10-04 07:01:48 -04:00
|
|
|
let(:expected_flaky_reports) { 0 }
|
|
|
|
end
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
context 'when given a FlakyExample hash' do
|
2017-10-04 07:01:48 -04:00
|
|
|
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
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
describe '#update_flakiness!' do
|
|
|
|
shared_examples 'an up-to-date FlakyExample instance' do
|
|
|
|
let(:flaky_example) { described_class.new(args) }
|
2017-07-19 13:51:59 -04:00
|
|
|
|
2021-02-23 04:10:45 -05:00
|
|
|
it 'sets the first_flaky_at if none exists' do
|
|
|
|
args[:first_flaky_at] = nil
|
2017-07-19 13:51:59 -04:00
|
|
|
|
2021-02-23 04:10:45 -05:00
|
|
|
freeze_time do
|
|
|
|
flaky_example.update_flakiness!
|
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
expect(flaky_example.to_h[:first_flaky_at]).to eq(Time.now)
|
2021-02-23 04:10:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'maintains the first_flaky_at if exists' do
|
|
|
|
flaky_example.update_flakiness!
|
2021-12-13 16:14:32 -05:00
|
|
|
expected_first_flaky_at = flaky_example.to_h[:first_flaky_at]
|
2021-02-23 04:10:45 -05:00
|
|
|
|
|
|
|
travel_to(Time.now + 42) do
|
|
|
|
flaky_example.update_flakiness!
|
2021-12-13 16:14:32 -05:00
|
|
|
expect(flaky_example.to_h[:first_flaky_at]).to eq(expected_first_flaky_at)
|
2021-02-23 04:10:45 -05:00
|
|
|
end
|
2017-10-04 07:01:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the last_flaky_at' do
|
2021-02-23 04:10:45 -05:00
|
|
|
travel_to(Time.now + 42) do
|
|
|
|
the_future = Time.now
|
|
|
|
flaky_example.update_flakiness!
|
2017-07-19 13:51:59 -04:00
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
expect(flaky_example.to_h[:last_flaky_at]).to eq(the_future)
|
2021-02-23 04:10:45 -05:00
|
|
|
end
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
it 'updates the flaky_reports' do
|
2021-12-13 16:14:32 -05:00
|
|
|
expected_flaky_reports = flaky_example.to_h[:first_flaky_at] ? flaky_example.to_h[:flaky_reports] + 1 : 1
|
2017-10-04 07:01:48 -04:00
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
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)
|
2017-10-04 07:01:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when passed a :last_attempts_count' do
|
|
|
|
it 'updates the last_attempts_count' do
|
|
|
|
flaky_example.update_flakiness!(last_attempts_count: 42)
|
2017-07-19 13:51:59 -04:00
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
expect(flaky_example.to_h[:last_attempts_count]).to eq(42)
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when run on the CI' do
|
2021-12-13 16:14:32 -05:00
|
|
|
let(:job_url) { 'https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/42' }
|
|
|
|
|
2017-07-19 13:51:59 -04:00
|
|
|
before do
|
2021-12-13 16:14:32 -05:00
|
|
|
stub_env('CI_JOB_URL', job_url)
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
it 'updates the last_flaky_job' do
|
|
|
|
flaky_example.update_flakiness!
|
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
expect(flaky_example.to_h[:last_flaky_job]).to eq(job_url)
|
2017-10-04 07:01:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
context 'when given an Example hash' do
|
2017-10-04 07:01:48 -04:00
|
|
|
it_behaves_like 'an up-to-date FlakyExample instance' do
|
2021-12-13 16:14:32 -05:00
|
|
|
let(:args) { example_attrs }
|
2017-10-04 07:01:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
context 'when given a FlakyExample hash' do
|
2017-10-04 07:01:48 -04:00
|
|
|
it_behaves_like 'an up-to-date FlakyExample instance' do
|
|
|
|
let(:args) { flaky_example_attrs }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
describe '#to_h', :freeze_time do
|
2017-10-04 07:01:48 -04:00
|
|
|
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
|
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
context 'when given an Example hash' do
|
|
|
|
let(:args) { example_attrs }
|
2017-10-04 07:01:48 -04:00
|
|
|
|
|
|
|
it_behaves_like 'a valid FlakyExample hash' do
|
|
|
|
let(:additional_attrs) do
|
2021-12-13 16:14:32 -05:00
|
|
|
{ first_flaky_at: Time.now, last_flaky_at: Time.now, last_flaky_job: nil, flaky_reports: 0 }
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-13 16:14:32 -05:00
|
|
|
context 'when given a FlakyExample hash' do
|
2017-07-19 13:51:59 -04:00
|
|
|
let(:args) { flaky_example_attrs }
|
|
|
|
|
|
|
|
it_behaves_like 'a valid FlakyExample hash'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|