summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/badge/coverage/report_spec.rb
blob: da789bf370577099e3dc3a33cec0cc9b25df0f4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'spec_helper'

describe Gitlab::Badge::Coverage::Report do
  let(:project) { create(:project, :repository) }
  let(:job_name) { nil }

  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

  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

  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 '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
        expect(badge.status).to eq 40
      end
    end

    context 'when 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 'when only failed pipeline exists' do
    before do
      create_pipeline do |pipeline|
        create(:ci_build, :failed, pipeline: pipeline, coverage: 10)
      end
    end

    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

  context 'pipeline does not exist' do
    it_behaves_like 'unknown coverage report'
  end

  def create_pipeline
    opts = { project: project, sha: project.commit.id, ref: 'master' }

    create(:ci_pipeline, opts).tap do |pipeline|
      yield pipeline
      pipeline.update_status
    end
  end
end