Skip to content

Commit

Permalink
Fix validate --diff when multiple owned globs are present (#101)
Browse files Browse the repository at this point in the history
* Add spec that demonstrates the issue

* Presence of file in any owned glob is good enough
  • Loading branch information
nvengal authored Jun 24, 2024
1 parent 53eb92b commit fd21e7d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/code_ownership/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def self.file_tracked?(file)
# However, globbing out can take 5 or more seconds on a large repository, dramatically slowing down
# invocations to `bin/codeownership validate --diff`.
# Using `File.fnmatch?` is a lot faster!
in_owned_globs = configuration.owned_globs.all? do |owned_glob|
in_owned_globs = configuration.owned_globs.any? do |owned_glob|
File.fnmatch?(owned_glob, file, File::FNM_PATHNAME | File::FNM_EXTGLOB)
end

Expand Down
19 changes: 18 additions & 1 deletion spec/lib/code_ownership/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

describe 'validate' do
let(:argv) { ['validate'] }
let(:owned_globs) { nil }

before do
write_configuration
write_configuration(owned_globs: owned_globs)
write_file('app/services/my_file.rb')
write_file('frontend/javascripts/my_file.jsx')
end
Expand All @@ -21,6 +22,22 @@
end
end

context 'with --diff argument' do
let(:argv) { ['validate', '--diff'] }

before do
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with('CODEOWNERS_GIT_STAGED_FILES').and_return('app/services/my_file.rb')
end

context 'when there are multiple owned_globs' do
let(:owned_globs) { ['app/*/**', 'lib/*/**'] }

it 'validates the tracked file' do
expect { subject }.to raise_error CodeOwnership::InvalidCodeOwnershipConfigurationError
end
end
end
end

describe 'for_file' do
Expand Down

0 comments on commit fd21e7d

Please sign in to comment.