summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/reports/coverage_reports_spec.rb
blob: 7cf43ceab324f7c616bc1ab60dd47253083b81d9 (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
# frozen_string_literal: true

require 'spec_helper'

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

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

  describe '#pick' do
    before do
      coverage_report.add_file('app.rb', { 1 => 0, 2 => 1 })
      coverage_report.add_file('routes.rb', { 3 => 1, 4 => 0 })
    end

    it 'returns only picked files while ignoring nonexistent ones' do
      expect(coverage_report.pick(['routes.rb', 'nonexistent.txt'])).to eq({
        files: { 'routes.rb' => { 3 => 1, 4 => 0 } }
      })
    end
  end

  describe '#add_file' do
    context 'when providing two individual files' do
      before do
        coverage_report.add_file('app.rb', { 1 => 0, 2 => 1 })
        coverage_report.add_file('routes.rb', { 3 => 1, 4 => 0 })
      end

      it 'initializes a new test suite and returns it' do
        expect(coverage_report.files).to eq({
          'app.rb' => { 1 => 0, 2 => 1 },
          'routes.rb' => { 3 => 1, 4 => 0 }
        })
      end
    end

    context 'when providing the same files twice' do
      context 'with different line coverage' do
        before do
          coverage_report.add_file('admin.rb', { 1 => 0, 2 => 1 })
          coverage_report.add_file('admin.rb', { 3 => 1, 4 => 0 })
        end

        it 'initializes a new test suite and returns it' do
          expect(coverage_report.files).to eq({
            'admin.rb' => { 1 => 0, 2 => 1, 3 => 1, 4 => 0 }
          })
        end
      end

      context 'with identical line coverage' do
        before do
          coverage_report.add_file('projects.rb', { 1 => 0, 2 => 1 })
          coverage_report.add_file('projects.rb', { 1 => 0, 2 => 1 })
        end

        it 'initializes a new test suite and returns it' do
          expect(coverage_report.files).to eq({
            'projects.rb' => { 1 => 0, 2 => 2 }
          })
        end
      end
    end
  end
end