Commit f0ff1bfd authored by Grzegorz Bizon's avatar Grzegorz Bizon

Implement the main class of test coverage badge

parent f3de46e6
...@@ -5,13 +5,42 @@ module Gitlab ...@@ -5,13 +5,42 @@ module Gitlab
# Test coverage report badge # Test coverage report badge
# #
class Report < Badge::Base class Report < Badge::Base
attr_reader :project, :ref, :job
def initialize(project, ref, job = nil) def initialize(project, ref, job = nil)
@project = project @project = project
@ref = ref @ref = ref
@job = job @job = job
@pipeline = @project.pipelines
.where(ref: @ref)
.where(sha: @project.commit(@ref).try(:sha))
.first
end
def entity
'coverage'
end
def status
@coverage ||= raw_coverage
return unless @coverage
@coverage.to_i
end end
def coverage private
def raw_coverage
return unless @pipeline
if @job.blank?
@pipeline.coverage
else
@pipeline.builds
.find_by(name: @job)
.try(:coverage)
end
end end
end end
end end
......
...@@ -2,20 +2,80 @@ require 'spec_helper' ...@@ -2,20 +2,80 @@ require 'spec_helper'
describe Gitlab::Badge::Coverage::Report do describe Gitlab::Badge::Coverage::Report do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:job_name) { nil }
let(:pipeline) do let(:badge) do
described_class.new(project, 'master', job_name)
end
describe '#entity' do
it 'describes a coverage' do
expect(badge.entity).to eq 'coverage'
end
end
shared_examples 'unknown coverage report' do
context 'particular job specified' do
let(:job_name) { '' }
it 'returns nil' do
expect(badge.status).to be_nil
end
end
context 'particular job not specified' do
let(:job_name) { nil }
it 'returns nil' do
expect(badge.status).to be_nil
end
end
end
context 'pipeline exists' do
let!(:pipeline) do
create(:ci_pipeline, project: project, create(:ci_pipeline, project: project,
sha: project.commit.id, sha: project.commit.id,
ref: 'master') ref: 'master')
end end
let(:badge) do context 'builds exist' do
described_class.new(project, 'master') before do
create(:ci_build, name: 'first', pipeline: pipeline, coverage: 40)
create(:ci_build, pipeline: pipeline, coverage: 60)
end end
context 'builds exist' do context 'particular job specified' do
let(:job_name) { 'first' }
it 'returns coverage for the particular job' do
expect(badge.status).to eq 40
end
end
context 'particular job not specified' do
let(:job_name) { '' }
it 'returns arithemetic mean for the pipeline' do
expect(badge.status).to eq 50
end
end
end end
context 'builds do not exist' do context 'builds do not exist' do
it_behaves_like 'unknown coverage report'
context 'particular job specified' do
let(:job_name) { 'nonexistent' }
it 'retruns nil' do
expect(badge.status).to be_nil
end
end
end
end
context 'pipeline does not exist' do
it_behaves_like 'unknown coverage report'
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