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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Ci::Reports::CodequalityReports do
let(:codequality_report) { described_class.new }
let(:degradation_1) { build(:codequality_degradation_1) }
let(:degradation_2) { build(:codequality_degradation_2) }
it { expect(codequality_report.degradations).to eq({}) }
describe '#add_degradation' do
context 'when there is a degradation' do
before do
codequality_report.add_degradation(degradation_1)
end
it 'adds degradation to codequality report' do
expect(codequality_report.degradations.keys).to eq([degradation_1[:fingerprint]])
expect(codequality_report.degradations.values.size).to eq(1)
end
end
context 'when a required property is missing in the degradation' do
let(:invalid_degradation) do
{
"type": "Issue",
"check_name": "Rubocop/Metrics/ParameterLists",
"description": "Avoid parameter lists longer than 5 parameters. [12/5]",
"fingerprint": "ab5f8b935886b942d621399aefkaehfiaehf",
"severity": "minor"
}.with_indifferent_access
end
it 'sets location as an error' do
codequality_report.add_degradation(invalid_degradation)
end
end
end
describe '#set_error_message' do
context 'when there is an error' do
it 'sets errors' do
codequality_report.set_error_message("error")
expect(codequality_report.error_message).to eq("error")
end
end
end
describe '#degradations_count' do
subject(:degradations_count) { codequality_report.degradations_count }
context 'when there are many degradations' do
before do
codequality_report.add_degradation(degradation_1)
codequality_report.add_degradation(degradation_2)
end
it 'returns the number of degradations' do
expect(degradations_count).to eq(2)
end
end
end
describe '#all_degradations' do
subject(:all_degradations) { codequality_report.all_degradations }
context 'when there are many degradations' do
before do
codequality_report.add_degradation(degradation_1)
codequality_report.add_degradation(degradation_2)
end
it 'returns all degradations' do
expect(all_degradations).to contain_exactly(degradation_1, degradation_2)
end
end
end
describe '#sort_degradations!' do
let(:major) { build(:codequality_degradation, :major) }
let(:minor) { build(:codequality_degradation, :minor) }
let(:blocker) { build(:codequality_degradation, :blocker) }
let(:info) { build(:codequality_degradation, :info) }
let(:major_2) { build(:codequality_degradation, :major) }
let(:critical) { build(:codequality_degradation, :critical) }
let(:uppercase_major) { build(:codequality_degradation, severity: 'MAJOR') }
let(:unknown) { build(:codequality_degradation, severity: 'unknown') }
let(:codequality_report) { described_class.new }
before do
codequality_report.add_degradation(major)
codequality_report.add_degradation(minor)
codequality_report.add_degradation(blocker)
codequality_report.add_degradation(major_2)
codequality_report.add_degradation(info)
codequality_report.add_degradation(critical)
codequality_report.add_degradation(unknown)
codequality_report.sort_degradations!
end
it 'sorts degradations based on severity' do
expect(codequality_report.degradations.values).to eq([
blocker,
critical,
major,
major_2,
minor,
info,
unknown
])
end
context 'with non-existence and uppercase severities' do
let(:other_report) { described_class.new }
let(:non_existent) { build(:codequality_degradation, severity: 'non-existent') }
before do
other_report.add_degradation(blocker)
other_report.add_degradation(uppercase_major)
other_report.add_degradation(minor)
other_report.add_degradation(non_existent)
end
it 'sorts unknown last' do
expect(other_report.degradations.values).to eq([
blocker,
uppercase_major,
minor,
non_existent
])
end
end
end
end
|