/
autocorrect_spec.rb
1954 lines (1742 loc) · 53.7 KB
/
autocorrect_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 --autocorrect', :isolated_environment do # rubocop:disable RSpec/DescribeClass
subject(:cli) { RuboCop::CLI.new }
include_context 'cli spec behavior'
before do
RuboCop::ConfigLoader.default_configuration = nil
RuboCop::ConfigLoader.default_configuration.for_all_cops['SuggestExtensions'] = false
end
it 'does not correct ExtraSpacing in a hash that would be changed back' do
create_file('.rubocop.yml', <<~YAML)
Layout/HashAlignment:
EnforcedColonStyle: table
Style/FrozenStringLiteralComment:
Enabled: false
YAML
source = <<~RUBY
hash = {
alice: {
age: 23,
role: 'Director'
},
bob: {
age: 25,
role: 'Consultant'
}
}
RUBY
create_file('example.rb', source)
expect(cli.run(['--auto-correct-all'])).to eq(1)
expect(IO.read('example.rb')).to eq(source)
end
it 'plays nicely with default cops in complex ExtraSpacing scenarios' do
create_file('.rubocop.yml', <<~YAML)
# These cops change indentation and thus need disabling in order for the
# ExtraSpacing rules to apply to this scenario.
Layout/BlockAlignment:
Enabled: false
Layout/ExtraSpacing:
ForceEqualSignAlignment: true
Layout/MultilineMethodCallBraceLayout:
Enabled: false
YAML
source = <<~RUBY
def batch
@areas = params[:param].map do
var_1 = 123_456
variable_2 = 456_123
end
@another = params[:param].map do
char_1 = begin
variable_1_1 = 'a'
variable_1_20 = 'b'
variable_1_300 = 'c'
# A Comment
variable_1_4000 = 'd'
variable_1_50000 = 'e'
puts 'a non-assignment statement without a blank line'
some_other_length_variable = 'f'
end
var_2 = 456_123
end
render json: @areas
end
RUBY
create_file('example.rb', source)
expect(cli.run(['--auto-correct-all'])).to eq(1)
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
def batch
@areas = params[:param].map do
var_1 = 123_456
variable_2 = 456_123
end
@another = params[:param].map do
char_1 = begin
variable_1_1 = 'a'
variable_1_20 = 'b'
variable_1_300 = 'c'
# A Comment
variable_1_4000 = 'd'
variable_1_50000 = 'e'
puts 'a non-assignment statement without a blank line'
some_other_length_variable = 'f'
end
var_2 = 456_123
end
render json: @areas
end
RUBY
end
it 'corrects `Layout/SpaceAroundOperators` and `Layout/ExtraSpacing` ' \
'offenses when using `ForceEqualSignAlignment: true`' do
create_file('.rubocop.yml', <<~YAML)
Layout/ExtraSpacing:
ForceEqualSignAlignment: true
YAML
create_file('example.rb', <<~RUBY)
test123456 = nil
test1234 = nil
test1_test2_test3_test4_12 =nil
RUBY
expect(cli.run(['--auto-correct-all'])).to eq(1)
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
test123456 = nil
test1234 = nil
test1_test2_test3_test4_12 = nil
RUBY
end
it 'does not correct SpaceAroundOperators in a hash that would be ' \
'changed back' do
create_file('.rubocop.yml', <<~YAML)
Style/HashSyntax:
EnforcedStyle: hash_rockets
Layout/HashAlignment:
EnforcedHashRocketStyle: table
YAML
source = <<~RUBY
a = { 1=>2, a => b }
hash = {
:alice => {
:age => 23,
:role => 'Director'
},
:bob => {
:age => 25,
:role => 'Consultant'
}
}
RUBY
create_file('example.rb', source)
expect(cli.run(['--auto-correct-all'])).to eq(1)
# 1=>2 is changed to 1 => 2. The rest is unchanged.
# SpaceAroundOperators leaves it to HashAlignment when the style is table.
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
a = { 1 => 2, a => b }
hash = {
:alice => {
:age => 23,
:role => 'Director'
},
:bob => {
:age => 25,
:role => 'Consultant'
}
}
RUBY
end
it 'corrects `EnforcedStyle: hash_rockets` of `Style/HashSyntax` with `Layout/HashAlignment`' do
create_file('.rubocop.yml', <<~YAML)
Style/HashSyntax:
EnforcedStyle: hash_rockets
YAML
source = <<~RUBY
some_method(a: 'abc', b: 'abc',
c: 'abc', d: 'abc'
)
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only', 'Style/HashSyntax,Style/HashAlignment'
])).to eq(0)
expect(IO.read('example.rb')).to eq(<<~RUBY)
some_method(:a => 'abc', :b => 'abc',
:c => 'abc', :d => 'abc'
)
RUBY
end
it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with `Style/NestedParenthesizedCalls`' do
create_file('.rubocop.yml', <<~YAML)
Style/MethodCallWithArgsParentheses:
EnforcedStyle: require_parentheses
YAML
source = <<~RUBY
a(b 1)
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only', 'Style/MethodCallWithArgsParentheses,Style/NestedParenthesizedCalls'
])).to eq(0)
expect(IO.read('example.rb')).to eq(<<~RUBY)
a(b(1))
RUBY
end
it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with ' \
'`EnforcedStyle: conditionals` of `Style/AndOr`' do
create_file('.rubocop.yml', <<~YAML)
Style/MethodCallWithArgsParentheses:
EnforcedStyle: require_parentheses
Style/AndOr:
EnforcedStyle: conditionals
YAML
create_file('example.rb', <<~RUBY)
if foo and bar :arg
end
RUBY
expect(
cli.run(['--auto-correct', '--only', 'Style/MethodCallWithArgsParentheses,Style/AndOr'])
).to eq(0)
expect(IO.read('example.rb')).to eq(<<~RUBY)
if foo && bar(:arg)
end
RUBY
end
it 'corrects `Style/IfUnlessModifier` with `Style/SoleNestedConditional`' do
source = <<~RUBY
def foo
# NOTE: comment
if a? && b?
puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message" unless c?
end
end
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only', 'Style/IfUnlessModifier,Style/SoleNestedConditional'
])).to eq(0)
expect(IO.read('example.rb')).to eq(<<~RUBY)
def foo
# NOTE: comment
if a? && b? && !c?
puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message"
end
end
RUBY
end
describe 'trailing comma cops' do
let(:source) do
<<~RUBY
func({
@abc => 0,
@xyz => 1
})
func(
{
abc: 0
}
)
func(
{},
{
xyz: 1
}
)
RUBY
end
let(:config) do
{
'Style/TrailingCommaInArguments' => {
'EnforcedStyleForMultiline' => comma_style
},
'Style/TrailingCommaInArrayLiteral' => {
'EnforcedStyleForMultiline' => comma_style
},
'Style/TrailingCommaInHashLiteral' => {
'EnforcedStyleForMultiline' => comma_style
}
}
end
before do
create_file('example.rb', source)
create_file('.rubocop.yml', YAML.dump(config))
end
shared_examples 'corrects offenses without producing a double comma' do
it 'corrects TrailingCommaInLiteral and TrailingCommaInArguments ' \
'without producing a double comma' do
cli.run(['--auto-correct-all'])
expect(IO.read('example.rb'))
.to eq(expected_corrected_source)
expect($stderr.string).to eq('')
end
end
context 'when the style is `comma`' do
let(:comma_style) do
'comma'
end
let(:expected_corrected_source) do
<<~RUBY
# frozen_string_literal: true
func({
@abc => 0,
@xyz => 1,
})
func(
{
abc: 0,
},
)
func(
{},
{
xyz: 1,
},
)
RUBY
end
include_examples 'corrects offenses without producing a double comma'
end
context 'when the style is `consistent_comma`' do
let(:comma_style) do
'consistent_comma'
end
let(:expected_corrected_source) do
<<~RUBY
# frozen_string_literal: true
func({
@abc => 0,
@xyz => 1,
})
func(
{
abc: 0,
},
)
func(
{},
{
xyz: 1,
},
)
RUBY
end
include_examples 'corrects offenses without producing a double comma'
end
end
context 'space_inside_bracket cops' do
let(:source) do
<<~RUBY
[ a[b], c[ d ], [1, 2] ]
foo[[ 3, 4 ], [5, 6] ]
RUBY
end
let(:config) do
{
'Layout/SpaceInsideArrayLiteralBrackets' => {
'EnforcedStyle' => array_style
},
'Layout/SpaceInsideReferenceBrackets' => {
'EnforcedStyle' => reference_style
}
}
end
before do
create_file('example.rb', source)
create_file('.rubocop.yml', YAML.dump(config))
end
shared_examples 'corrects offenses' do
it 'corrects SpaceInsideArrayLiteralBrackets and ' \
'SpaceInsideReferenceBrackets' do
cli.run(['--auto-correct-all'])
expect(IO.read('example.rb'))
.to eq(corrected_source)
expect($stderr.string).to eq('')
end
end
context 'when array style is space & reference style is no space' do
let(:array_style) { 'space' }
let(:reference_style) { 'no_space' }
let(:corrected_source) do
<<~RUBY
# frozen_string_literal: true
[ a[b], c[d], [ 1, 2 ] ]
foo[[ 3, 4 ], [ 5, 6 ]]
RUBY
end
include_examples 'corrects offenses'
end
context 'when array style is no_space & reference style is space' do
let(:array_style) { 'no_space' }
let(:reference_style) { 'space' }
let(:corrected_source) do
<<~RUBY
# frozen_string_literal: true
[a[ b ], c[ d ], [1, 2]]
foo[ [3, 4], [5, 6] ]
RUBY
end
include_examples 'corrects offenses'
end
context 'when array style is compact & reference style is no_space' do
let(:array_style) { 'compact' }
let(:reference_style) { 'no_space' }
let(:corrected_source) do
<<~RUBY
# frozen_string_literal: true
[ a[b], c[d], [ 1, 2 ]]
foo[[ 3, 4 ], [ 5, 6 ]]
RUBY
end
include_examples 'corrects offenses'
end
context 'when array style is compact & reference style is space' do
let(:array_style) { 'compact' }
let(:reference_style) { 'space' }
let(:corrected_source) do
<<~RUBY
# frozen_string_literal: true
[ a[ b ], c[ d ], [ 1, 2 ]]
foo[ [ 3, 4 ], [ 5, 6 ] ]
RUBY
end
include_examples 'corrects offenses'
end
end
it 'corrects IndentationWidth, RedundantBegin, and ' \
'RescueEnsureAlignment offenses' do
source = <<~RUBY
def verify_section
begin
scroll_down_until_element_exists
rescue StandardError
scroll_down_until_element_exists
end
end
RUBY
create_file('example.rb', source)
expect(cli.run(['--auto-correct-all'])).to eq(0)
corrected = <<~RUBY
# frozen_string_literal: true
def verify_section
scroll_down_until_element_exists
rescue StandardError
scroll_down_until_element_exists
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end
it 'corrects LineEndConcatenation offenses leaving the ' \
'RedundantInterpolation offense unchanged' do
# If we change string concatenation from plus to backslash, the string
# literal that follows must remain a string literal.
source = <<~'RUBY'
puts 'foo' +
"#{bar}"
puts 'a' +
'b'
"#{c}"
RUBY
create_file('example.rb', source)
expect(cli.run(['--auto-correct-all'])).to eq(0)
corrected = ['# frozen_string_literal: true',
'',
"puts 'foo' \\",
' "#{bar}"',
# Expressions that need correction from only one of these cops
# are corrected as expected.
"puts 'a' \\",
" 'b'",
'c.to_s',
''].join("\n")
expect(IO.read('example.rb')).to eq(corrected)
end
it 'corrects Style/InverseMethods and Style/Not offenses' do
source = <<~'RUBY'
x.select {|y| not y.z }
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only', 'Style/InverseMethods,Style/Not'
])).to eq(0)
corrected = <<~'RUBY'
x.reject {|y| y.z }
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end
it 'corrects Style/Next and Style/SafeNavigation offenses' do
create_file('.rubocop.yml', <<~YAML)
AllCops:
TargetRubyVersion: 2.4
YAML
source = <<~'RUBY'
until x
if foo
foo.some_method do
y
end
end
end
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only', 'Style/Next,Style/SafeNavigation'
])).to eq(0)
corrected = <<~'RUBY'
until x
next unless foo
foo.some_method do
y
end
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end
it 'corrects `Lint/Lambda` and `Lint/UnusedBlockArgument` offenses' do
source = <<~'RUBY'
c = -> event do
puts 'Hello world'
end
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only', 'Lint/Lambda,Lint/UnusedBlockArgument'
])).to eq(0)
corrected = <<~'RUBY'
c = lambda do |_event|
puts 'Hello world'
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end
describe 'caching' do
let(:cache) do
instance_double(RuboCop::ResultCache, 'valid?' => true,
'load' => cached_offenses)
end
let(:source) { %(puts "Hi"\n) }
before do
allow(RuboCop::ResultCache).to receive(:new) { cache }
create_file('example.rb', source)
end
context 'with no offenses in the cache' do
let(:cached_offenses) { [] }
it "doesn't correct offenses" do
expect(cli.run(['--auto-correct-all'])).to eq(0)
expect(IO.read('example.rb')).to eq(source)
end
end
context 'with an offense in the cache' do
let(:cached_offenses) { ['Style/StringLiterals: ...'] }
it 'corrects offenses' do
allow(cache).to receive(:save)
expect(cli.run(['--auto-correct-all'])).to eq(0)
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
puts 'Hi'
RUBY
end
end
end
%i[line_count_based semantic braces_for_chaining].each do |style|
context "when BlockDelimiters has #{style} style" do
it 'corrects SpaceBeforeBlockBraces, SpaceInsideBlockBraces offenses' do
source = <<~RUBY
r = foo.map{|a|
a.bar.to_s
}
foo.map{|a|
a.bar.to_s
}.baz
RUBY
create_file('example.rb', source)
create_file('.rubocop.yml', <<~YAML)
Style/BlockDelimiters:
EnforcedStyle: #{style}
YAML
expect(cli.run(['--auto-correct-all'])).to eq(1)
# rubocop:disable Style/HashLikeCase
corrected = case style
when :semantic
<<~RUBY
# frozen_string_literal: true
r = foo.map { |a|
a.bar.to_s
}
foo.map { |a|
a.bar.to_s
}.baz
RUBY
when :braces_for_chaining
<<~RUBY
# frozen_string_literal: true
r = foo.map do |a|
a.bar.to_s
end
foo.map { |a|
a.bar.to_s
}.baz
RUBY
when :line_count_based
<<~RUBY
# frozen_string_literal: true
r = foo.map do |a|
a.bar.to_s
end
foo.map do |a|
a.bar.to_s
end.baz
RUBY
end
# rubocop:enable Style/HashLikeCase
expect($stderr.string).to eq('')
expect(IO.read('example.rb')).to eq(corrected)
end
end
end
it 'corrects InitialIndentation offenses' do
source = <<~RUBY
# comment 1
# comment 2
def func
begin
foo
bar
rescue StandardError
baz
end
end
RUBY
create_file('example.rb', source)
create_file('.rubocop.yml', <<~YAML)
Layout/DefEndAlignment:
AutoCorrect: true
YAML
expect(cli.run(['--auto-correct-all'])).to eq(0)
corrected = <<~RUBY
# frozen_string_literal: true
# comment 1
# comment 2
def func
foo
bar
rescue StandardError
baz
end
RUBY
expect($stderr.string).to eq('')
expect(IO.read('example.rb')).to eq(corrected)
end
it 'corrects RedundantCopDisableDirective offenses' do
source = <<~RUBY
class A
# rubocop:disable Metrics/MethodLength
def func
# rubocop:enable Metrics/MethodLength
x = foo # rubocop:disable Lint/UselessAssignment,Style/For
# rubocop:disable all
# rubocop:disable Style/ClassVars
@@bar = "3"
end
end
RUBY
create_file('example.rb', source)
expect(cli.run(%w[--auto-correct-all --format simple])).to eq(1)
expect($stdout.string).to eq(<<~RESULT)
== example.rb ==
C: 1: 1: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
C: 3: 1: Style/Documentation: Missing top-level class documentation comment.
W: 4: 3: [Corrected] Lint/RedundantCopDisableDirective: Unnecessary disabling of Metrics/MethodLength.
C: 5: 3: [Corrected] Layout/IndentationWidth: Use 2 (not 6) spaces for indentation.
W: 5: 22: [Corrected] Lint/RedundantCopEnableDirective: Unnecessary enabling of Metrics/MethodLength.
W: 7: 54: [Corrected] Lint/RedundantCopDisableDirective: Unnecessary disabling of Style/For.
W: 9: 5: [Corrected] Lint/RedundantCopDisableDirective: Unnecessary disabling of Style/ClassVars.
1 file inspected, 8 offenses detected, 7 offenses corrected
RESULT
corrected = <<~RUBY
# frozen_string_literal: true
class A
def func
x = foo # rubocop:disable Lint/UselessAssignment
# rubocop:disable all
@@bar = "3"
end
end
RUBY
expect($stderr.string).to eq('')
expect(IO.read('example.rb')).to eq(corrected)
end
it 'corrects RedundantBegin offenses and fixes indentation etc' do
source = <<~RUBY
def func
begin
foo
bar
rescue
baz
end
end
def func; begin; x; y; rescue; z end end
def method
begin
BlockA do |strategy|
foo
end
BlockB do |portfolio|
foo
end
rescue => e # some problem
bar
end
end
def method
begin # comment 1
do_some_stuff
rescue # comment 2
end # comment 3
end
RUBY
create_file('example.rb', source)
expect(cli.run(['--auto-correct-all'])).to eq(1)
corrected = <<~RUBY
# frozen_string_literal: true
def func
foo
bar
rescue StandardError
baz
end
def func
x; y; rescue StandardError; z
end
def method
BlockA do |_strategy|
foo
end
BlockB do |_portfolio|
foo
end
rescue StandardError => e # some problem
bar
end
def method
# comment 1
do_some_stuff
rescue StandardError # comment 2
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end
it 'corrects Tab and IndentationConsistency offenses' do
source = <<~RUBY
render_views
describe 'GET index' do
\t it 'returns http success' do
\t end
\tdescribe 'admin user' do
before(:each) do
\t end
\tend
end
RUBY
create_file('example.rb', source)
expect(cli.run(['--auto-correct-all'])).to eq(0)
corrected = <<~RUBY
# frozen_string_literal: true
render_views
describe 'GET index' do
it 'returns http success' do
end
describe 'admin user' do
before(:each) do
end
end
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end
it 'corrects IndentationWidth and IndentationConsistency offenses' do
source = <<~RUBY
require 'spec_helper'
describe ArticlesController do
render_views
describe "GET \'index\'" do
it "returns http success" do
end
describe "admin user" do
before(:each) do
end
end
end
end
RUBY
create_file('example.rb', source)
expect(cli.run(['--auto-correct-all'])).to eq(0)
corrected = <<~RUBY
# frozen_string_literal: true
require 'spec_helper'
describe ArticlesController do
render_views
describe \"GET 'index'\" do
it 'returns http success' do
end
describe 'admin user' do
before(:each) do
end
end
end
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end
it 'corrects IndentationWidth and IndentationConsistency offenses' \
'when using `EnforcedStyle: outdent` and ' \
'`EnforcedStyle: indented_internal_methods`' do
create_file('.rubocop.yml', <<~YAML)
Layout/AccessModifierIndentation:
EnforcedStyle: outdent
Layout/IndentationConsistency:
EnforcedStyle: indented_internal_methods
YAML
source = <<~'RUBY'
class Foo
private
def do_something
# something
end
end
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only',
[
'Layout/AccessModifierIndentation',
'Layout/IndentationConsistency',
'Layout/IndentationWidth'
].join(',')
])).to eq(0)
corrected = <<~'RUBY'
class Foo
private
def do_something
# something
end
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end
it 'corrects SymbolProc and SpaceBeforeBlockBraces offenses' do
source = ['foo.map{ |a| a.nil? }']
create_file('example.rb', source)
expect(cli.run(['-D', '--auto-correct-all'])).to eq(0)
corrected = "# frozen_string_literal: true\n\nfoo.map(&:nil?)\n"
expect(IO.read('example.rb')).to eq(corrected)
uncorrected = $stdout.string.split($RS).select do |line|
line.include?('example.rb:') && !line.include?('[Corrected]')
end
expect(uncorrected.empty?).to be(true) # Hence exit code 0.
end
it 'corrects only IndentationWidth without crashing' do
source = <<~RUBY
foo = if bar
something
elsif baz
other_thing
else
raise
end
RUBY
create_file('example.rb', source)
expect(cli.run(%w[--only IndentationWidth --auto-correct-all])).to eq(0)
corrected = <<~RUBY
foo = if bar
something
elsif baz
other_thing
else
raise
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end
it 'honors Exclude settings in individual cops' do
source = 'puts %x(ls)'
create_file('example.rb', source)
create_file('.rubocop.yml', <<~YAML)
Style/CommandLiteral:
Exclude:
- example.rb
Style/FrozenStringLiteralComment:
Enabled: false
YAML
expect(cli.run(['--auto-correct-all'])).to eq(0)
expect($stdout.string).to include('no offenses detected')
expect(IO.read('example.rb')).to eq("#{source}\n")
end
it 'corrects code with indentation problems' do
create_file('example.rb', <<~RUBY)