forked from rubocop/rubocop
/
runner_spec.rb
327 lines (270 loc) · 8.41 KB
/
runner_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
# frozen_string_literal: true
module RuboCop
class Runner
attr_writer :errors # Needed only for testing.
end
end
RSpec.describe RuboCop::Runner, :isolated_environment do
include FileHelper
let(:formatter_output_path) { 'formatter_output.txt' }
let(:formatter_output) { File.read(formatter_output_path) }
describe '#run when interrupted' do
include_context 'cli spec behavior'
let(:runner) { described_class.new({}, RuboCop::ConfigStore.new) }
before do
create_empty_file('example.rb')
end
def interrupt(pid)
Process.kill 'INT', pid
end
def wait_for_input(io)
line = nil
until line
line = io.gets
sleep 0.1
end
line
end
around do |example|
old_handler = Signal.trap('INT', 'DEFAULT')
example.run
Signal.trap('INT', old_handler)
end
context 'with SIGINT' do
it 'returns false' do
skip unless Process.respond_to?(:fork)
# Make sure the runner works slowly and thus is interruptible
allow(runner).to receive(:process_file) do
sleep 99
end
rd, wr = IO.pipe
pid = Process.fork do
rd.close
wr.puts 'READY'
wr.puts runner.run(['example.rb'])
wr.close
end
wr.close
# Make sure the runner has started by waiting for a specific message
line = wait_for_input(rd)
expect(line.chomp).to eq('READY')
# Interrupt the runner
interrupt(pid)
# Make sure the runner returns false
line = wait_for_input(rd)
expect(line.chomp).to eq('false')
end
end
end
describe '#run' do
subject(:runner) { described_class.new(options, RuboCop::ConfigStore.new) }
before do
create_file('example.rb', source)
end
let(:options) { { formatters: [['progress', formatter_output_path]] } }
context 'if there are no offenses in inspected files' do
let(:source) { <<~RUBY }
# frozen_string_literal: true
def valid_code; end
RUBY
it 'returns true' do
expect(runner.run([])).to be true
end
end
context 'if there is an offense in an inspected file' do
let(:source) { <<~RUBY }
# frozen_string_literal: true
def INVALID_CODE; end
RUBY
it 'returns false' do
expect(runner.run([])).to be false
end
it 'sends the offense to a formatter' do
runner.run([])
expect(formatter_output).to eq <<~RESULT
Inspecting 1 file
C
Offenses:
example.rb:3:5: C: Naming/MethodName: Use snake_case for method names.
def INVALID_CODE; end
^^^^^^^^^^^^
1 file inspected, 1 offense detected
RESULT
end
end
context 'if a cop crashes' do
before do
# The cache responds that it's not valid, which means that new results
# should normally be collected and saved...
cache = instance_double(RuboCop::ResultCache, 'valid?' => false)
# ... but there's a crash in one cop.
runner.errors = ['An error occurred in ...']
allow(RuboCop::ResultCache).to receive(:new) { cache }
end
let(:source) { '' }
it 'does not call ResultCache#save' do
# The double doesn't define #save, so we'd get an error if it were
# called.
runner.run([])
end
end
context 'if -s/--stdin is used with an offense' do
before do
# Make Style/EndOfLine give same output regardless of platform.
create_file('.rubocop.yml', <<~YAML)
Layout/EndOfLine:
EnforcedStyle: lf
YAML
end
let(:options) do
{
formatters: [['progress', formatter_output_path]],
stdin: <<~RUBY
# frozen_string_literal: true
def INVALID_CODE; end
RUBY
}
end
let(:source) { '' }
it 'returns false' do
expect(runner.run([])).to be false
end
it 'sends the offense to a formatter' do
runner.run([])
expect(formatter_output).to eq <<~RESULT
Inspecting 1 file
C
Offenses:
example.rb:3:5: C: Naming/MethodName: Use snake_case for method names.
def INVALID_CODE; end
^^^^^^^^^^^^
1 file inspected, 1 offense detected
RESULT
end
end
end
describe '#run with cops autocorrecting each-other' do
let(:source_file_path) { create_file('example.rb', source) }
let(:options) do
{
auto_correct: true,
formatters: [['progress', formatter_output_path]]
}
end
context 'with two conflicting cops' do
subject(:runner) do
runner_class = Class.new(RuboCop::Runner) do
def mobilized_cop_classes(_config)
RuboCop::Cop::Registry.new(
[
RuboCop::Cop::Test::ClassMustBeAModuleCop,
RuboCop::Cop::Test::ModuleMustBeAClassCop
]
)
end
end
runner_class.new(options, RuboCop::ConfigStore.new)
end
context 'if there is an offense in an inspected file' do
let(:source) { <<~RUBY }
# frozen_string_literal: true
class Klass
end
RUBY
it 'aborts because of an infinite loop' do
expect do
runner.run([])
end.to raise_error(
RuboCop::Runner::InfiniteCorrectionLoop,
"Infinite loop detected in #{source_file_path} and caused by " \
'Test/ClassMustBeAModuleCop -> Test/ModuleMustBeAClassCop'
)
end
end
context 'if there are multiple offenses in an inspected file' do
let(:source) { <<~RUBY }
# frozen_string_literal: true
class Klass
end
class AnotherKlass
end
RUBY
it 'aborts because of an infinite loop' do
expect do
runner.run([])
end.to raise_error(
RuboCop::Runner::InfiniteCorrectionLoop,
"Infinite loop detected in #{source_file_path} and caused by " \
'Test/ClassMustBeAModuleCop -> Test/ModuleMustBeAClassCop'
)
end
end
end
context 'with two pairs of conflicting cops' do
subject(:runner) do
runner_class = Class.new(RuboCop::Runner) do
def mobilized_cop_classes(_config)
RuboCop::Cop::Registry.new(
[
RuboCop::Cop::Test::ClassMustBeAModuleCop,
RuboCop::Cop::Test::ModuleMustBeAClassCop,
RuboCop::Cop::Test::AtoB,
RuboCop::Cop::Test::BtoA
]
)
end
end
runner_class.new(options, RuboCop::ConfigStore.new)
end
context 'if there is an offense in an inspected file' do
let(:source) { <<~RUBY }
# frozen_string_literal: true
class A_A
end
RUBY
it 'aborts because of an infinite loop' do
expect do
runner.run([])
end.to raise_error(
RuboCop::Runner::InfiniteCorrectionLoop,
"Infinite loop detected in #{source_file_path} and caused by " \
'Test/ClassMustBeAModuleCop, Test/AtoB ' \
'-> Test/ModuleMustBeAClassCop, Test/BtoA'
)
end
end
context 'with three cop cycle' do
subject(:runner) do
runner_class = Class.new(RuboCop::Runner) do
def mobilized_cop_classes(_config)
RuboCop::Cop::Registry.new(
[
RuboCop::Cop::Test::AtoB,
RuboCop::Cop::Test::BtoC,
RuboCop::Cop::Test::CtoA
]
)
end
end
runner_class.new(options, RuboCop::ConfigStore.new)
end
context 'if there is an offense in an inspected file' do
let(:source) { <<~RUBY }
# frozen_string_literal: true
class A
end
RUBY
it 'aborts because of an infinite loop' do
expect do
runner.run([])
end.to raise_error(
RuboCop::Runner::InfiniteCorrectionLoop,
"Infinite loop detected in #{source_file_path} and caused by " \
'Test/AtoB -> Test/BtoC -> Test/CtoA'
)
end
end
end
end
end
end