2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-19 13:51:59 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
describe RspecFlaky::FlakyExample, :aggregate_failures do
|
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
|
|
|
|
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) }
|
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
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
|
|
|
|
|
2017-07-19 13:51:59 -04:00
|
|
|
describe '#initialize' do
|
|
|
|
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
|
2017-07-19 13:51:59 -04:00
|
|
|
expect(flaky_example.uid).to eq(flaky_example_attrs[:uid])
|
2017-10-04 07:01:48 -04:00
|
|
|
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)
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when given an Rspec::Example' do
|
2017-10-04 07:01:48 -04:00
|
|
|
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
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when given a 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
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
it 'updates the first_flaky_at' do
|
|
|
|
now = Time.now
|
|
|
|
expected_first_flaky_at = flaky_example.first_flaky_at ? flaky_example.first_flaky_at : now
|
|
|
|
Timecop.freeze(now) { flaky_example.update_flakiness! }
|
2017-07-19 13:51:59 -04:00
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
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! }
|
2017-07-19 13:51:59 -04:00
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
expect(flaky_example.last_flaky_at).to eq(now)
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
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)
|
2017-07-19 13:51:59 -04:00
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
expect(flaky_example.last_attempts_count).to eq(42)
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when run on the CI' do
|
|
|
|
before do
|
2019-09-18 10:02:45 -04:00
|
|
|
stub_env('CI_PROJECT_URL', 'https://gitlab.com/gitlab-org/gitlab-foss')
|
2017-07-19 13:51:59 -04:00
|
|
|
stub_env('CI_JOB_ID', 42)
|
|
|
|
end
|
|
|
|
|
2017-10-04 07:01:48 -04:00
|
|
|
it 'updates the last_flaky_job' do
|
|
|
|
flaky_example.update_flakiness!
|
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
expect(flaky_example.last_flaky_job).to eq('https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/42')
|
2017-10-04 07:01:48 -04:00
|
|
|
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 }
|
2017-07-19 13:51:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when given a hash' do
|
|
|
|
let(:args) { flaky_example_attrs }
|
|
|
|
|
|
|
|
it_behaves_like 'a valid FlakyExample hash'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|