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

code_climate_spec.rb « codequality « parsers « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1ef341ff863320b0093c27601fdf154ffe95e0bc (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Parsers::Codequality::CodeClimate do
  describe '#parse!' do
    subject(:parse) { described_class.new.parse!(code_climate, codequality_report, metadata) }

    let(:codequality_report) { Gitlab::Ci::Reports::CodequalityReports.new }
    let(:code_climate) do
      [
        {
          "categories": [
            "Complexity"
          ],
          "check_name": "argument_count",
          "content": {
            "body": ""
          },
          "description": "Method `new_array` has 12 arguments (exceeds 4 allowed). Consider refactoring.",
          "fingerprint": "15cdb5c53afd42bc22f8ca366a08d547",
          "location": {
            "path": "foo.rb",
            "lines": {
              "begin": 10,
              "end": 10
            }
          },
          "other_locations": [],
          "remediation_points": 900000,
          "severity": "major",
          "type": "issue",
          "engine_name": "structure"
        }
      ].to_json
    end

    let_it_be(:group) { create(:group, name: 'test-group') }
    let_it_be(:project) { create(:project, path: 'test-project', group: group) }
    let(:metadata) do
      {
        project: project,
        commit_sha: 'f0cc5229e2aa5e9429f1b17a3b3b102f21d7fe31'
      }
    end

    context "when data is code_climate style JSON" do
      context "when there are no degradations" do
        let(:code_climate) { [].to_json }

        it "returns a codequality report" do
          expect { parse }.not_to raise_error

          expect(codequality_report.degradations_count).to eq(0)
        end
      end

      context "when there are degradations" do
        it "returns a codequality report" do
          expect { parse }.not_to raise_error

          expect(codequality_report.degradations_count).to eq(1)
        end
      end
    end

    context "when data is not a valid JSON string" do
      let(:code_climate) do
        [
          {
            "categories": [
              "Complexity"
            ],
            "check_name": "argument_count",
            "content": {
              "body": ""
            },
            "description": "Method `new_array` has 12 arguments (exceeds 4 allowed). Consider refactoring.",
            "fingerprint": "15cdb5c53afd42bc22f8ca366a08d547",
            "location": {
              "path": "foo.rb",
              "lines": {
                "begin": 10,
                "end": 10
              }
            },
            "other_locations": [],
            "remediation_points": 900000,
            "severity": "major",
            "type": "issue",
            "engine_name": "structure"
          }
        ]
      end

      it "sets error_message" do
        expect { parse }.not_to raise_error

        expect(codequality_report.error_message).to include('JSON parsing failed')
      end
    end

    context 'when degradations contain an invalid one' do
      let(:code_climate) do
        [
          {
          "type": "Issue",
          "check_name": "Rubocop/Metrics/ParameterLists",
          "description": "Avoid parameter lists longer than 5 parameters. [12/5]",
          "fingerprint": "ab5f8b935886b942d621399aefkaehfiaehf",
          "severity": "minor"
          },
          {
            "categories": [
              "Complexity"
            ],
            "check_name": "argument_count",
            "content": {
              "body": ""
            },
            "description": "Method `new_array` has 12 arguments (exceeds 4 allowed). Consider refactoring.",
            "fingerprint": "15cdb5c53afd42bc22f8ca366a08d547",
            "location": {
              "path": "foo.rb",
              "lines": {
                "begin": 10,
                "end": 10
              }
            },
            "other_locations": [],
            "remediation_points": 900000,
            "severity": "major",
            "type": "issue",
            "engine_name": "structure"
          }
        ].to_json
      end

      it 'stops parsing the report' do
        expect { parse }.not_to raise_error

        expect(codequality_report.degradations_count).to eq(0)
      end
    end

    context 'for web_url' do
      let(:code_climate) do
        [
          {
            "categories": [
              "Complexity"
            ],
            "check_name": "argument_count",
            "content": {
              "body": ""
            },
            "description": "Method `new_array` has 12 arguments (exceeds 4 allowed). Consider refactoring.",
            "fingerprint": "15cdb5c53afd42bc22f8ca366a08d547",
            "location": {
              "path": "foo.rb",
              "lines": {
                "begin": 10,
                "end": 10
              }
            },
            "other_locations": [],
            "remediation_points": 900000,
            "severity": "major",
            "type": "issue",
            "engine_name": "structure"
          }
        ].to_json
      end

      context 'when metadata has project and commit_sha' do
        it 'adds a non nil url' do
          want = 'http://localhost/test-group/test-project/-/blob/f0cc5229e2aa5e9429f1b17a3b3b102f21d7fe31/foo.rb#L10'
          expect { parse }.not_to raise_error

          expect(codequality_report.degradations_count).to eq(1)
          expect(codequality_report.all_degradations[0]['web_url']).to eq(want)
        end
      end

      context 'when metadata does not have project and commit_sha' do
        let(:metadata) { {} }

        it 'adds a nil url' do
          expect { parse }.not_to raise_error

          expect(codequality_report.degradations_count).to eq(1)
          expect(codequality_report.all_degradations[0]['web_url']).to be_nil
        end
      end
    end
  end
end