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

cop_todo_spec.rb « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 978df2c01ee95c25289d42cd61381d978d58ebae (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
# frozen_string_literal: true

require 'fast_spec_helper'
require_relative '../../rubocop/cop_todo'

RSpec.describe RuboCop::CopTodo do
  let(:cop_name) { 'Cop/Rule' }

  subject(:cop_todo) { described_class.new(cop_name) }

  describe '#initialize' do
    it 'initializes a cop todo' do
      expect(cop_todo).to have_attributes(
        cop_name: cop_name,
        files: be_empty,
        offense_count: 0,
        previously_disabled: false
      )
    end
  end

  describe '#record' do
    it 'records offenses' do
      cop_todo.record('a.rb', 1)
      cop_todo.record('b.rb', 2)

      expect(cop_todo).to have_attributes(
        files: contain_exactly('a.rb', 'b.rb'),
        offense_count: 3
      )
    end
  end

  describe '#autocorrectable?' do
    subject { cop_todo.autocorrectable? }

    context 'when found in rubocop registry' do
      before do
        fake_cop = double(:cop, support_autocorrect?: autocorrectable) # rubocop:disable RSpec/VerifiedDoubles

        allow(described_class).to receive(:find_cop_by_name)
          .with(cop_name).and_return(fake_cop)
      end

      context 'when autocorrectable' do
        let(:autocorrectable) { true }

        it { is_expected.to be_truthy }
      end

      context 'when not autocorrectable' do
        let(:autocorrectable) { false }

        it { is_expected.to be_falsey }
      end
    end

    context 'when not found in rubocop registry' do
      before do
        allow(described_class).to receive(:find_cop_by_name)
          .with(cop_name).and_return(nil).and_call_original
      end

      it { is_expected.to be_falsey }
    end
  end

  describe '#to_yaml' do
    subject(:yaml) { cop_todo.to_yaml }

    context 'when autocorrectable' do
      before do
        allow(cop_todo).to receive(:autocorrectable?).and_return(true)
      end

      specify do
        expect(yaml).to eq(<<~YAML)
          ---
          # Cop supports --auto-correct.
          #{cop_name}:
            Exclude:
        YAML
      end
    end

    context 'when previously disabled' do
      specify do
        cop_todo.record('a.rb', 1)
        cop_todo.record('b.rb', 2)
        cop_todo.previously_disabled = true

        expect(yaml).to eq(<<~YAML)
          ---
          #{cop_name}:
            # Offense count: 3
            # Temporarily disabled due to too many offenses
            Enabled: false
            Exclude:
              - 'a.rb'
              - 'b.rb'
        YAML
      end
    end

    context 'with multiple files' do
      before do
        cop_todo.record('a.rb', 0)
        cop_todo.record('c.rb', 0)
        cop_todo.record('b.rb', 0)
      end

      it 'sorts excludes alphabetically' do
        expect(yaml).to eq(<<~YAML)
        ---
        #{cop_name}:
          Exclude:
            - 'a.rb'
            - 'b.rb'
            - 'c.rb'
        YAML
      end
    end
  end
end