forked from rubocop/rubocop
/
team_spec.rb
401 lines (321 loc) · 10.8 KB
/
team_spec.rb
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Team do
subject(:team) { described_class.mobilize(cop_classes, config, options) }
let(:cop_classes) { RuboCop::Cop::Registry.global }
let(:config) { RuboCop::ConfigLoader.default_configuration }
let(:options) { {} }
let(:ruby_version) { RuboCop::TargetRuby.supported_versions.last }
before do
RuboCop::ConfigLoader.default_configuration = nil
end
context 'when incompatible cops are correcting together' do
include FileHelper
let(:options) { { formatters: [], auto_correct: true } }
let(:runner) { RuboCop::Runner.new(options, RuboCop::ConfigStore.new) }
let(:file_path) { 'example.rb' }
it 'auto corrects without SyntaxError', :isolated_environment do
source = <<~'RUBY'
foo.map{ |a| a.nil? }
'foo' +
'bar' +
"#{baz}"
i=i+1
def a
self::b
end
RUBY
corrected = <<~'RUBY'
# frozen_string_literal: true
foo.map(&:nil?)
'foo' \
'bar' \
"#{baz}"
i += 1
def a
b
end
RUBY
create_file(file_path, source)
runner.run([])
expect(File.read(file_path)).to eq(corrected)
end
end
describe '#autocorrect?' do
subject { team.autocorrect? }
context 'when the option argument of .new is omitted' do
subject { described_class.new(cop_classes, config).autocorrect? }
it { is_expected.to be_falsey }
end
context 'when { auto_correct: true } is passed to .new' do
let(:options) { { auto_correct: true } }
it { is_expected.to be_truthy }
end
end
describe '#debug?' do
subject { team.debug? }
context 'when the option argument of .new is omitted' do
subject { described_class.new(cop_classes, config).debug? }
it { is_expected.to be_falsey }
end
context 'when { debug: true } is passed to .new' do
let(:options) { { debug: true } }
it { is_expected.to be_truthy }
end
end
describe '#inspect_file', :isolated_environment do
include FileHelper
let(:file_path) { '/tmp/example.rb' }
let(:source) { RuboCop::ProcessedSource.from_file(file_path, ruby_version) }
let(:offenses) do
team.inspect_file(source)
end
before do
create_file(file_path, [
'#' * 90,
'puts test;'
])
end
it 'returns offenses' do
expect(offenses.empty?).to be(false)
expect(offenses).to all(be_a(RuboCop::Cop::Offense))
end
context 'when Parser reports non-fatal warning for the file' do
before do
create_file(file_path, ['#' * 130, 'puts *test'])
end
let(:cop_names) { offenses.map(&:cop_name) }
it 'returns Parser warning offenses' do
expect(cop_names).to include('Lint/AmbiguousOperator')
end
it 'returns offenses from cops' do
expect(cop_names).to include('Layout/LineLength')
end
context 'when a cop has no interest in the file' do
it 'returns all offenses except the ones of the cop' do
allow_any_instance_of(RuboCop::Cop::Layout::LineLength)
.to receive(:excluded_file?).and_return(true)
expect(cop_names).to include('Lint/AmbiguousOperator')
expect(cop_names).not_to include('Layout/LineLength')
end
end
end
context 'when autocorrection is enabled' do
let(:options) { { auto_correct: true } }
before do
create_file(file_path, 'puts "string"')
end
it 'does autocorrection' do
source = RuboCop::ProcessedSource.from_file(file_path, ruby_version)
team.inspect_file(source)
corrected_source = File.read(file_path)
expect(corrected_source).to eq(<<~RUBY)
# frozen_string_literal: true
puts 'string'
RUBY
end
it 'still returns offenses' do
expect(offenses[1].cop_name).to eq('Style/StringLiterals')
end
end
context 'when Cop#on_* raises an error' do
include_context 'mock console output'
before do
allow_any_instance_of(RuboCop::Cop::Style::NumericLiterals)
.to receive(:on_int).and_raise(StandardError)
create_file(file_path, '10_00_000')
end
let(:error_message) do
'An error occurred while Style/NumericLiterals cop was inspecting ' \
'/tmp/example.rb:1:0.'
end
it 'records Team#errors' do
source = RuboCop::ProcessedSource.from_file(file_path, ruby_version)
team.inspect_file(source)
expect(team.errors).to eq([error_message])
expect($stderr.string).to include(error_message)
end
end
context 'when a correction raises an error' do
include_context 'mock console output'
before do
allow_any_instance_of(RuboCop::Cop::Style::NumericLiterals)
.to receive(:autocorrect).and_return(buggy_correction)
create_file(file_path, '10_00_000')
end
let(:buggy_correction) do
lambda do |_corrector|
raise cause
end
end
let(:options) { { auto_correct: true } }
let(:cause) { StandardError.new('cause') }
let(:error_message) do
'An error occurred while Style/NumericLiterals cop was inspecting ' \
'/tmp/example.rb:1:0.'
end
it 'records Team#errors' do
source = RuboCop::ProcessedSource.from_file(file_path, ruby_version)
team.inspect_file(source)
expect($stderr.string).to include(error_message)
end
end
context 'when done twice', :restore_registry do
let(:persisting_cop_class) do
stub_cop_class('Test::Persisting') do
def self.support_multiple_source?
true
end
end
end
let(:cop_classes) { [persisting_cop_class, RuboCop::Cop::Base] }
it 'allows cops to get ready' do
before = team.cops.dup
team.inspect_file(source)
team.inspect_file(source)
expect(team.cops).to match_array([be(before.first), be_a(RuboCop::Cop::Base)])
expect(team.cops.last).not_to be(before.last)
end
end
end
describe '#cops' do
subject(:cops) { team.cops }
it 'returns cop instances' do
expect(cops.empty?).to be(false)
expect(cops.all? { |c| c.is_a?(RuboCop::Cop::Base) }).to be_truthy
end
context 'when only some cop classes are passed to .new' do
let(:cop_classes) do
RuboCop::Cop::Registry.new(
[RuboCop::Cop::Lint::Void, RuboCop::Cop::Layout::LineLength]
)
end
it 'returns only instances of the classes' do
expect(cops.size).to eq(2)
cops.sort! { |a, b| a.name <=> b.name }
expect(cops[0].name).to eq('Layout/LineLength')
expect(cops[1].name).to eq('Lint/Void')
end
end
context 'when some classes are disabled with config' do
let(:disabled_config) do
%w[
Lint/Void
Layout/LineLength
].each_with_object(RuboCop::Config.new) do |cop_name, accum|
accum[cop_name] = { 'Enabled' => false }
end
end
let(:config) do
RuboCop::ConfigLoader.merge_with_default(disabled_config, '')
end
let(:cop_names) { cops.map(&:name) }
it 'does not return instances of the classes' do
expect(cops.empty?).to be(false)
expect(cop_names).not_to include('Lint/Void')
expect(cop_names).not_to include('Layout/LineLength')
end
end
end
describe '#forces' do
subject(:forces) { team.forces }
let(:cop_classes) { RuboCop::Cop::Registry.global }
it 'returns force instances' do
expect(forces.empty?).to be(false)
forces.each do |force|
expect(force.is_a?(RuboCop::Cop::Force)).to be(true)
end
end
context 'when a cop joined a force' do
let(:cop_classes) do
RuboCop::Cop::Registry.new([RuboCop::Cop::Lint::UselessAssignment])
end
it 'returns the force' do
expect(forces.size).to eq(1)
expect(forces.first.is_a?(RuboCop::Cop::VariableForce)).to be(true)
end
end
context 'when multiple cops joined a same force' do
let(:cop_classes) do
RuboCop::Cop::Registry.new(
[
RuboCop::Cop::Lint::UselessAssignment,
RuboCop::Cop::Lint::ShadowingOuterLocalVariable
]
)
end
it 'returns only one force instance' do
expect(forces.size).to eq(1)
end
end
context 'when no cops joined force' do
let(:cop_classes) do
RuboCop::Cop::Registry.new([RuboCop::Cop::Style::For])
end
it 'returns nothing' do
expect(forces.empty?).to be(true)
end
end
end
describe '#external_dependency_checksum' do
let(:cop_classes) { RuboCop::Cop::Registry.new }
it 'does not error with no cops' do
expect(team.external_dependency_checksum.is_a?(String)).to be(true)
end
context 'when a cop joins' do
let(:cop_classes) do
RuboCop::Cop::Registry.new([RuboCop::Cop::Lint::UselessAssignment])
end
it 'returns string' do
expect(team.external_dependency_checksum.is_a?(String)).to be(true)
end
end
context 'when multiple cops join' do
let(:cop_classes) do
RuboCop::Cop::Registry.new(
[
RuboCop::Cop::Lint::UselessAssignment,
RuboCop::Cop::Lint::ShadowingOuterLocalVariable
]
)
end
it 'returns string' do
expect(team.external_dependency_checksum.is_a?(String)).to be(true)
end
end
context 'when cop with different checksum joins', :restore_registry do
before do
stub_cop_class('Test::CopWithExternalDeps') do
def external_dependency_checksum
'something other than nil'
end
end
end
let(:new_cop_classes) do
RuboCop::Cop::Registry.new(
[
Test::CopWithExternalDeps,
RuboCop::Cop::Lint::UselessAssignment,
RuboCop::Cop::Lint::ShadowingOuterLocalVariable
]
)
end
it 'has a different checksum for the whole team' do
original_checksum = team.external_dependency_checksum
new_team = described_class.mobilize(new_cop_classes, config, options)
new_checksum = new_team.external_dependency_checksum
expect(original_checksum).not_to eq(new_checksum)
end
end
end
describe '.new' do
it 'calls mobilize when passed classes' do
expect(described_class).to receive(:mobilize).with(cop_classes, config, options)
described_class.new(cop_classes, config, options)
end
it 'accepts cops directly classes' do
cop = RuboCop::Cop::Metrics::AbcSize.new
team = described_class.new([cop], config, options)
expect(team.cops.first).to equal(cop)
end
end
end