Skip to content

Commit

Permalink
Add mode parameter to TargetFinder#find()
Browse files Browse the repository at this point in the history
Add tests by duplicating.
  • Loading branch information
jonas054 committed May 8, 2020
1 parent c2f6f95 commit a09e9aa
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def warm_cache(target_files)

def find_target_files(paths)
target_finder = TargetFinder.new(@config_store, @options)
target_files = target_finder.find(paths)
target_files = target_finder.find(paths, :only_recognized_file_types)
target_files.each(&:freeze).freeze
end

Expand Down
10 changes: 6 additions & 4 deletions lib/rubocop/target_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def fail_fast?
# (if any). If args is empty, recursively find all Ruby source
# files under the current directory
# @return [Array] array of file paths
def find(args)
def find(args, mode)
return target_files_in_dir if args.empty?

files = []
Expand All @@ -36,7 +36,7 @@ def find(args)
files += if File.directory?(arg)
target_files_in_dir(arg.chomp(File::SEPARATOR))
else
process_explicit_path(arg)
process_explicit_path(arg, mode)
end
end

Expand Down Expand Up @@ -169,10 +169,12 @@ def included_file?(file)
ruby_file?(file) || configured_include?(file)
end

def process_explicit_path(path)
def process_explicit_path(path, mode)
files = path.include?('*') ? Dir[path] : [path]

files.select! { |file| included_file?(file) }
if mode == :only_recognized_file_types || force_exclusion?
files.select! { |file| included_file?(file) }
end

return files unless force_exclusion?

Expand Down
241 changes: 239 additions & 2 deletions spec/rubocop/target_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
create_empty_file('.hidden/ruby4.rb')
end

describe '#find' do
let(:found_files) { target_finder.find(args) }
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) { [] }

Expand Down Expand Up @@ -315,6 +315,243 @@
end
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) { [] }

it 'returns absolute paths' do
expect(found_files.empty?).to be(false)
found_files.each do |file|
expect(file.sub(/^[A-Z]:/, '')).to start_with('/')
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
RuboCop::PathUtil.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
RuboCop::PathUtil.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 a non-ruby file is passed' do
let(:args) { ['dir2/file'] }

it "doesn't pick the file" do
expect(found_basenames).to contain_exactly('file')
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 all the ruby files' do
expect(found_basenames)
.to eq(RUBY_EXTENSIONS.map { |ext| "file#{ext}" })
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

it 'picks all the ruby files' do
expect(found_basenames)
.to eq(RUBY_EXTENSIONS.map { |ext| "file#{ext}" })
end
end
end
end

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

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

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

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_files' do
let(:found_files) { target_finder.find_files(base_dir, flags) }
let(:found_basenames) { found_files.map { |f| File.basename(f) } }
Expand Down

0 comments on commit a09e9aa

Please sign in to comment.