Skip to content

Commit f0ff1bf

Browse files
committed
Implement the main class of test coverage badge
1 parent f3de46e commit f0ff1bf

File tree

2 files changed

+98
-9
lines changed

2 files changed

+98
-9
lines changed

lib/gitlab/badge/coverage/report.rb

+30-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,42 @@ module Coverage
55
# Test coverage report badge
66
#
77
class Report < Badge::Base
8+
attr_reader :project, :ref, :job
9+
810
def initialize(project, ref, job = nil)
911
@project = project
1012
@ref = ref
1113
@job = job
14+
15+
@pipeline = @project.pipelines
16+
.where(ref: @ref)
17+
.where(sha: @project.commit(@ref).try(:sha))
18+
.first
1219
end
1320

14-
def coverage
21+
def entity
22+
'coverage'
23+
end
24+
25+
def status
26+
@coverage ||= raw_coverage
27+
return unless @coverage
28+
29+
@coverage.to_i
30+
end
31+
32+
private
33+
34+
def raw_coverage
35+
return unless @pipeline
36+
37+
if @job.blank?
38+
@pipeline.coverage
39+
else
40+
@pipeline.builds
41+
.find_by(name: @job)
42+
.try(:coverage)
43+
end
1544
end
1645
end
1746
end

spec/lib/gitlab/badge/coverage/report_spec.rb

+68-8
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,80 @@
22

33
describe Gitlab::Badge::Coverage::Report do
44
let(:project) { create(:project) }
5+
let(:job_name) { nil }
56

6-
let(:pipeline) do
7-
create(:ci_pipeline, project: project,
8-
sha: project.commit.id,
9-
ref: 'master')
7+
let(:badge) do
8+
described_class.new(project, 'master', job_name)
109
end
1110

12-
let(:badge) do
13-
described_class.new(project, 'master')
11+
describe '#entity' do
12+
it 'describes a coverage' do
13+
expect(badge.entity).to eq 'coverage'
14+
end
15+
end
16+
17+
shared_examples 'unknown coverage report' do
18+
context 'particular job specified' do
19+
let(:job_name) { '' }
20+
21+
it 'returns nil' do
22+
expect(badge.status).to be_nil
23+
end
24+
end
25+
26+
context 'particular job not specified' do
27+
let(:job_name) { nil }
28+
29+
it 'returns nil' do
30+
expect(badge.status).to be_nil
31+
end
32+
end
1433
end
1534

16-
context 'builds exist' do
35+
context 'pipeline exists' do
36+
let!(:pipeline) do
37+
create(:ci_pipeline, project: project,
38+
sha: project.commit.id,
39+
ref: 'master')
40+
end
41+
42+
context 'builds exist' do
43+
before do
44+
create(:ci_build, name: 'first', pipeline: pipeline, coverage: 40)
45+
create(:ci_build, pipeline: pipeline, coverage: 60)
46+
end
47+
48+
context 'particular job specified' do
49+
let(:job_name) { 'first' }
50+
51+
it 'returns coverage for the particular job' do
52+
expect(badge.status).to eq 40
53+
end
54+
end
55+
56+
context 'particular job not specified' do
57+
let(:job_name) { '' }
58+
59+
it 'returns arithemetic mean for the pipeline' do
60+
expect(badge.status).to eq 50
61+
end
62+
end
63+
end
64+
65+
context 'builds do not exist' do
66+
it_behaves_like 'unknown coverage report'
67+
68+
context 'particular job specified' do
69+
let(:job_name) { 'nonexistent' }
70+
71+
it 'retruns nil' do
72+
expect(badge.status).to be_nil
73+
end
74+
end
75+
end
1776
end
1877

19-
context 'builds do not exist' do
78+
context 'pipeline does not exist' do
79+
it_behaves_like 'unknown coverage report'
2080
end
2181
end

0 commit comments

Comments
 (0)