Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #9636] Resolve symlinks when excluding directories #9703

Merged
merged 1 commit into from Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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