From fe934c2f006043cd1657d80fd81100ffdff758b4 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Fri, 16 Apr 2021 22:43:57 -0700 Subject: [PATCH] [Fix #9636] Resolve symlinks when excluding directories --- CHANGELOG.md | 1 + ...lve_symlinks_when_excluding_directories.md | 1 + lib/rubocop/target_finder.rb | 6 ++++- spec/rubocop/target_finder_spec.rb | 27 +++++++++++++++++++ spec/support/file_helper.rb | 9 +++++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_resolve_symlinks_when_excluding_directories.md diff --git a/CHANGELOG.md b/CHANGELOG.md index a3d6cfaece3..1f3c90e789a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5559,3 +5559,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 diff --git a/changelog/fix_resolve_symlinks_when_excluding_directories.md b/changelog/fix_resolve_symlinks_when_excluding_directories.md new file mode 100644 index 00000000000..5843dc045d5 --- /dev/null +++ b/changelog/fix_resolve_symlinks_when_excluding_directories.md @@ -0,0 +1 @@ +* [#9636](https://github.com/rubocop/rubocop/issues/9636): Resolve symlinks when excluding directories. ([@ob-stripe][]) diff --git a/lib/rubocop/target_finder.rb b/lib/rubocop/target_finder.rb index a643442f341..2e251abb9c1 100644 --- a/lib/rubocop/target_finder.rb +++ b/lib/rubocop/target_finder.rb @@ -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 diff --git a/spec/rubocop/target_finder_spec.rb b/spec/rubocop/target_finder_spec.rb index 449612bf3e5..b1b9ca43d81 100755 --- a/spec/rubocop/target_finder_spec.rb +++ b/spec/rubocop/target_finder_spec.rb @@ -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 diff --git a/spec/support/file_helper.rb b/spec/support/file_helper.rb index b5418cfb737..d30f417c750 100644 --- a/spec/support/file_helper.rb +++ b/spec/support/file_helper.rb @@ -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