Skip to content

Commit

Permalink
[Fix #12052] Handle all glob special characters
Browse files Browse the repository at this point in the history
Co-Authored-By: Love Ottosson <love.ottosson@hemnet.se>
  • Loading branch information
meric426 and loveo committed Jul 17, 2023
1 parent a01e6f4 commit 7fcd290
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_nested_special_glob_characters.md
@@ -0,0 +1 @@
* [#12052](https://github.com/rubocop/rubocop/issues/12052): Fix "Subfolders can't include glob special characters". ([@meric426][], [@loveo][])
8 changes: 6 additions & 2 deletions lib/rubocop/target_finder.rb
Expand Up @@ -94,8 +94,12 @@ 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('/*/', '/\*/').gsub('/**/', '/\**/'), '*/'), flags)
# Escape glob characters in base_dir to avoid unwanted behavior.
base_dir = base_dir.gsub(/[\\\{\}\[\]\*\?]/) do |reserved_glob_character|
"\\#{reserved_glob_character}"
end

dirs = Dir.glob(File.join(base_dir, '*/'), flags)
.reject do |dir|
next true if dir.end_with?('/./', '/../')
next true if File.fnmatch?(exclude_pattern, dir, flags)
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/target_finder_spec.rb
Expand Up @@ -435,6 +435,32 @@
expect(found_basenames.include?('ruby4.rb')).to be(true)
end

it 'works also if a folder is named "{foo}"' do
create_empty_file('{foo}/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.include?('ruby1.rb')).to be(false)
expect(found_basenames.include?('ruby3.rb')).to be(true)
expect(found_basenames.include?('ruby4.rb')).to be(true)
end

it 'works also if a folder is named "[...something]"' do
create_empty_file('[...something]/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.include?('ruby1.rb')).to be(false)
expect(found_basenames.include?('ruby3.rb')).to be(true)
expect(found_basenames.include?('ruby4.rb')).to be(true)
end

it 'works if patterns are empty' do
allow(Dir).to receive(:glob).and_call_original
allow_any_instance_of(described_class).to receive(:wanted_dir_patterns).and_return([])
Expand Down

0 comments on commit 7fcd290

Please sign in to comment.