/
redundant_cop_disable_directive_spec.rb
507 lines (441 loc) · 16.8 KB
/
redundant_cop_disable_directive_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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# frozen_string_literal: true
require 'rubocop/cop/legacy/corrector'
RSpec.describe RuboCop::Cop::Lint::RedundantCopDisableDirective, :config do
describe '.check' do
let(:offenses) { [] }
let(:cop) { cop_class.new(config, cop_options, offenses) }
before { $stderr = StringIO.new } # rubocop:disable RSpec/ExpectOutput
context 'when there are no disabled lines' do
let(:source) { '' }
it 'returns no offense' do
expect_no_offenses(source)
end
end
context 'when there are disabled lines' do
context 'and there are no offenses' do
context 'and a comment disables' do
context 'one cop' do
it 'returns an offense' do
expect_offense(<<~RUBY)
# rubocop:disable Metrics/MethodLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/MethodLength`.
RUBY
expect_correction('')
end
end
context 'an unknown cop' do
it 'returns an offense' do
expect_offense(<<~RUBY)
# rubocop:disable UnknownCop
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `UnknownCop` (unknown cop).
RUBY
expect_correction('')
end
end
context 'when using a directive comment after a non-directive comment' do
it 'returns an offense' do
expect_offense(<<~RUBY)
# not very long comment # rubocop:disable Layout/LineLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Layout/LineLength`.
RUBY
expect_correction(<<~RUBY)
# not very long comment
RUBY
end
end
context 'itself and another cop' do
context 'disabled on the same range' do
it 'returns no offense' do
expect_no_offenses(<<~RUBY)
# rubocop:disable Lint/RedundantCopDisableDirective, Metrics/ClassLength
RUBY
end
end
context 'disabled on different ranges' do
it 'returns no offense' do
expect_no_offenses(<<~RUBY)
# rubocop:disable Lint/RedundantCopDisableDirective
# rubocop:disable Metrics/ClassLength
RUBY
end
end
context 'and the other cop is disabled a second time' do
let(:source) do
['# rubocop:disable Lint/RedundantCopDisableDirective',
'# rubocop:disable Metrics/ClassLength',
'# rubocop:disable Metrics/ClassLength'].join("\n")
end
it 'returns no offense' do
expect_no_offenses(source)
end
end
end
context 'multiple cops' do
it 'returns an offense' do
expect_offense(<<~RUBY.gsub("<\n", '')) # Wrap lines & avoid issue with JRuby
# rubocop:disable Metrics/MethodLength, Metrics/ClassLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <
Unnecessary disabling of `Metrics/ClassLength`, `Metrics/MethodLength`.
RUBY
end
end
context 'multiple cops, and one of them has offenses' do
let(:offenses) do
[
RuboCop::Cop::Offense.new(:convention,
OpenStruct.new(line: 7, column: 0),
'Class has too many lines.',
'Metrics/ClassLength')
]
end
it 'returns an offense' do
expect_offense(<<~RUBY.gsub("<\n", '')) # Wrap lines & avoid issue with JRuby
# rubocop:disable Metrics/MethodLength, Metrics/ClassLength, Lint/Debugger, <
Lint/AmbiguousOperator
^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/MethodLength`.
^^^^^^^^^^^^^ Unnecessary disabling of `Lint/Debugger`.
<
^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Lint/AmbiguousOperator`.
RUBY
expect_correction(<<~RUBY)
# rubocop:disable Metrics/ClassLength
RUBY
end
end
context 'multiple cops, and the leftmost one has no offenses' do
let(:offenses) do
[
RuboCop::Cop::Offense.new(:convention,
OpenStruct.new(line: 7, column: 0),
'Method has too many lines.',
'Metrics/MethodLength')
]
end
it 'returns an offense' do
expect_offense(<<~RUBY)
# rubocop:disable Metrics/ClassLength, Metrics/MethodLength
^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/ClassLength`.
RUBY
expect_correction(<<~RUBY)
# rubocop:disable Metrics/MethodLength
RUBY
end
end
context 'multiple cops, with abbreviated names' do
context 'one of them has offenses' do
let(:offenses) do
[
RuboCop::Cop::Offense.new(:convention,
OpenStruct.new(line: 4, column: 0),
'Method has too many lines.',
'Metrics/MethodLength')
]
end
it 'returns an offense' do
expect_offense(<<~RUBY)
puts 1
# rubocop:disable MethodLength, ClassLength, Debugger
^^^^^^^^^^^ Unnecessary disabling of `Metrics/ClassLength`.
^^^^^^^^ Unnecessary disabling of `Lint/Debugger`.
#
# offense here
RUBY
expect($stderr.string).to eq(<<~OUTPUT)
(string): Warning: no department given for MethodLength.
(string): Warning: no department given for ClassLength.
(string): Warning: no department given for Debugger.
OUTPUT
end
end
end
context 'comment is not at the beginning of the file' do
context 'and not all cops have offenses' do
let(:offenses) do
[
RuboCop::Cop::Offense.new(:convention,
OpenStruct.new(line: 4, column: 0),
'Method has too many lines.',
'Metrics/MethodLength')
]
end
it 'returns an offense' do
expect_offense(<<~RUBY)
puts 1
# rubocop:disable Metrics/MethodLength, Metrics/ClassLength
^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/ClassLength`.
#
# offense here
RUBY
end
end
end
context 'misspelled cops' do
it 'returns an offense' do
message = 'Unnecessary disabling of `KlassLength` (unknown ' \
'cop), `Metrics/MethodLenght` (did you mean ' \
'`Metrics/MethodLength`?).'
expect_offense(<<~RUBY)
# rubocop:disable Metrics/MethodLenght, KlassLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY
end
end
context 'all cops' do
it 'returns an offense' do
expect_offense(<<~RUBY)
# rubocop : disable all
^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of all cops.
RUBY
end
end
context 'itself and all cops' do
context 'disabled on different ranges' do
let(:source) do
['# rubocop:disable Lint/RedundantCopDisableDirective',
'# rubocop:disable all'].join("\n")
end
it 'returns no offense' do
expect_no_offenses(source)
end
end
end
end
end
context 'and there are two offenses' do
let(:message) { 'Replace class var @@class_var with a class instance var.' }
let(:cop_name) { 'Style/ClassVars' }
let(:offenses) do
offense_lines.map do |line|
RuboCop::Cop::Offense.new(:convention,
OpenStruct.new(line: line, column: 3),
message,
cop_name)
end
end
context 'and a comment disables' do
context 'one cop twice' do
let(:offense_lines) { [3, 8] }
it 'returns an offense' do
expect_offense(<<~RUBY)
class One
# rubocop:disable Style/ClassVars
@@class_var = 1 # offense here
end
class Two
# rubocop:disable Style/ClassVars
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Style/ClassVars`.
@@class_var = 2 # offense and here
# rubocop:enable Style/ClassVars
end
RUBY
end
end
context 'one cop and then all cops' do
let(:offense_lines) { [4] }
it 'returns an offense' do
expect_offense(<<~RUBY)
class One
# rubocop:disable Style/ClassVars
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Style/ClassVars`.
# rubocop:disable all
@@class_var = 1
# offense here
end
RUBY
end
end
end
end
context 'and there is an offense' do
let(:offenses) do
[
RuboCop::Cop::Offense.new(:convention,
OpenStruct.new(line: 3, column: 0),
'Tab detected.',
'Layout/IndentationStyle')
]
end
context 'and a comment disables' do
context 'that cop' do
let(:source) { '# rubocop:disable Layout/IndentationStyle' }
it 'returns no offense' do
expect_no_offenses(source)
end
end
context 'that cop but on other lines' do
it 'returns an offense' do
expect_offense(<<~RUBY)
# 1
# 2
# 3, offense here
# 4
# rubocop:disable Layout/IndentationStyle
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Layout/IndentationStyle`.
#
# rubocop:enable Layout/IndentationStyle
RUBY
end
end
context 'all cops' do
let(:source) { '# rubocop : disable all' }
it 'returns no offense' do
expect_no_offenses(source)
end
end
end
end
end
context 'autocorrecting whitespace' do
context 'when the comment is the first line of the file' do
context 'followed by code' do
it 'removes the comment' do
expect_offense(<<~RUBY)
# rubocop:disable Metrics/MethodLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/MethodLength`.
def my_method
end
# rubocop:enable Metrics/MethodLength
RUBY
expect_correction(<<~RUBY)
def my_method
end
# rubocop:enable Metrics/MethodLength
RUBY
end
end
context 'followed by a newline' do
it 'removes the comment and newline' do
expect_offense(<<~RUBY)
# rubocop:disable Metrics/MethodLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/MethodLength`.
def my_method
end
# rubocop:enable Metrics/MethodLength
RUBY
expect_correction(<<~RUBY)
def my_method
end
# rubocop:enable Metrics/MethodLength
RUBY
end
end
context 'followed by another comment' do
it 'removes the comment and newline' do
expect_offense(<<~RUBY)
# rubocop:disable Metrics/MethodLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/MethodLength`.
# @api private
def my_method
end
# rubocop:enable Metrics/MethodLength
RUBY
expect_correction(<<~RUBY)
# @api private
def my_method
end
# rubocop:enable Metrics/MethodLength
RUBY
end
end
end
context 'when there is only whitespace before the comment' do
it 'leaves the whitespace' do
expect_offense(<<~RUBY)
# rubocop:disable Metrics/MethodLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/MethodLength`.
def my_method
end
# rubocop:enable Metrics/MethodLength
RUBY
expect_correction(<<~RUBY)
def my_method
end
# rubocop:enable Metrics/MethodLength
RUBY
end
end
context 'when the comment is not the first line of the file' do
it 'preserves whitespace before the comment' do
expect_offense(<<~RUBY)
attr_reader :foo
# rubocop:disable Metrics/MethodLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/MethodLength`.
def my_method
end
# rubocop:enable Metrics/MethodLength
RUBY
expect_correction(<<~RUBY)
attr_reader :foo
def my_method
end
# rubocop:enable Metrics/MethodLength
RUBY
end
end
context 'nested inside a namespace' do
it 'preserves indentation' do
expect_offense(<<~RUBY)
module Foo
module Bar
# rubocop:disable Metrics/ClassLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/ClassLength`.
class Baz
end
# rubocop:enable Metrics/ClassLength
end
end
RUBY
expect_correction(<<~RUBY)
module Foo
module Bar
class Baz
end
# rubocop:enable Metrics/ClassLength
end
end
RUBY
end
end
context 'inline comment' do
it 'removes the comment and preceding whitespace' do
expect_offense(<<~RUBY)
module Foo
module Bar
class Baz # rubocop:disable Metrics/ClassLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/ClassLength`.
end
end
end
RUBY
expect_correction(<<~RUBY)
module Foo
module Bar
class Baz
end
end
end
RUBY
end
end
context 'when there is a blank line before inline comment' do
it 'removes the comment and preceding whitespace' do
expect_offense(<<~RUBY)
def foo; end
def bar # rubocop:disable Metrics/ClassLength
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnecessary disabling of `Metrics/ClassLength`.
do_something do
end
end
RUBY
expect_correction(<<~RUBY)
def foo; end
def bar
do_something do
end
end
RUBY
end
end
end
end
end