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

todo_dir_spec.rb « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e014c4c2c37410f25d29d7a856dc5eced7f1265d (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# frozen_string_literal: true

require 'fast_spec_helper'

require 'active_support/inflector/inflections'
require 'fileutils'
require 'tmpdir'

require_relative '../../rubocop/todo_dir'

RSpec.describe RuboCop::TodoDir do
  let(:todo_dir) { described_class.new(directory) }
  let(:directory) { Dir.mktmpdir }
  let(:cop_name) { 'RSpec/VariableInstance' }
  let(:cop_name_underscore) { ActiveSupport::Inflector.underscore(cop_name) }
  let(:yaml_path) { "#{File.join(directory, cop_name_underscore)}.yml" }

  around do |example|
    Dir.chdir(directory) do
      example.run
    end
  end

  after do
    FileUtils.remove_entry(directory)
  end

  describe '#initialize' do
    context 'when passing inflector' do
      let(:fake_inflector) { double(:inflector) } # rubocop:disable RSpec/VerifiedDoubles
      let(:todo_dir) { described_class.new(directory, inflector: fake_inflector) }

      before do
        allow(fake_inflector).to receive(:underscore)
          .with(cop_name)
          .and_return(cop_name_underscore)
      end

      it 'calls .underscore' do
        todo_dir.write(cop_name, 'a')

        expect(fake_inflector).to have_received(:underscore)
      end
    end
  end

  describe '#read' do
    let(:content) { 'a' }

    subject { todo_dir.read(cop_name) }

    context 'when file exists' do
      before do
        todo_dir.write(cop_name, content)
      end

      it { is_expected.to eq(content) }
    end

    context 'when file is missing' do
      it { is_expected.to be_nil }
    end
  end

  describe '#write' do
    let(:content) { 'a' }

    subject { todo_dir.write(cop_name, content) }

    it { is_expected.to eq(yaml_path) }

    it 'writes content to YAML file' do
      subject

      expect(File.read(yaml_path)).to eq(content)
    end
  end

  describe '#inspect' do
    subject { todo_dir.inspect(cop_name) }

    context 'with existing YAML file' do
      before do
        todo_dir.write(cop_name, 'a')
      end

      it { is_expected.to eq(true) }

      it 'moves YAML file to .inspect' do
        subject

        expect(File).not_to exist(yaml_path)
        expect(File).to exist("#{yaml_path}.inspect")
      end
    end

    context 'with missing YAML file' do
      it { is_expected.to eq(false) }
    end
  end

  describe '#inspect_all' do
    subject { todo_dir.inspect_all }

    context 'with YAML files' do
      before do
        todo_dir.write(cop_name, 'a')
        todo_dir.write('Other/Rule', 'a')
        todo_dir.write('Very/Nested/Rule', 'a')
      end

      it { is_expected.to eq(3) }

      it 'moves all YAML files to .inspect' do
        subject

        expect(Dir.glob('**/*.yml')).to be_empty
        expect(Dir.glob('**/*.yml.inspect').size).to eq(3)
      end
    end

    context 'with non-YAML files' do
      before do
        File.write('file', 'a')
        File.write('file.txt', 'a')
        File.write('file.yaml', 'a') # not .yml
      end

      it { is_expected.to eq(0) }

      it 'does not move non-YAML files' do
        subject

        expect(Dir.glob('**/*'))
          .to contain_exactly('file', 'file.txt', 'file.yaml')
      end
    end

    context 'without files' do
      it { is_expected.to eq(0) }
    end
  end

  describe '#list_inspect' do
    let(:content) { 'a' }

    subject { todo_dir.list_inspect }

    context 'when file exists and is being inspected' do
      before do
        todo_dir.write(cop_name, content)
        todo_dir.inspect_all
      end

      it do
        is_expected.to contain_exactly("#{yaml_path}.inspect")
      end
    end

    context 'when file exists but not being inspected' do
      before do
        todo_dir.write(cop_name, content)
      end

      it { is_expected.to be_empty }
    end

    context 'when file is missing' do
      it { is_expected.to be_empty }
    end
  end

  describe '#delete_inspected' do
    subject { todo_dir.delete_inspected }

    context 'with YAML files' do
      before do
        todo_dir.write(cop_name, 'a')
        todo_dir.write('Other/Rule', 'a')
        todo_dir.write('Very/Nested/Rule', 'a')
        todo_dir.inspect_all
      end

      it { is_expected.to eq(3) }

      it 'deletes all .inspected YAML files' do
        subject

        expect(Dir.glob('**/*.yml.inspect')).to be_empty
      end
    end

    context 'with non-YAML files' do
      before do
        File.write('file.inspect', 'a')
        File.write('file.txt.inspect', 'a')
        File.write('file.yaml.inspect', 'a') # not .yml
      end

      it { is_expected.to eq(0) }

      it 'does not delete non-YAML files' do
        subject

        expect(Dir.glob('**/*')).to contain_exactly(
          'file.inspect', 'file.txt.inspect', 'file.yaml.inspect')
      end
    end

    context 'without files' do
      it { is_expected.to eq(0) }
    end
  end
end