Skip to content

Commit

Permalink
Merge pull request #12708 from jonas054/12695_fix_include_bug
Browse files Browse the repository at this point in the history
[Fix #12695] Handle Include from inherited file in parent directory
  • Loading branch information
koic committed Feb 25, 2024
2 parents 4c5fce1 + 36370f2 commit 32467d4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_config_include_bug.md
@@ -0,0 +1 @@
* [#12695](https://github.com/rubocop/rubocop/issues/12695): Fix bug in `Include` from inherited file in a parent directory. ([@jonas054][])
8 changes: 6 additions & 2 deletions lib/rubocop/path_util.rb
Expand Up @@ -44,14 +44,18 @@ def smart_path(path)
end
end

# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def match_path?(pattern, path)
case pattern
when String
matches =
if pattern == path
true
elsif glob?(pattern)
# File name matching doesn't really work with relative patterns the start with "..". We
# get around that problem by converting the pattern to an absolute path.
pattern = File.expand_path(pattern) if pattern.start_with?('..')

File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB)
end

Expand All @@ -66,7 +70,7 @@ def match_path?(pattern, path)
end
end
end
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# Returns true for an absolute Unix or Windows path.
def absolute?(path)
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cli_spec.rb
Expand Up @@ -1113,6 +1113,31 @@ def meow_at_4am?
abs('regexp')])
end

context 'when a .rubocop.yml is included from an ancestor directory' do
before do
create_file('child/grandkid/.rubocop.yml', <<~YAML)
inherit_from:
- ../../.rubocop.yml
YAML
end

context 'and it specifies an Include pattern' do
before do
create_file('.rubocop.yml', <<~YAML)
AllCops:
Include:
- "**/*.rbi"
YAML
end

it 'finds files included through inheritance' do
create_file('child/grandkid/file.rbi', 'x=0')
Dir.chdir('child/grandkid') { expect(cli.run(['-L'])).to eq(0) }
expect($stdout.string).to eq("file.rbi\n")
end
end
end

it 'ignores excluded files' do
create_file('example.rb', ['x = 0', 'puts x'])
create_file('regexp.rb', ['x = 0', 'puts x'])
Expand Down

0 comments on commit 32467d4

Please sign in to comment.