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: 6a08e8f0b7f79810860d3535402cae1952cde2d9 (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
# 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) }

    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

    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
  end
end