Welcome to mirror list, hosted at ThFree Co, Russian Federation.

coverage_spec.rb « badges « projects « features « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7555e567c372e23a078b0d1135d34051b9b6e44c (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'test coverage badge' do
  let!(:user) { create(:user) }
  let!(:project) { create(:project, :private) }

  context 'when user has access to view badge' do
    before do
      project.add_developer(user)
      sign_in(user)
    end

    it 'user requests coverage badge image for pipeline with custom limits - 80% good' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 80, name: 'test:1')
      end

      show_test_coverage_badge(min_good: 75, min_acceptable: 50, min_medium: 25)

      expect_coverage_badge_color(:good)
      expect_coverage_badge('80.00%')
    end

    it 'user requests coverage badge image for pipeline with custom limits - 74% - bad config' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 74, name: 'test:1')
      end
      # User sets a minimum good value that is lower than min acceptable and min medium,
      # in which case we force the min acceptable value to be min good -1 and min medium value to be min acceptable -1
      show_test_coverage_badge(min_good: 75, min_acceptable: 76, min_medium: 77)

      expect_coverage_badge_color(:acceptable)
      expect_coverage_badge('74.00%')
    end

    it 'user requests coverage badge image for pipeline with custom limits - 73% - bad config' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 73, name: 'test:1')
      end
      # User sets a minimum good value that is lower than min acceptable and min medium,
      # in which case we force the min acceptable value to be min good -1 and min medium value to be min acceptable -1
      show_test_coverage_badge(min_good: 75, min_acceptable: 76, min_medium: 77)

      expect_coverage_badge_color(:medium)
      expect_coverage_badge('73.00%')
    end

    it 'user requests coverage badge image for pipeline with custom limits - 72% - partial config - low' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 72, name: 'test:1')
      end
      # User only sets good to 75 and leaves the others on the default settings,
      # in which case we force the min acceptable value to be min good -1 and min medium value to be min acceptable -1
      show_test_coverage_badge(min_good: 75)

      expect_coverage_badge_color(:low)
      expect_coverage_badge('72.00%')
    end

    it 'user requests coverage badge image for pipeline with custom limits - 72% - partial config - medium' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 72, name: 'test:1')
      end
      # User only sets good to 74 and leaves the others on the default settings,
      # in which case we force the min acceptable value to be min good -1 and min medium value to be min acceptable -1
      show_test_coverage_badge(min_good: 74)

      expect_coverage_badge_color(:medium)
      expect_coverage_badge('72.00%')
    end

    it 'user requests coverage badge image for pipeline with custom limits - 72% - partial config - medium v2' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 72, name: 'test:1')
      end
      # User only sets medium to 72 and leaves the others on the defaults good as 95 and acceptable as 90
      show_test_coverage_badge(min_medium: 72)

      expect_coverage_badge_color(:medium)
      expect_coverage_badge('72.00%')
    end

    it 'user requests coverage badge image for pipeline with custom limits - 70% acceptable' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 70, name: 'test:1')
      end

      show_test_coverage_badge(min_good: 75, min_acceptable: 50, min_medium: 25)

      expect_coverage_badge_color(:acceptable)
      expect_coverage_badge('70.00%')
    end

    it 'user requests coverage badge image for pipeline with custom limits - 30% medium' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 30, name: 'test:1')
      end

      show_test_coverage_badge(min_good: 75, min_acceptable: 50, min_medium: 25)

      expect_coverage_badge_color(:medium)
      expect_coverage_badge('30.00%')
    end

    it 'user requests coverage badge image for pipeline with custom limits - 20% low' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 20, name: 'test:1')
      end

      show_test_coverage_badge(min_good: 75, min_acceptable: 50, min_medium: 25)

      expect_coverage_badge_color(:low)
      expect_coverage_badge('20.00%')
    end

    it 'user requests coverage badge image for pipeline with custom limits - nonsense values which use the defaults' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 92, name: 'test:1')
      end

      show_test_coverage_badge(min_good: "nonsense", min_acceptable: "rubbish", min_medium: "NaN")

      expect_coverage_badge_color(:acceptable)
      expect_coverage_badge('92.00%')
    end

    it 'user requests coverage badge image for pipeline' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 100, name: 'test:1')
        create_build(pipeline, coverage: 90, name: 'test:2')
      end

      show_test_coverage_badge

      expect_coverage_badge_color(:good)
      expect_coverage_badge('95.00%')
    end

    it 'user requests coverage badge for specific job' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 50, name: 'test:1')
        create_build(pipeline, coverage: 50, name: 'test:2')
        create_build(pipeline, coverage: 85, name: 'coverage')
      end

      show_test_coverage_badge(job: 'coverage')

      expect_coverage_badge_color(:medium)
      expect_coverage_badge('85.00%')
    end

    it 'user requests coverage badge for pipeline without coverage' do
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: nil, name: 'test')
      end

      show_test_coverage_badge

      expect_coverage_badge('unknown')
    end
  end

  context 'when user does not have access to view badge' do
    before do
      sign_in(user)
    end

    it 'user requests test coverage badge image' do
      show_test_coverage_badge

      expect(page).to have_gitlab_http_status(:not_found)
    end
  end

  def create_pipeline
    opts = { project: project }

    create(:ci_pipeline, opts).tap do |pipeline|
      yield pipeline
      ::Ci::ProcessPipelineService.new(pipeline).execute
    end
  end

  def create_build(pipeline, coverage:, name:)
    opts = { pipeline: pipeline, coverage: coverage, name: name }

    create(:ci_build, :success, opts)
  end

  def show_test_coverage_badge(job: nil, min_good: nil, min_acceptable: nil, min_medium: nil)
    visit coverage_project_badges_path(project, ref: :master, job: job, min_good: min_good,
                                                min_acceptable: min_acceptable, min_medium: min_medium, format: :svg)
  end

  def expect_coverage_badge(coverage)
    svg = Nokogiri::XML.parse(page.body)
    expect(page.response_headers['Content-Type']).to include('image/svg+xml')
    expect(svg.at(%Q{text:contains("#{coverage}")})).to be_truthy
  end

  def expect_coverage_badge_color(color)
    svg = Nokogiri::HTML(page.body)
    expect(page.response_headers['Content-Type']).to include('image/svg+xml')
    badge_color = svg.xpath("//path[starts-with(@d, 'M62')]")[0].attributes['fill'].to_s
    expected_badge_color = Gitlab::Ci::Badge::Coverage::Template::STATUS_COLOR[color]
    expect(badge_color).to eq(expected_badge_color)
  end
end