/
cli_disable_uncorrectable_spec.rb
191 lines (165 loc) · 7.61 KB
/
cli_disable_uncorrectable_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
# frozen_string_literal: true
RSpec.describe RuboCop::CLI, :isolated_environment do
include_context 'cli spec behavior'
subject(:cli) { described_class.new }
describe '--disable-uncorrectable' do
let(:exit_code) do
cli.run(%w[--auto-correct --format simple --disable-uncorrectable])
end
let(:setup_long_line) do
create_file('.rubocop.yml', <<~YAML)
Style/IpAddresses:
Enabled: true
Layout/LineLength:
Max: #{max_length}
YAML
create_file('example.rb', <<~RUBY)
ip('1.2.3.4')
# last line
RUBY
end
let(:max_length) { 46 }
it 'does not disable anything for cops that support autocorrect' do
create_file('example.rb', 'puts 1==2')
expect(exit_code).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<~OUTPUT)
== example.rb ==
C: 1: 1: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 1: 7: [Corrected] Layout/SpaceAroundOperators: Surrounding space missing for operator ==.
C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
1 file inspected, 3 offenses detected, 3 offenses corrected
OUTPUT
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
puts 1 == 2
RUBY
end
context 'if one one-line disable statement fits' do
it 'adds it' do
setup_long_line
expect(exit_code).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<~OUTPUT)
== example.rb ==
C: 1: 1: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 1: 4: [Todo] Style/IpAddresses: Do not hardcode IP addresses.
C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
1 file inspected, 3 offenses detected, 3 offenses corrected
OUTPUT
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
ip('1.2.3.4') # rubocop:todo Style/IpAddresses
# last line
RUBY
end
context 'and there are two offenses of the same kind on one line' do
it 'adds a single one-line disable statement' do
create_file('.rubocop.yml', <<~YAML)
Style/IpAddresses:
Enabled: true
YAML
create_file('example.rb', <<~RUBY)
ip('1.2.3.4', '5.6.7.8')
RUBY
expect(exit_code).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<~OUTPUT)
== example.rb ==
C: 1: 1: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 1: 4: [Todo] Style/IpAddresses: Do not hardcode IP addresses.
C: 1: 15: [Todo] Style/IpAddresses: Do not hardcode IP addresses.
C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
1 file inspected, 4 offenses detected, 4 offenses corrected
OUTPUT
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
ip('1.2.3.4', '5.6.7.8') # rubocop:todo Style/IpAddresses
RUBY
end
end
context "but there are more offenses on the line and they don't all " \
'fit' do
it 'adds both one-line and before-and-after disable statements' do
create_file('example.rb', <<~RUBY)
# Chess engine.
class Chess
def choose_move(who_to_move)
legal_moves = all_legal_moves_that_dont_put_me_in_check(who_to_move)
return nil if legal_moves.empty?
mating_move = checkmating_move(legal_moves)
return mating_move if mating_move
best_moves = checking_moves(legal_moves)
best_moves = castling_moves(legal_moves) if best_moves.empty?
best_moves = taking_moves(legal_moves) if best_moves.empty?
best_moves = legal_moves if best_moves.empty?
best_moves = remove_dangerous_moves(best_moves, who_to_move)
best_moves = legal_moves if best_moves.empty?
best_moves.sample
end
end
RUBY
expect(exit_code).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<~OUTPUT)
== 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: 3: [Todo] Metrics/AbcSize: Assignment Branch Condition size for choose_move is too high. [<8, 12, 6> 15.62/15]
C: 3: 3: [Todo] Metrics/CyclomaticComplexity: Cyclomatic complexity for choose_move is too high. [7/6]
C: 3: 3: [Todo] Metrics/MethodLength: Method has too many lines. [11/10]
C: 4: 3: [Todo] Metrics/AbcSize: Assignment Branch Condition size for choose_move is too high. [<8, 12, 6> 15.62/15]
C: 4: 3: [Todo] Metrics/MethodLength: Method has too many lines. [11/10]
C: 4: 32: [Corrected] Style/DoubleCopDisableDirective: More than one disable comment on one line.
1 file inspected, 8 offenses detected, 8 offenses corrected
OUTPUT
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
# Chess engine.
class Chess
# rubocop:todo Metrics/MethodLength
# rubocop:todo Metrics/AbcSize
def choose_move(who_to_move) # rubocop:todo Metrics/CyclomaticComplexity
legal_moves = all_legal_moves_that_dont_put_me_in_check(who_to_move)
return nil if legal_moves.empty?
mating_move = checkmating_move(legal_moves)
return mating_move if mating_move
best_moves = checking_moves(legal_moves)
best_moves = castling_moves(legal_moves) if best_moves.empty?
best_moves = taking_moves(legal_moves) if best_moves.empty?
best_moves = legal_moves if best_moves.empty?
best_moves = remove_dangerous_moves(best_moves, who_to_move)
best_moves = legal_moves if best_moves.empty?
best_moves.sample
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
end
RUBY
end
end
end
context "if a one-line disable statement doesn't fit" do
let(:max_length) { super() - 1 }
it 'adds before-and-after disable statement' do
setup_long_line
expect(exit_code).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<~OUTPUT)
== example.rb ==
C: 1: 1: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 1: 4: [Todo] Style/IpAddresses: Do not hardcode IP addresses.
C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
1 file inspected, 3 offenses detected, 3 offenses corrected
OUTPUT
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
# rubocop:todo Style/IpAddresses
ip('1.2.3.4')
# rubocop:enable Style/IpAddresses
# last line
RUBY
end
end
end
end