forked from rubocop/rubocop
/
target_finder_spec.rb
executable file
·577 lines (482 loc) · 16.8 KB
/
target_finder_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
# frozen_string_literal: true
RSpec.describe RuboCop::TargetFinder, :isolated_environment do
include FileHelper
ruby_extensions = %w[.rb
.arb
.axlsx
.builder
.fcgi
.gemfile
.gemspec
.god
.jb
.jbuilder
.mspec
.opal
.pluginspec
.podspec
.rabl
.rake
.rbuild
.rbw
.rbx
.ru
.ruby
.spec
.thor
.watchr]
ruby_interpreters = %w[ruby
macruby
rake
jruby
rbx]
ruby_filenames = %w[.irbrc
.pryrc
.simplecov
Appraisals
Berksfile
Brewfile
Buildfile
Capfile
Cheffile
Dangerfile
Deliverfile
Fastfile
Gemfile
Guardfile
Jarfile
Mavenfile
Podfile
Puppetfile
Rakefile
rakefile
Snapfile
Steepfile
Thorfile
Vagabondfile
Vagrantfile
buildfile]
subject(:target_finder) do
described_class.new(config_store, options)
end
let(:config_store) { RuboCop::ConfigStore.new }
let(:options) { { force_exclusion: force_exclusion, debug: debug } }
let(:force_exclusion) { false }
let(:debug) { false }
before do
create_empty_file('dir1/ruby1.rb')
create_empty_file('dir1/ruby2.rb')
create_empty_file('dir1/file.txt')
create_empty_file('dir1/file')
create_file('dir1/executable', '#!/usr/bin/env ruby')
create_empty_file('dir2/ruby3.rb')
create_empty_file('.hidden/ruby4.rb')
end
shared_examples 'common behavior for #find' do
context 'when a file with a ruby filename is passed' do
let(:args) { ruby_filenames.map { |name| "dir2/#{name}" } }
it 'picks all the ruby files' do
expect(found_basenames).to eq(ruby_filenames)
end
end
context 'when files with ruby interpreters are passed' do
let(:args) { ruby_interpreters.map { |name| "dir2/#{name}" } }
before do
ruby_interpreters.each do |interpreter|
create_file("dir2/#{interpreter}", "#!/usr/bin/#{interpreter}")
end
end
it 'picks all the ruby files' do
expect(found_basenames).to eq(ruby_interpreters)
end
end
context 'when a pattern is passed' do
let(:args) { ['dir1/*2.rb'] }
it 'finds files which match the pattern' do
expect(found_basenames).to eq(['ruby2.rb'])
end
end
context 'when same paths are passed' do
let(:args) { %w[dir1 dir1] }
it 'does not return duplicated file paths' do
count = found_basenames.count { |f| f == 'ruby1.rb' }
expect(count).to eq(1)
end
end
context 'when some paths are specified in the configuration Exclude ' \
'and they are explicitly passed as arguments' do
before do
create_file('.rubocop.yml', <<~YAML)
AllCops:
Exclude:
- dir1/ruby1.rb
- 'dir2/*'
YAML
create_file('dir1/.rubocop.yml', <<~YAML)
AllCops:
Exclude:
- executable
YAML
end
let(:args) do
['dir1/ruby1.rb', 'dir1/ruby2.rb', 'dir1/exe*', 'dir2/ruby3.rb']
end
context 'normally' do
it 'does not exclude them' do
expect(found_basenames)
.to eq(['ruby1.rb', 'ruby2.rb', 'executable', 'ruby3.rb'])
end
end
context "when it's forced to adhere file exclusion configuration" do
let(:force_exclusion) { true }
it 'excludes them' do
expect(found_basenames).to eq(['ruby2.rb'])
end
end
end
it 'returns absolute paths' do
expect(found_files.empty?).to be(false)
found_files.each do |file|
expect(Pathname.new(file).absolute?).to be(true)
end
end
it 'does not find hidden files' do
expect(found_files).not_to include('.hidden/ruby4.rb')
end
context 'when no argument is passed' do
let(:args) { [] }
it 'finds files under the current directory' do
Dir.chdir('dir1') do
expect(found_files.empty?).to be(false)
found_files.each do |file|
expect(file).to include('/dir1/')
expect(file).not_to include('/dir2/')
end
end
end
end
context 'when a directory path is passed' do
let(:args) { ['../dir2'] }
it 'finds files under the specified directory' do
Dir.chdir('dir1') do
expect(found_files.empty?).to be(false)
found_files.each do |file|
expect(file).to include('/dir2/')
expect(file).not_to include('/dir1/')
end
end
end
end
context 'when a hidden directory path is passed' do
let(:args) { ['.hidden'] }
it 'finds files under the specified directory' do
expect(found_files.size).to be(1)
expect(found_files.first).to include('.hidden/ruby4.rb')
end
end
context 'when some non-known Ruby files are specified in the ' \
'configuration Include and they are explicitly passed ' \
'as arguments' do
before do
create_file('.rubocop.yml', <<~YAML)
AllCops:
Include:
- dir1/file
YAML
end
let(:args) do
['dir1/file']
end
it 'includes them' do
expect(found_basenames)
.to contain_exactly('file')
end
end
end
shared_examples 'when input is passed on stdin' do
context 'when input is passed on stdin' do
let(:options) do
{
force_exclusion: force_exclusion,
debug: debug,
stdin: 'def example; end'
}
end
let(:args) { ['Untitled'] }
it 'includes the file' do
expect(found_basenames).to eq(['Untitled'])
end
end
end
describe '#find(..., :only_recognized_file_types)' do
let(:found_files) { target_finder.find(args, :only_recognized_file_types) }
let(:found_basenames) { found_files.map { |f| File.basename(f) } }
let(:args) { [] }
context 'when a hidden directory path is passed' do
let(:args) { ['.hidden'] }
it 'finds files under the specified directory' do
expect(found_files.size).to be(1)
expect(found_files.first).to include('.hidden/ruby4.rb')
end
end
context 'when a non-ruby file is passed' do
let(:args) { ['dir2/file'] }
it "doesn't pick the file" do
expect(found_basenames.empty?).to be(true)
end
end
context 'when files with a ruby extension are passed' do
let(:args) { ruby_extensions.map { |ext| "dir2/file#{ext}" } }
it 'picks all the ruby files' do
expect(found_basenames)
.to eq(ruby_extensions.map { |ext| "file#{ext}" })
end
context 'when local AllCops/Include lists two patterns' do
before do
create_file('.rubocop.yml', <<-YAML)
AllCops:
Include:
- '**/*.rb'
- '**/*.arb'
YAML
end
it 'picks two files' do
expect(found_basenames).to eq(%w[file.rb file.arb])
end
context 'when a subdirectory AllCops/Include only lists one pattern' do
before do
create_file('dir2/.rubocop.yml', <<-YAML)
AllCops:
Include:
- '**/*.ruby'
YAML
end
# Include and Exclude patterns are take from the top directory and
# settings in subdirectories are silently ignored.
it 'picks two files' do
expect(found_basenames).to eq(%w[file.rb file.arb])
end
end
end
end
include_examples 'common behavior for #find'
context 'when some non-known Ruby files are specified in the ' \
'configuration Include and they are not explicitly passed ' \
'as arguments' do
before do
create_file('.rubocop.yml', <<~YAML)
AllCops:
Include:
- '**/*.rb'
- dir1/file
YAML
end
let(:args) do
['dir1/**/*']
end
it 'includes them' do
expect(found_basenames)
.to contain_exactly('executable', 'file', 'ruby1.rb', 'ruby2.rb')
end
end
include_examples 'when input is passed on stdin'
end
describe '#find(..., :all_file_types)' do
let(:found_files) { target_finder.find(args, :all_file_types) }
let(:found_basenames) { found_files.map { |f| File.basename(f) } }
let(:args) { [] }
include_examples 'common behavior for #find'
context 'when a non-ruby file is passed' do
let(:args) { ['dir2/file'] }
it 'picks the file' do
expect(found_basenames).to contain_exactly('file')
end
end
context 'when files with a ruby extension are passed' do
shared_examples 'picks all the ruby files' do
it 'picks all the ruby files' do
expect(found_basenames)
.to eq(ruby_extensions.map { |ext| "file#{ext}" })
end
end
let(:args) { ruby_extensions.map { |ext| "dir2/file#{ext}" } }
include_examples 'picks all the ruby files'
context 'when local AllCops/Include lists two patterns' do
before do
create_file('.rubocop.yml', <<-YAML)
AllCops:
Include:
- '**/*.rb'
- '**/*.arb'
YAML
end
include_examples 'picks all the ruby files'
context 'when a subdirectory AllCops/Include only lists one pattern' do
before do
create_file('dir2/.rubocop.yml', <<-YAML)
AllCops:
Include:
- '**/*.ruby'
YAML
end
include_examples 'picks all the ruby files'
end
end
end
context 'when some non-known Ruby files are specified in the ' \
'configuration Include and they are not explicitly passed ' \
'as arguments' do
before do
create_file('.rubocop.yml', <<~YAML)
AllCops:
Include:
- '**/*.rb'
- dir1/file
YAML
end
let(:args) do
['dir1/**/*']
end
it 'includes them' do
expect(found_basenames).to contain_exactly('executable', 'file',
'file.txt', 'ruby1.rb',
'ruby2.rb')
end
end
include_examples 'when input is passed on stdin'
end
describe '#find_files' do
let(:found_files) { target_finder.find_files(base_dir, flags) }
let(:found_basenames) { found_files.map { |f| File.basename(f) } }
let(:base_dir) { Dir.pwd }
let(:flags) { 0 }
it 'does not search excluded top level directories' do
config = instance_double(RuboCop::Config)
exclude_property = { 'Exclude' => [File.expand_path('dir1/**/*')] }
allow(config).to receive(:for_all_cops).and_return(exclude_property)
allow(config_store).to receive(:for).and_return(config)
expect(found_basenames).not_to include('ruby1.rb')
expect(found_basenames).to include('ruby3.rb')
end
it 'works also if a folder is named ","' do
create_empty_file(',/ruby4.rb')
config = instance_double(RuboCop::Config)
exclude_property = { 'Exclude' => [File.expand_path('dir1/**/*')] }
allow(config).to receive(:for_all_cops).and_return(exclude_property)
allow(config_store).to receive(:for).and_return(config)
expect(found_basenames).not_to include('ruby1.rb')
expect(found_basenames).to include('ruby3.rb')
expect(found_basenames).to include('ruby4.rb')
end
it 'works also if a folder is named "{}"' do
create_empty_file('{}/ruby4.rb')
config = instance_double(RuboCop::Config)
exclude_property = { 'Exclude' => [File.expand_path('dir1/**/*')] }
allow(config).to receive(:for_all_cops).and_return(exclude_property)
allow(config_store).to receive(:for).and_return(config)
expect(found_basenames).not_to include('ruby1.rb')
expect(found_basenames).to include('ruby3.rb')
expect(found_basenames).to include('ruby4.rb')
end
# Cannot create a directory with containing `*` character on Windows.
# https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
unless RuboCop::Platform.windows?
it 'works also if a folder is named "**"' do
create_empty_file('**/ruby5.rb')
config = instance_double(RuboCop::Config)
exclude_property = { 'Exclude' => [File.expand_path('dir1/**/*')] }
allow(config).to receive(:for_all_cops).and_return(exclude_property)
allow(config_store).to receive(:for).and_return(config)
expect(found_basenames).to include('ruby5.rb')
end
end
end
describe '#target_files_in_dir' do
let(:found_files) { target_finder.target_files_in_dir(base_dir) }
let(:found_basenames) { found_files.map { |f| File.basename(f) } }
let(:base_dir) { '.' }
it 'picks files with extension .rb' do
rb_file_count = found_files.count { |f| f.end_with?('.rb') }
expect(rb_file_count).to eq(3)
end
it 'picks ruby executable files with no extension' do
expect(found_basenames).to include('executable')
end
it 'does not pick files with no extension and no ruby shebang' do
expect(found_basenames).not_to include('file')
end
it 'does not pick directories' do
found_basenames = found_files.map { |f| File.basename(f) }
allow(config_store).to receive(:for).and_return({})
expect(found_basenames).not_to include('dir1')
end
it 'picks files specified to be included in config' do
config = instance_double(RuboCop::Config)
allow(config).to receive(:file_to_include?) do |file|
File.basename(file) == 'file'
end
allow(config)
.to receive(:for_all_cops).and_return('Exclude' => [],
'Include' => [],
'RubyInterpreters' => [])
allow(config).to receive(:[]).and_return([])
allow(config).to receive(:file_to_exclude?).and_return(false)
allow(config_store).to receive(:for).and_return(config)
expect(found_basenames).to include('file')
end
it 'does not pick files specified to be excluded in config' do
config = instance_double(RuboCop::Config).as_null_object
allow(config)
.to receive(:for_all_cops).and_return('Exclude' => [],
'Include' => [],
'RubyInterpreters' => [])
allow(config).to receive(:file_to_include?).and_return(false)
allow(config).to receive(:file_to_exclude?) do |file|
File.basename(file) == 'ruby2.rb'
end
allow(config_store).to receive(:for).and_return(config)
expect(found_basenames).not_to include('ruby2.rb')
end
context 'when an exception is raised while reading file' do
around do |example|
original_stderr = $stderr
$stderr = StringIO.new
begin
example.run
ensure
$stderr = original_stderr
end
end
before do
allow_any_instance_of(File).to receive(:readline).and_raise(EOFError)
end
context 'and debug mode is enabled' do
let(:debug) { true }
it 'outputs error message' do
found_files
expect($stderr.string).to include('Unprocessable file')
end
end
context 'and debug mode is disabled' do
let(:debug) { false }
it 'outputs nothing' do
found_files
expect($stderr.string.empty?).to be(true)
end
end
end
context 'w/ --fail-fast option' do
let(:options) do
{
force_exclusion: force_exclusion,
debug: debug,
fail_fast: true
}
end
it 'works' do
rb_file_count = found_files.count { |f| f.end_with?('.rb') }
expect(rb_file_count).to eq(3)
end
end
end
end