2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe 'test coverage badge' do
|
2018-11-08 19:23:39 +05:30
|
|
|
let!(:user) { create(:user) }
|
|
|
|
let!(:project) { create(:project, :private) }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
context 'when user has access to view badge' do
|
2018-11-08 19:23:39 +05:30
|
|
|
before do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_developer(user)
|
2017-09-10 17:25:29 +05:30
|
|
|
sign_in(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
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
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'user requests coverage badge image for pipeline' do
|
2016-09-13 17:45:13 +05:30
|
|
|
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
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
expect_coverage_badge_color(:good)
|
2018-03-17 18:26:18 +05:30
|
|
|
expect_coverage_badge('95.00%')
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'user requests coverage badge for specific job' do
|
2016-09-13 17:45:13 +05:30
|
|
|
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')
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
expect_coverage_badge_color(:medium)
|
2018-03-17 18:26:18 +05:30
|
|
|
expect_coverage_badge('85.00%')
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'user requests coverage badge for pipeline without coverage' do
|
2016-09-13 17:45:13 +05:30
|
|
|
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
|
2018-11-08 19:23:39 +05:30
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'user requests test coverage badge image' do
|
2016-09-13 17:45:13 +05:30
|
|
|
show_test_coverage_badge
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(page).to have_gitlab_http_status(:not_found)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_pipeline
|
2017-09-10 17:25:29 +05:30
|
|
|
opts = { project: project }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
create(:ci_pipeline, opts).tap do |pipeline|
|
|
|
|
yield pipeline
|
2020-10-24 23:57:45 +05:30
|
|
|
::Ci::ProcessPipelineService.new(pipeline).execute
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_build(pipeline, coverage:, name:)
|
|
|
|
opts = { pipeline: pipeline, coverage: coverage, name: name }
|
|
|
|
|
|
|
|
create(:ci_build, :success, opts)
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
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)
|
2016-09-13 17:45:13 +05:30
|
|
|
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
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
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
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|