diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2016-08-11 13:38:03 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2016-08-15 15:39:46 +0300 |
commit | f0ff1bfdcc43decd1888f7b8d4a9e8c4dd5540d9 (patch) | |
tree | 0ed2fa8531ec046c9adc01c6086c13246231d099 | |
parent | f3de46e6b0d4cc61e00c884753a8c9eec66f66c4 (diff) |
Implement the main class of test coverage badge
-rw-r--r-- | lib/gitlab/badge/coverage/report.rb | 31 | ||||
-rw-r--r-- | spec/lib/gitlab/badge/coverage/report_spec.rb | 76 |
2 files changed, 98 insertions, 9 deletions
diff --git a/lib/gitlab/badge/coverage/report.rb b/lib/gitlab/badge/coverage/report.rb index e6de15e085f..f06142003e3 100644 --- a/lib/gitlab/badge/coverage/report.rb +++ b/lib/gitlab/badge/coverage/report.rb @@ -5,13 +5,42 @@ module Gitlab # Test coverage report badge # class Report < Badge::Base + attr_reader :project, :ref, :job + def initialize(project, ref, job = nil) @project = project @ref = ref @job = job + + @pipeline = @project.pipelines + .where(ref: @ref) + .where(sha: @project.commit(@ref).try(:sha)) + .first end - def coverage + def entity + 'coverage' + end + + def status + @coverage ||= raw_coverage + return unless @coverage + + @coverage.to_i + end + + 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 diff --git a/spec/lib/gitlab/badge/coverage/report_spec.rb b/spec/lib/gitlab/badge/coverage/report_spec.rb index 57b89dd8cda..46b14873ee9 100644 --- a/spec/lib/gitlab/badge/coverage/report_spec.rb +++ b/spec/lib/gitlab/badge/coverage/report_spec.rb @@ -2,20 +2,80 @@ require 'spec_helper' describe Gitlab::Badge::Coverage::Report do let(:project) { create(:project) } + let(:job_name) { nil } - let(:pipeline) do - create(:ci_pipeline, project: project, - sha: project.commit.id, - ref: 'master') + let(:badge) do + described_class.new(project, 'master', job_name) end - let(:badge) do - described_class.new(project, 'master') + 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 'builds exist' do + context 'pipeline exists' do + let!(:pipeline) do + create(:ci_pipeline, project: project, + sha: project.commit.id, + ref: 'master') + end + + context 'builds exist' do + before do + create(:ci_build, name: 'first', pipeline: pipeline, coverage: 40) + create(:ci_build, pipeline: pipeline, coverage: 60) + end + + 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 + + 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 'builds do not exist' do + context 'pipeline does not exist' do + it_behaves_like 'unknown coverage report' end end |