summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/parsers/coverage/cobertura_spec.rb
blob: 08a3fbd7867e9bb4a1f398c299634edf8e590a72 (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
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Gitlab::Ci::Parsers::Coverage::Cobertura do
  describe '#parse!' do
    subject { described_class.new.parse!(cobertura, coverage_report) }

    let(:coverage_report) { Gitlab::Ci::Reports::CoverageReports.new }

    context 'when data is Cobertura style XML' do
      context 'when there is no <class>' do
        let(:cobertura) { '' }

        it 'parses XML and returns empty coverage' do
          expect { subject }.not_to raise_error

          expect(coverage_report.files).to eq({})
        end
      end

      context 'when there is a single <class>' do
        context 'with no lines' do
          let(:cobertura) do
            <<-EOF.strip_heredoc
            <classes><class filename="app.rb"></class></classes>
            EOF
          end

          it 'parses XML and returns empty coverage' do
            expect { subject }.not_to raise_error

            expect(coverage_report.files).to eq({})
          end
        end

        context 'with a single line' do
          let(:cobertura) do
            <<-EOF.strip_heredoc
            <classes>
              <class filename="app.rb"><lines>
                <line number="1" hits="2"/>
              </lines></class>
            </classes>
            EOF
          end

          it 'parses XML and returns a single file with coverage' do
            expect { subject }.not_to raise_error

            expect(coverage_report.files).to eq({ 'app.rb' => { 1 => 2 } })
          end
        end

        context 'with multipe lines and methods info' do
          let(:cobertura) do
            <<-EOF.strip_heredoc
            <classes>
              <class filename="app.rb"><methods/><lines>
                <line number="1" hits="2"/>
                <line number="2" hits="0"/>
              </lines></class>
            </classes>
            EOF
          end

          it 'parses XML and returns a single file with coverage' do
            expect { subject }.not_to raise_error

            expect(coverage_report.files).to eq({ 'app.rb' => { 1 => 2, 2 => 0 } })
          end
        end
      end

      context 'when there are multipe <class>' do
        context 'with the same filename and different lines' do
          let(:cobertura) do
            <<-EOF.strip_heredoc
            <classes>
              <class filename="app.rb"><methods/><lines>
                <line number="1" hits="2"/>
                <line number="2" hits="0"/>
              </lines></class>
              <class filename="app.rb"><methods/><lines>
                <line number="6" hits="1"/>
                <line number="7" hits="1"/>
              </lines></class>
            </classes>
            EOF
          end

          it 'parses XML and returns a single file with merged coverage' do
            expect { subject }.not_to raise_error

            expect(coverage_report.files).to eq({ 'app.rb' => { 1 => 2, 2 => 0, 6 => 1, 7 => 1 } })
          end
        end

        context 'with the same filename and lines' do
          let(:cobertura) do
            <<-EOF.strip_heredoc
            <packages><package><classes>
              <class filename="app.rb"><methods/><lines>
                <line number="1" hits="2"/>
                <line number="2" hits="0"/>
              </lines></class>
              <class filename="app.rb"><methods/><lines>
                <line number="1" hits="1"/>
                <line number="2" hits="1"/>
              </lines></class>
            </classes></package></packages>
            EOF
          end

          it 'parses XML and returns a single file with summed-up coverage' do
            expect { subject }.not_to raise_error

            expect(coverage_report.files).to eq({ 'app.rb' => { 1 => 3, 2 => 1 } })
          end
        end

        context 'with missing filename' do
          let(:cobertura) do
            <<-EOF.strip_heredoc
            <classes>
              <class filename="app.rb"><methods/><lines>
                <line number="1" hits="2"/>
                <line number="2" hits="0"/>
              </lines></class>
              <class><methods/><lines>
                <line number="6" hits="1"/>
                <line number="7" hits="1"/>
              </lines></class>
            </classes>
            EOF
          end

          it 'parses XML and ignores class with missing name' do
            expect { subject }.not_to raise_error

            expect(coverage_report.files).to eq({ 'app.rb' => { 1 => 2, 2 => 0 } })
          end
        end

        context 'with invalid line information' do
          let(:cobertura) do
            <<-EOF.strip_heredoc
            <classes>
              <class filename="app.rb"><methods/><lines>
                <line number="1" hits="2"/>
                <line number="2" hits="0"/>
              </lines></class>
              <class filename="app.rb"><methods/><lines>
                <line null="test" hits="1"/>
                <line number="7" hits="1"/>
              </lines></class>
            </classes>
            EOF
          end

          it 'raises an error' do
            expect { subject }.to raise_error(described_class::CoberturaParserError)
          end
        end
      end
    end

    context 'when data is not Cobertura style XML' do
      let(:cobertura) { { coverage: '12%' }.to_json }

      it 'raises an error' do
        expect { subject }.to raise_error(described_class::CoberturaParserError)
      end
    end
  end
end