Skip to content

Commit

Permalink
[Fix rubocop#9636] Resolve symlinks when excluding directories
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Apr 19, 2021
1 parent b4e6aaa commit cdb9df2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5555,3 +5555,4 @@
[@kachick]: https://github.com/kachick
[@corroded]: https://github.com/corroded
[@osyo-manga]: https://github.com/osyo-manga
[@ob-stripe]: https://github.com/ob-stripe
@@ -0,0 +1 @@
* [#9636](https://github.com/rubocop/rubocop/issues/9636): Resolve symlinks when excluding directories. ([@ob-stripe][])
6 changes: 5 additions & 1 deletion lib/rubocop/target_finder.rb
Expand Up @@ -97,7 +97,11 @@ def wanted_dir_patterns(base_dir, exclude_pattern, flags)
base_dir = base_dir.gsub('/{}/', '/\{}/')
dirs = Dir.glob(File.join(base_dir.gsub('/**/', '/\**/'), '*/'), flags)
.reject do |dir|
dir.end_with?('/./', '/../') || File.fnmatch?(exclude_pattern, dir, flags)
next true if dir.end_with?('/./', '/../')
next true if File.fnmatch?(exclude_pattern, dir, flags)

File.symlink?(dir.chomp('/')) && File.fnmatch?(exclude_pattern,
"#{File.realpath(dir)}/", flags)
end
dirs.flat_map { |dir| wanted_dir_patterns(dir, exclude_pattern, flags) }.unshift(base_dir)
end
Expand Down
27 changes: 27 additions & 0 deletions spec/rubocop/target_finder_spec.rb
Expand Up @@ -447,6 +447,33 @@
expect(found_basenames).to include('ruby5.rb')
end
end

it 'resolves symlinks when looking for excluded directories' do
create_link('link', 'dir1')

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 'can exclude symlinks as well as directories' do
Dir.mktmpdir do |tmpdir|
create_empty_file(File.join(tmpdir, 'ruby5.rb'))
create_link('link', tmpdir)

config = instance_double(RuboCop::Config)
exclude_property = { 'Exclude' => [File.expand_path('link/**/*')] }
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('ruby5.rb')
expect(found_basenames).to include('ruby3.rb')
end
end
end

describe '#target_files_in_dir' do
Expand Down
9 changes: 9 additions & 0 deletions spec/support/file_helper.rb
Expand Up @@ -24,4 +24,13 @@ def create_file(file_path, content)
def create_empty_file(file_path)
create_file(file_path, '')
end

def create_link(link_path, target_path)
link_path = File.expand_path(link_path)

dir_path = File.dirname(link_path)
FileUtils.makedirs dir_path unless File.exist?(dir_path)

FileUtils.symlink(target_path, link_path)
end
end

0 comments on commit cdb9df2

Please sign in to comment.