2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Badge::Coverage::Report do
|
2021-01-29 00:20:46 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:success_pipeline) { create(:ci_pipeline, :success, project: project) }
|
|
|
|
let_it_be(:running_pipeline) { create(:ci_pipeline, :running, project: project) }
|
|
|
|
let_it_be(:failure_pipeline) { create(:ci_pipeline, :failed, project: project) }
|
|
|
|
|
|
|
|
let_it_be(:builds) do
|
|
|
|
[
|
|
|
|
create(:ci_build, :success, pipeline: success_pipeline, coverage: 40, created_at: 9.seconds.ago, name: 'coverage'),
|
|
|
|
create(:ci_build, :success, pipeline: success_pipeline, coverage: 60, created_at: 8.seconds.ago)
|
|
|
|
]
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
let(:badge) do
|
2020-06-23 00:09:42 +05:30
|
|
|
described_class.new(project, 'master', opts: { job: job_name })
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
let(:job_name) { nil }
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
describe '#entity' do
|
|
|
|
it 'describes a coverage' do
|
|
|
|
expect(badge.entity).to eq 'coverage'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#metadata' do
|
|
|
|
it 'returns correct metadata' do
|
|
|
|
expect(badge.metadata.image_url).to include 'coverage.svg'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#template' do
|
|
|
|
it 'returns correct template' do
|
|
|
|
expect(badge.template.key_text).to eq 'coverage'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
describe '#status' do
|
|
|
|
context 'with no job specified' do
|
|
|
|
it 'returns the most recent successful pipeline coverage value' do
|
|
|
|
expect(badge.status).to eq(50.00)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'and no successful pipelines' do
|
|
|
|
before do
|
|
|
|
allow(badge).to receive(:successful_pipeline).and_return(nil)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'returns nil' do
|
|
|
|
expect(badge.status).to eq(nil)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'with a blank job name' do
|
|
|
|
let(:job_name) { ' ' }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'returns the latest successful pipeline coverage value' do
|
|
|
|
expect(badge.status).to eq(50.00)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'with an unmatching job name specified' do
|
|
|
|
let(:job_name) { 'incorrect name' }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'returns nil' do
|
|
|
|
expect(badge.status).to be_nil
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'with a matching job name specified' do
|
|
|
|
let(:job_name) { 'coverage' }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'returns the pipeline coverage value' do
|
|
|
|
expect(badge.status).to eq(40.00)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'with a more recent running pipeline' do
|
|
|
|
let!(:another_build) { create(:ci_build, :success, pipeline: running_pipeline, coverage: 20, created_at: 7.seconds.ago, name: 'coverage') }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'returns the running pipeline coverage value' do
|
|
|
|
expect(badge.status).to eq(20.00)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'with a more recent failed pipeline' do
|
|
|
|
let!(:another_build) { create(:ci_build, :success, pipeline: failure_pipeline, coverage: 10, created_at: 6.seconds.ago, name: 'coverage') }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'returns the failed pipeline coverage value' do
|
|
|
|
expect(badge.status).to eq(10.00)
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|