Commit 89af9330 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Add & update specs for CODEOWNER parsing

The patterns are now built differently so the specs are updated to
reflect that.

Added a spec to make sure we're not accidentally matching partial
folder names.
parent 9c7e56c3
...@@ -73,11 +73,10 @@ module Gitlab ...@@ -73,11 +73,10 @@ module Gitlab
end end
def path_matches?(pattern, path) def path_matches?(pattern, path)
# `FNM_DOTMATCH` makes sure we also match files starting with a `.`
# `FNM_PATHNAME` makes sure ** matches path separators
flags = ::File::FNM_DOTMATCH | ::File::FNM_PATHNAME flags = ::File::FNM_DOTMATCH | ::File::FNM_PATHNAME
# Then the pattern ends in a wildcard, we only want to go one level deep
# setting `::File::FNM_PATHNAME` makes the `*` not match directory
# separators
::File.fnmatch?(pattern, path, flags) ::File.fnmatch?(pattern, path, flags)
end end
end end
......
...@@ -17,15 +17,15 @@ describe Gitlab::CodeOwners::File do ...@@ -17,15 +17,15 @@ describe Gitlab::CodeOwners::File do
describe '#parsed_data' do describe '#parsed_data' do
it 'parses all the required lines' do it 'parses all the required lines' do
expected_patterns = [ expected_patterns = [
'*', '**#file_with_pound.rb', '*.rb', '**CODEOWNERS', '**LICENSE', 'docs/', '/**/*', '/**/#file_with_pound.rb', '/**/*.rb', '/**/CODEOWNERS', '/**/LICENSE', '/docs/**/*',
'docs/*', 'config/', '**lib/', '**path with spaces/' '/docs/*', '/config/**/*', '/**/lib/**/*', '/**/path with spaces/**/*'
] ]
expect(file.parsed_data.keys) expect(file.parsed_data.keys)
.to contain_exactly(*expected_patterns) .to contain_exactly(*expected_patterns)
end end
it 'allows usernames and emails' do it 'allows usernames and emails' do
expect(file.parsed_data['**LICENSE']).to include('legal', 'janedoe@gitlab.com') expect(file.parsed_data['/**/LICENSE']).to include('legal', 'janedoe@gitlab.com')
end end
context 'when there are entries that do not look like user references' do context 'when there are entries that do not look like user references' do
...@@ -34,7 +34,7 @@ describe Gitlab::CodeOwners::File do ...@@ -34,7 +34,7 @@ describe Gitlab::CodeOwners::File do
end end
it 'ignores the entries' do it 'ignores the entries' do
expect(file.parsed_data['**a-path/']).to include('username', 'email@gitlab.org') expect(file.parsed_data['/**/a-path/**/*']).to include('username', 'email@gitlab.org')
end end
end end
end end
...@@ -178,5 +178,22 @@ describe Gitlab::CodeOwners::File do ...@@ -178,5 +178,22 @@ describe Gitlab::CodeOwners::File do
expect(owners).to be_nil expect(owners).to be_nil
end end
end end
context 'partial matches' do
let(:file_content) do
'foo/* @user-1 @user-2'
end
it 'does not match a file in a folder that looks the same' do
owners = file.owners_for_path('fufoo/bar')
expect(owners).to be_nil
end
it 'matches the file in any folder' do
expect(file.owners_for_path('baz/foo/bar')).to include('user-1', 'user-2')
expect(file.owners_for_path('/foo/bar')).to include('user-1', 'user-2')
end
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment