-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
cli_options_spec.rb
2095 lines (1817 loc) · 71.8 KB
/
cli_options_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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
RSpec.describe RuboCop::CLI, :isolated_environment do
subject(:cli) { described_class.new }
include_context 'cli spec behavior'
let(:rubocop) { "#{RuboCop::ConfigLoader::RUBOCOP_HOME}/exe/rubocop" }
before do
RuboCop::ConfigLoader.default_configuration = nil
end
describe '--parallel' do
if RuboCop::Platform.windows?
context 'on Windows' do
it 'prints a warning' do
cli.run ['-P']
expect($stderr.string)
.to include('Process.fork is not supported by this Ruby')
end
end
else
context 'combined with AllCops:UseCache:false' do
before do
create_file('.rubocop.yml', ['AllCops:',
' UseCache: false'])
end
it 'fails with an error message' do
cli.run %w[-P]
expect($stderr.string)
.to include('-P/--parallel uses caching to speed up execution, ' \
'so combining with AllCops: UseCache: false is not ' \
'allowed.')
end
end
context 'on Unix-like systems' do
it 'prints a message if --debug is specified' do
cli.run ['--parallel', '--debug']
expect($stdout.string).to match(/Running parallel inspection/)
end
it 'does not print a message if --debug is not specified' do
cli.run ['--parallel']
expect($stdout.string).not_to match(/Running parallel inspection/)
end
end
context 'in combination with --ignore-parent-exclusion' do
before do
create_file('.rubocop.yml', ['AllCops:',
' Exclude:',
' - subdir/*'])
create_file('subdir/.rubocop.yml', ['AllCops:',
' Exclude:',
' - foobar'])
create_file('subdir/test.rb', 'puts 1')
end
it 'does ignore the exclusion in the parent directory configuration' do
Dir.chdir('subdir') do
cli.run ['--parallel', '--ignore-parent-exclusion']
end
expect($stdout.string).to match(/Inspecting 1 file/)
end
end
context 'in combination with --force-default-config' do
before do
create_file('.rubocop.yml', ['ALLCOPS:', # Faulty configuration
' Exclude:',
' - subdir/*'])
create_file('test.rb', 'puts 1')
end
it 'does not parse local configuration' do
cli.run ['--parallel', '--force-default-config']
expect($stdout.string).to match(/Inspecting 1 file/)
end
end
end
end
describe '--list-target-files' do
context 'when there are no files' do
it 'prints nothing with -L' do
cli.run ['-L']
expect($stdout.string.empty?).to be(true)
end
it 'prints nothing with --list-target-files' do
cli.run ['--list-target-files']
expect($stdout.string.empty?).to be(true)
end
end
context 'when there are some files' do
before do
create_file('show.rabl2', 'object @user => :person')
create_file('show.rabl', 'object @user => :person')
create_file('app.rb', 'puts "hello world"')
create_file('Gemfile', <<~RUBY)
source "https://rubygems.org"
gem "rubocop"
RUBY
create_file('lib/helper.rb', 'puts "helpful"')
end
context 'when there are no includes or excludes' do
it 'prints known ruby files' do
cli.run ['-L']
expect($stdout.string.split("\n")).to contain_exactly(
'app.rb', 'Gemfile', 'lib/helper.rb', 'show.rabl'
)
end
end
context 'when there is an include and exclude' do
before do
create_file('.rubocop.yml', <<~YAML)
AllCops:
Exclude:
- Gemfile
Include:
- "**/*.rb"
- "**/*.rabl"
- "**/*.rabl2"
YAML
end
it 'prints the included files and not the excluded ones' do
cli.run ['--list-target-files']
expect($stdout.string.split("\n")).to contain_exactly(
'app.rb', 'lib/helper.rb', 'show.rabl', 'show.rabl2'
)
end
end
end
end
describe '--version' do
it 'exits cleanly' do
expect(cli.run(['-v'])).to eq(0)
expect(cli.run(['--version'])).to eq(0)
expect($stdout.string).to eq("#{RuboCop::Version::STRING}\n" * 2)
end
end
describe '-V' do
it 'exits cleanly' do
expect(cli.run(['-V'])).to eq(0)
expect($stdout.string).to include(RuboCop::Version::STRING)
expect($stdout.string).to match(/Parser \d+\.\d+\.\d+/)
expect($stdout.string).to match(/rubocop-ast \d+\.\d+\.\d+/)
end
context 'when requiring extension cops' do
before do
create_file('.rubocop.yml', <<~YAML)
require:
- rubocop-performance
- rubocop-rspec
YAML
end
it 'shows with version of extension cops' do
# Run in different process that requiring rubocop-performance and rubocop-rspec
# does not affect other testing processes.
output = `ruby -I . "#{rubocop}" -V --disable-pending-cops`
expect(output).to include(RuboCop::Version::STRING)
expect(output).to match(/Parser \d+\.\d+\.\d+/)
expect(output).to match(/rubocop-ast \d+\.\d+\.\d+/)
expect(output).to match(/rubocop-performance \d+\.\d+\.\d+/)
expect(output).to match(/rubocop-rspec \d+\.\d+\.\d+/)
end
end
context 'when requiring extension cops in multiple layers' do
before do
create_file('.rubocop-parent.yml', <<~YAML)
require:
- rubocop-performance
YAML
create_file('.rubocop.yml', <<~YAML)
inherit_from: ./.rubocop-parent.yml
require:
- rubocop-rspec
YAML
end
it 'shows with version of extension cops' do
# Run in different process that requiring rubocop-performance and rubocop-rspec
# does not affect other testing processes.
output = `ruby -I . "#{rubocop}" -V --disable-pending-cops`
expect(output).to include(RuboCop::Version::STRING)
expect(output).to match(/Parser \d+\.\d+\.\d+/)
expect(output).to match(/rubocop-ast \d+\.\d+\.\d+/)
expect(output).to match(/rubocop-performance \d+\.\d+\.\d+/)
expect(output).to match(/rubocop-rspec \d+\.\d+\.\d+/)
end
end
context 'when requiring redundant extension cop' do
before do
create_file('ext.yml', <<~YAML)
require:
- rubocop-rspec
YAML
create_file('.rubocop.yml', <<~YAML)
inherit_from: ext.yml
require:
- rubocop-performance
- rubocop-rspec
YAML
end
it 'shows with version of each extension cop once' do
output = `ruby -I . "#{rubocop}" -V --disable-pending-cops`
expect(output).to include(RuboCop::Version::STRING)
expect(output).to match(/Parser \d+\.\d+\.\d+/)
expect(output).to match(/rubocop-ast \d+\.\d+\.\d+/)
expect(output).to match(
/- rubocop-performance \d+\.\d+\.\d+\n - rubocop-rspec \d+\.\d+\.\d+\n\z/
)
end
end
context 'when there are pending cops' do
let(:pending_cop_warning) { <<~PENDING_COP_WARNING }
The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.
PENDING_COP_WARNING
before do
create_file('.rubocop.yml', <<~YAML)
require: rubocop_ext
AllCops:
NewCops: pending
Style/SomeCop:
Description: Something
Enabled: pending
VersionAdded: '1.6'
YAML
create_file('rubocop_ext.rb', <<~RUBY)
module RuboCop
module Cop
module Style
class SomeCop < Cop
end
end
end
end
RUBY
create_file('redirect.rb', '$stderr = STDOUT')
end
it 'does not show warnings for pending cops' do
output = `ruby -I . "#{rubocop}" --require redirect.rb -V`
expect(output).to include(RuboCop::Version::STRING)
expect(output).to match(/Parser \d+\.\d+\.\d+/)
expect(output).to match(/rubocop-ast \d+\.\d+\.\d+/)
expect(output).not_to include(pending_cop_warning)
end
end
end
describe '--only' do
context 'when one cop is given' do
it 'runs just one cop' do
# The disable comment should not be reported as unnecessary (even if
# it is) since --only overrides configuration.
create_file('example.rb', ['# rubocop:disable LineLength',
'if x== 0 ',
"\ty",
'end'])
# IfUnlessModifier depends on the configuration of LineLength.
expect(cli.run(['--format', 'simple',
'--only', 'Style/IfUnlessModifier',
'example.rb'])).to eq(1)
expect($stdout.string)
.to eq(['== example.rb ==',
'C: 2: 1: [Correctable] Style/IfUnlessModifier: Favor modifier if ' \
'usage when having a single-line body. Another good ' \
'alternative is the usage of control flow &&/||.',
'',
'1 file inspected, 1 offense detected, 1 offense auto-correctable',
''].join("\n"))
end
it 'exits with error if an incorrect cop name is passed' do
create_file('example.rb', ['if x== 0 ',
"\ty",
'end'])
expect(cli.run(['--only', 'Style/123'])).to eq(2)
expect($stderr.string)
.to include('Unrecognized cop or department: Style/123.')
end
it 'displays correction candidate if an incorrect cop name is given' do
create_file('example.rb', ['x'])
expect(cli.run(['--only', 'Style/BlockComment'])).to eq(2)
expect($stderr.string)
.to include('Unrecognized cop or department: Style/BlockComment.')
expect($stderr.string)
.to include('Did you mean? Style/BlockComments')
end
it 'exits with error if an empty string is given' do
create_file('example.rb', 'x')
expect(cli.run(['--only', ''])).to eq(2)
expect($stderr.string).to include('Unrecognized cop or department: .')
end
%w[Syntax Lint/Syntax].each do |name|
it "only checks syntax if #{name} is given" do
create_file('example.rb', 'x ')
expect(cli.run(['--only', name])).to eq(0)
expect($stdout.string).to include('no offenses detected')
end
end
%w[Lint/RedundantCopDisableDirective
RedundantCopDisableDirective].each do |name|
it "exits with error if cop name #{name} is passed" do
create_file('example.rb', ['if x== 0 ',
"\ty",
'end'])
expect(cli.run(['--only', 'RedundantCopDisableDirective'])).to eq(2)
expect($stderr.string)
.to include(
'Lint/RedundantCopDisableDirective cannot be used with --only.'
)
end
end
it 'accepts cop names from plugins' do
create_file('.rubocop.yml', <<~YAML)
require: rubocop_ext
Style/SomeCop:
Description: Something
Enabled: true
YAML
create_file('rubocop_ext.rb', <<~RUBY)
module RuboCop
module Cop
module Style
class SomeCop < Cop
end
end
end
end
RUBY
create_file('redirect.rb', '$stderr = STDOUT')
rubocop = "#{RuboCop::ConfigLoader::RUBOCOP_HOME}/exe/rubocop"
# Since we define a new cop class, we have to do this in a separate
# process. Otherwise, the extra cop will affect other specs.
output =
`ruby -I . "#{rubocop}" --require redirect.rb --only Style/SomeCop`
# Excludes a warning when new `Enabled: pending` status cop is specified
# in config/default.yml.
output_excluding_warn_for_pending_cops =
output.split("\n").last(4).join("\n") << "\n"
expect(output_excluding_warn_for_pending_cops)
.to eq(<<~RESULT)
Inspecting 2 files
..
2 files inspected, no offenses detected
RESULT
end
context 'when specifying a pending cop' do
# Since we define a new cop class, we have to do this in a separate
# process. Otherwise, the extra cop will affect other specs.
let(:output) do
`ruby -I . "#{rubocop}" --require redirect.rb --only Style/SomeCop`
end
let(:pending_cop_warning) { <<~PENDING_COP_WARNING }
The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.
PENDING_COP_WARNING
let(:inspected_output) { <<~INSPECTED_OUTPUT }
Inspecting 2 files
..
2 files inspected, no offenses detected
INSPECTED_OUTPUT
let(:versioning_manual_url) { <<~VERSIONING_MANUAL_URL.chop }
For more information: https://docs.rubocop.org/rubocop/versioning.html
VERSIONING_MANUAL_URL
before do
create_file('rubocop_ext.rb', <<~RUBY)
module RuboCop
module Cop
module Style
class SomeCop < Cop
end
end
end
end
RUBY
create_file('redirect.rb', '$stderr = STDOUT')
end
context 'when Style department is enabled' do
let(:new_cops_option) { '' }
let(:version_added) { "VersionAdded: '0.80'" }
before do
create_file('.rubocop.yml', <<~YAML)
require: rubocop_ext
AllCops:
DefaultFormatter: progress
#{new_cops_option}
Style/SomeCop:
Description: Something
Enabled: pending
#{version_added}
YAML
end
context 'when `VersionAdded` is specified' do
it 'accepts cop names from plugins with a pending cop warning' do
expect(output).to start_with(pending_cop_warning)
expect(output).to end_with(inspected_output)
remaining_range =
pending_cop_warning.length..-(inspected_output.length + 1)
pending_cops = output[remaining_range]
expect(pending_cops).to include("Style/SomeCop: # (new in 0.80)\n Enabled: true")
manual_url = output[remaining_range].split("\n").last
expect(manual_url).to eq(versioning_manual_url)
end
end
context 'when `VersionAdded` is not specified' do
let(:version_added) { '' }
it 'accepts cop names from plugins with a pending cop warning' do
expect(output).to start_with(pending_cop_warning)
expect(output).to end_with(inspected_output)
remaining_range =
pending_cop_warning.length..-(inspected_output.length + 1)
pending_cops = output[remaining_range]
expect(pending_cops).to include("Style/SomeCop: # (new in N/A)\n Enabled: true")
manual_url = output[remaining_range].split("\n").last
expect(manual_url).to eq(versioning_manual_url)
end
end
context 'when using `--disable-pending-cops` command-line option' do
let(:option) { '--disable-pending-cops' }
let(:output) do
`ruby -I . "#{rubocop}" --require redirect.rb #{option}`
end
it 'does not display a pending cop warning' do
expect(output).not_to start_with(pending_cop_warning)
end
end
context 'when using `--enable-pending-cops` command-line option' do
let(:option) { '--enable-pending-cops' }
let(:output) do
`ruby -I . "#{rubocop}" --require redirect.rb #{option}`
end
it 'does not display a pending cop warning' do
expect(output).not_to start_with(pending_cop_warning)
end
end
context 'when specifying `NewCops: pending` in .rubocop.yml' do
let(:new_cops_option) { 'NewCops: pending' }
let(:output) do
`ruby -I . "#{rubocop}" --require redirect.rb`
end
it 'displays a pending cop warning' do
expect(output).to start_with(pending_cop_warning)
end
end
context 'when specifying `NewCops: disable` in .rubocop.yml' do
let(:new_cops_option) { 'NewCops: disable' }
let(:output) do
`ruby -I . "#{rubocop}" --require redirect.rb`
end
it 'does not display a pending cop warning' do
expect(output).not_to start_with(pending_cop_warning)
end
end
context 'when specifying `NewCops: enable` in .rubocop.yml' do
let(:new_cops_option) { 'NewCops: enable' }
let(:output) do
`ruby -I . "#{rubocop}" --require redirect.rb`
end
it 'does not display a pending cop warning' do
expect(output).not_to start_with(pending_cop_warning)
end
end
end
context 'when Style department is disabled' do
before do
create_file('.rubocop.yml', <<~YAML)
require: rubocop_ext
Lint:
Enabled: false
Style:
Enabled: false
Layout:
Enabled: false
Gemspec:
Enabled: false
Style/SomeCop:
Description: Something
Enabled: pending
YAML
end
it 'does not show pending cop warning' do
expect(output).to eq(inspected_output)
end
end
end
context 'without using namespace' do
it 'runs just one cop' do
create_file('example.rb', ['if x== 0 ',
"\ty",
'end'])
expect(cli.run(['--format', 'simple',
'--display-cop-names',
'--only', 'IfUnlessModifier',
'example.rb'])).to eq(1)
expect($stdout.string)
.to eq(['== example.rb ==',
'C: 1: 1: [Correctable] Style/IfUnlessModifier: Favor modifier if ' \
'usage when having a single-line body. Another good ' \
'alternative is the usage of control flow &&/||.',
'',
'1 file inspected, 1 offense detected, 1 offense auto-correctable',
''].join("\n"))
end
end
it 'enables the given cop' do
create_file('example.rb',
['x = 0 ',
# Disabling comments still apply.
'# rubocop:disable Layout/TrailingWhitespace',
'y = 1 '])
create_file('.rubocop.yml', <<~YAML)
Layout/TrailingWhitespace:
Enabled: false
YAML
expect(cli.run(['--format', 'simple',
'--only', 'Layout/TrailingWhitespace',
'example.rb'])).to eq(1)
expect($stderr.string).to eq('')
expect($stdout.string)
.to eq(<<~RESULT)
== example.rb ==
C: 1: 6: [Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
1 file inspected, 1 offense detected, 1 offense auto-correctable
RESULT
end
end
context 'when several cops are given' do
it 'runs the given cops' do
create_file('example.rb', ['if x== 100000000000000 ',
"\ty",
'end'])
expect(cli.run(['--format', 'simple',
'--only',
'Style/IfUnlessModifier,Layout/IndentationStyle,' \
'Layout/SpaceAroundOperators',
'example.rb'])).to eq(1)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<~RESULT)
== example.rb ==
C: 1: 1: [Correctable] Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
C: 1: 5: [Correctable] Layout/SpaceAroundOperators: Surrounding space missing for operator ==.
C: 2: 1: [Correctable] Layout/IndentationStyle: Tab detected in indentation.
1 file inspected, 3 offenses detected, 3 offenses auto-correctable
RESULT
end
context 'and --lint' do
it 'runs the given cops plus all enabled lint cops' do
create_file('example.rb', ['if x== 100000000000000 ',
"\ty = 3",
' end'])
create_file('.rubocop.yml', <<~YAML)
Layout/EndAlignment:
Enabled: false
YAML
expect(cli.run(['--format', 'simple', '--only',
'Layout/IndentationStyle,Layout/SpaceAroundOperators',
'--lint', 'example.rb'])).to eq(1)
expect($stdout.string)
.to eq(<<~RESULT)
== example.rb ==
C: 1: 5: [Correctable] Layout/SpaceAroundOperators: Surrounding space missing for operator ==.
C: 2: 1: [Correctable] Layout/IndentationStyle: Tab detected in indentation.
W: 2: 2: Lint/UselessAssignment: Useless assignment to variable - y.
1 file inspected, 3 offenses detected, 2 offenses auto-correctable
RESULT
end
end
end
context 'when a namespace is given' do
it 'runs all enabled cops in that namespace' do
create_file('example.rb', ['if x== 100000000000000 ',
" #{'#' * 130}",
"\ty",
'end'])
expect(cli.run(%w[-f offenses --only Layout example.rb])).to eq(1)
expect($stdout.string).to eq(<<~RESULT)
1 Layout/CommentIndentation
1 Layout/IndentationStyle
1 Layout/IndentationWidth
1 Layout/LineLength
1 Layout/SpaceAroundOperators
1 Layout/TrailingWhitespace
--
6 Total
RESULT
end
end
context 'when three namespaces are given' do
it 'runs all enabled cops in those namespaces' do
create_file('example.rb', ['if x== 100000000000000 ',
" # #{'-' * 130}",
"\ty",
'end'])
create_file('.rubocop.yml', <<~YAML)
Layout/SpaceAroundOperators:
Enabled: false
YAML
expect(cli.run(%w[-f o --only Metrics,Style,Layout example.rb]))
.to eq(1)
expect($stdout.string)
.to eq(<<~RESULT)
1 Layout/CommentIndentation
1 Layout/IndentationStyle
1 Layout/IndentationWidth
1 Layout/LineLength
1 Layout/TrailingWhitespace
1 Style/FrozenStringLiteralComment
1 Style/NumericLiterals
--
7 Total
RESULT
end
end
context 'when a cop name is not specified' do
it 'displays how to use `--only` option' do
expect(cli.run(%w[--except -a Lint/NumberConverion])).to eq(2)
expect($stderr.string).to eq(<<~MESSAGE)
--except argument should be [COP1,COP2,...].
MESSAGE
end
end
context 'when the cop is defined in a require', :restore_registry do
before do
create_file('.rubocop.yml', <<~YAML)
require:
- './custom_cops.rb'
YAML
create_file('example.rb', 'foo(:bar)')
end
context 'when the cop name is not duplicated' do
before do
create_file('custom_cops.rb', <<~RUBY)
module CustomCops
class NoMethods < RuboCop::Cop::Base
MSG = 'Methods are not allowed.'
def on_send(node)
add_offense(node)
end
end
end
RUBY
end
it 'runs the cop without warnings' do
expect(cli.run(['--format', 'simple',
'--only', 'CustomCops/NoMethods',
'example.rb'])).to eq(1)
expect($stdout.string).to eq(<<~RESULT)
== example.rb ==
C: 1: 1: CustomCops/NoMethods: Methods are not allowed.
1 file inspected, 1 offense detected
RESULT
expect($stderr.string).not_to match(%r{CustomCops/NoMethods has the wrong namespace})
end
end
context 'when the cop name duplicates a built-in cop' do
before do
create_file('custom_cops.rb', <<~RUBY)
module CustomCops
class MethodLength < RuboCop::Cop::Base
MSG = 'Method is too long.'
def on_send(node)
add_offense(node)
end
end
end
RUBY
end
it 'runs the correct cop without warnings' do
expect(cli.run(['--format', 'simple',
'--only', 'CustomCops/MethodLength',
'example.rb'])).to eq(1)
expect($stdout.string).to eq(<<~RESULT)
== example.rb ==
C: 1: 1: CustomCops/MethodLength: Method is too long.
1 file inspected, 1 offense detected
RESULT
expect($stderr.string).not_to match(%r{CustomCops/MethodLength has the wrong namespace})
end
end
end
end
describe '--except' do
context 'when one name is given' do
it 'exits with error if the cop name is incorrect' do
create_file('example.rb', ['if x== 0 ',
"\ty",
'end'])
expect(cli.run(['--except', 'Style/123'])).to eq(2)
expect($stderr.string)
.to include('Unrecognized cop or department: Style/123.')
end
it 'exits with error if an empty string is given' do
create_file('example.rb', 'x')
expect(cli.run(['--except', ''])).to eq(2)
expect($stderr.string).to include('Unrecognized cop or department: .')
end
it 'displays correction candidate if an incorrect cop name is given' do
create_file('example.rb', 'x')
expect(cli.run(['--except', 'Style/BlockComment'])).to eq(2)
expect($stderr.string)
.to include('Unrecognized cop or department: Style/BlockComment.')
expect($stderr.string)
.to include('Did you mean? Style/BlockComments')
end
%w[Syntax Lint/Syntax].each do |name|
it "exits with error if #{name} is given" do
create_file('example.rb', 'x ')
expect(cli.run(['--except', name])).to eq(2)
expect($stderr.string)
.to include('Syntax checking cannot be turned off.')
end
end
end
context 'when one cop plus one namespace are given' do
it 'runs all cops except the given' do
# The disable comment should not be reported as unnecessary (even if
# it is) since --except overrides configuration.
create_file('example.rb', ['# rubocop:disable LineLength',
'if x== 0 ',
"\ty = 3",
'end'])
expect(cli.run(['--format', 'offenses',
'--except', 'Style/IfUnlessModifier,Lint',
'example.rb'])).to eq(1)
# NOTE: No Lint/UselessAssignment offense.
expect($stdout.string)
.to eq(<<~RESULT)
1 Layout/IndentationStyle
1 Layout/IndentationWidth
1 Layout/SpaceAroundOperators
1 Layout/TrailingWhitespace
1 Migration/DepartmentName
1 Style/FrozenStringLiteralComment
1 Style/NumericPredicate
--
7 Total
RESULT
end
end
context 'when one cop is given without namespace' do
it 'disables the given cop' do
create_file('example.rb', ['if x== 0 ',
"\ty",
'end'])
cli.run(['--format', 'offenses',
'--except', 'Style/IfUnlessModifier',
'example.rb'])
with_option = $stdout.string
$stdout = StringIO.new
cli.run(['--format', 'offenses',
'example.rb'])
without_option = $stdout.string
expect($stderr.string).to eq('')
expect(without_option.split($RS) - with_option.split($RS))
.to eq(['1 Style/IfUnlessModifier', '7 Total'])
end
end
context 'when several cops are given' do
%w[RedundantCopDisableDirective
Lint/RedundantCopDisableDirective Lint].each do |cop_name|
it "disables the given cops including #{cop_name}" do
create_file('example.rb', ['if x== 100000000000000 ',
"\ty",
'end # rubocop:disable all'])
expect(cli.run(['--format', 'offenses',
'--except',
'Style/IfUnlessModifier,Layout/IndentationStyle,' \
"Layout/SpaceAroundOperators,#{cop_name}",
'example.rb'])).to eq(1)
if cop_name == 'RedundantCopDisableDirective'
expect($stderr.string.chomp)
.to eq('--except option: Warning: no department given for ' \
'RedundantCopDisableDirective.')
end
expect($stdout.string)
.to eq(<<~RESULT)
1 Layout/IndentationWidth
1 Layout/TrailingWhitespace
1 Style/FrozenStringLiteralComment
1 Style/NumericLiterals
--
4 Total
RESULT
end
end
end
context 'when a cop name is not specified' do
it 'displays how to use `--except` option' do
expect(cli.run(%w[--except])).to eq(2)
expect($stderr.string).to eq(<<~MESSAGE)
--except argument should be [COP1,COP2,...].
MESSAGE
end
end
end
describe '--lint' do
it 'runs only lint cops' do
create_file('example.rb',
['if 0 ',
"\ty",
"\tz # rubocop:disable Layout/IndentationStyle",
'end'])
# IfUnlessModifier depends on the configuration of LineLength.
expect(cli.run(['--format', 'simple', '--lint',
'example.rb'])).to eq(1)
expect($stdout.string)
.to eq(<<~RESULT)
== example.rb ==
W: 1: 4: Lint/LiteralAsCondition: Literal 0 appeared as a condition.
1 file inspected, 1 offense detected
RESULT
end
end
describe '-d/--debug' do
before do
RuboCop::ConfigLoader.default_configuration = nil
end
it 'shows config files' do
create_file('example1.rb', "\tputs 0")
expect(cli.run(['--debug', 'example1.rb'])).to eq(1)
home = File.dirname(File.dirname(File.dirname(File.dirname(__FILE__))))
expect($stdout.string.lines.grep(/configuration/).map(&:chomp))
.to eq(["For #{abs('')}:" \
" Default configuration from #{home}/config/default.yml"])
end
it 'shows cop names' do
create_file('example1.rb', 'puts 0 ')
file = abs('example1.rb')
expect(cli.run(['--format',
'emacs',
'--debug',
'example1.rb'])).to eq(1)
expect($stdout.string.lines.to_a[-1])
.to eq("#{file}:1:7: C: [Correctable] Layout/TrailingWhitespace: Trailing " \
"whitespace detected.\n")
end
end
describe '--display-time' do
before do
create_file('example1.rb', '')
end
regex = /Finished in [0-9]*\.[0-9]* seconds/
context 'without --display-time' do
it 'does not display elapsed time in seconds' do
expect(`rubocop example1.rb`).not_to match(regex)
end
end
context 'with --display-time' do
it 'displays elapsed time in seconds' do
expect(`rubocop --display-time example1.rb`).to match(regex)
end
end
end
describe '-D/--display-cop-names' do
before do
create_file('example1.rb', 'puts 0 # rubocop:disable NumericLiterals ')
end
let(:file) { abs('example1.rb') }
it 'shows cop names' do
expect(cli.run(['--format', 'emacs', '--display-cop-names',
'example1.rb'])).to eq(1)
expect($stdout.string).to eq(<<~RESULT)
#{file}:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
#{file}:1:8: W: [Correctable] Lint/RedundantCopDisableDirective: Unnecessary disabling of `Style/NumericLiterals`.
#{file}:1:26: C: [Correctable] Migration/DepartmentName: Department name is missing.
#{file}:1:41: C: [Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
RESULT
end
context '--no-display-cop-names' do
it 'does not show cop names' do