Commit e7a9b79d authored by Sean McGivern's avatar Sean McGivern

Merge branch 'coverage-badge-spec-refactor' into 'master'

Refactor coverage badge spec

See merge request gitlab-org/gitlab!46400
parents 77fd9585 cb06951e
...@@ -3,13 +3,22 @@ ...@@ -3,13 +3,22 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Badge::Coverage::Report do RSpec.describe Gitlab::Badge::Coverage::Report do
let(:project) { create(:project, :repository) } let_it_be(:project) { create(:project) }
let(:job_name) { nil } let_it_be(:pipeline) { create(:ci_pipeline, :success, project: project) }
let_it_be(:builds) do
[
create(:ci_build, :success, pipeline: pipeline, coverage: 40, name: 'first'),
create(:ci_build, :success, pipeline: pipeline, coverage: 60)
]
end
let(:badge) do let(:badge) do
described_class.new(project, 'master', opts: { job: job_name }) described_class.new(project, 'master', opts: { job: job_name })
end end
let(:job_name) { nil }
describe '#entity' do describe '#entity' do
it 'describes a coverage' do it 'describes a coverage' do
expect(badge.entity).to eq 'coverage' expect(badge.entity).to eq 'coverage'
...@@ -28,81 +37,47 @@ RSpec.describe Gitlab::Badge::Coverage::Report do ...@@ -28,81 +37,47 @@ RSpec.describe Gitlab::Badge::Coverage::Report do
end end
end end
shared_examples 'unknown coverage report' do describe '#status' do
context 'particular job specified' do before do
let(:job_name) { '' } allow(badge).to receive(:pipeline).and_return(pipeline)
it 'returns nil' do
expect(badge.status).to be_nil
end
end end
context 'particular job not specified' do context 'with no pipeline' do
let(:job_name) { nil } let(:pipeline) { nil }
it 'returns nil' do it 'returns nil' do
expect(badge.status).to be_nil expect(badge.status).to be_nil
end end
end end
end
context 'when latest successful pipeline exists' do
before do
create_pipeline do |pipeline|
create(:ci_build, :success, pipeline: pipeline, name: 'first', coverage: 40)
create(:ci_build, :success, pipeline: pipeline, coverage: 60)
end
create_pipeline do |pipeline|
create(:ci_build, :failed, pipeline: pipeline, coverage: 10)
end
end
context 'when particular job specified' do
let(:job_name) { 'first' }
it 'returns coverage for the particular job' do context 'with no job specified' do
expect(badge.status).to eq 40 it 'returns the pipeline coverage value' do
expect(badge.status).to eq(50.00)
end end
end end
context 'when particular job not specified' do context 'with a blank job name' do
let(:job_name) { '' } let(:job_name) { ' ' }
it 'returns arithemetic mean for the pipeline' do it 'returns the pipeline coverage value' do
expect(badge.status).to eq 50 expect(badge.status).to eq(50.00)
end
end end
end end
context 'when only failed pipeline exists' do context 'with an unmatching job name specified' do
before do let(:job_name) { 'incorrect name' }
create_pipeline do |pipeline|
create(:ci_build, :failed, pipeline: pipeline, coverage: 10)
end
end
it_behaves_like 'unknown coverage report' it 'returns nil' do
context 'particular job specified' do
let(:job_name) { 'nonexistent' }
it 'retruns nil' do
expect(badge.status).to be_nil expect(badge.status).to be_nil
end end
end end
end
context 'pipeline does not exist' do context 'with a matching job name specified' do
it_behaves_like 'unknown coverage report' let(:job_name) { 'first' }
end
def create_pipeline
opts = { project: project, sha: project.commit.id, ref: 'master' }
create(:ci_pipeline, opts).tap do |pipeline| it 'returns the pipeline coverage value' do
yield pipeline expect(badge.status).to eq(40.00)
::Ci::ProcessPipelineService.new(pipeline).execute end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment