From 027655dc6cfe001be7d6fc97a9adb22a59f4239b Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 12 Mar 2021 09:46:55 +0900 Subject: [PATCH] [Fix #9593] Fix an error when processing a directory is named `{}` Fixes #9593. This PR fixes the following error when processing a directory is named `{}`. ```console % mkdir {} % rubocop (snip) File name too long - /tmp//////////////////////////////////////////////... ``` --- ...ror_when_processing_dir_is_named_empty_braces.md | 1 + lib/rubocop/target_finder.rb | 1 + spec/rubocop/target_finder_spec.rb | 13 +++++++++++++ 3 files changed, 15 insertions(+) create mode 100644 changelog/fix_an_error_when_processing_dir_is_named_empty_braces.md diff --git a/changelog/fix_an_error_when_processing_dir_is_named_empty_braces.md b/changelog/fix_an_error_when_processing_dir_is_named_empty_braces.md new file mode 100644 index 00000000000..cb264e677e4 --- /dev/null +++ b/changelog/fix_an_error_when_processing_dir_is_named_empty_braces.md @@ -0,0 +1 @@ +* [#9593](https://github.com/rubocop/rubocop/issues/9593): Fix an error when processing a directory is named `{}`. ([@koic][]) diff --git a/lib/rubocop/target_finder.rb b/lib/rubocop/target_finder.rb index 1f1899b731f..9522db9b43a 100644 --- a/lib/rubocop/target_finder.rb +++ b/lib/rubocop/target_finder.rb @@ -96,6 +96,7 @@ def find_files(base_dir, flags) end 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) diff --git a/spec/rubocop/target_finder_spec.rb b/spec/rubocop/target_finder_spec.rb index 46e19886595..397e4ce8b74 100755 --- a/spec/rubocop/target_finder_spec.rb +++ b/spec/rubocop/target_finder_spec.rb @@ -441,6 +441,19 @@ 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?