Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for consecutive asterisks at the beginning or end #20

Merged
merged 4 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/PatternMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ private function isMatch(Pattern $pattern, string $filename): bool
'*' => '[^\/]+',
'?' => '[^\/]',
'**' => '.*',
'/**' => '.*',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'/**' => '.*',
'/**' => '\/.*',

I think we do need to add the slashes. I have created a pull request on a test repository to verify some edge cases. I have added a coworker as codeowner to that repository: https://github.com/timoschinkel/test-repository/pull/6/files

For the rule docs/** that does apply to docs/file.md and docs/folder/file.md, but not to docs.md.

These situations can be tested by adding the following lines to \CodeOwners\Tests\PatternMatcherTest::provideNoMatchFoundExceptionIsThrownForFilename():

            // **
            [new Pattern('**/foo', ['@owner']), 'foo.ext'],
            [new Pattern('foo/**', ['@owner']), 'foo.ext'],

'**/' => '.*',
martinssipenko marked this conversation as resolved.
Show resolved Hide resolved
'/**/' => '\/([^\/]+\/)*',
];

Expand Down
16 changes: 16 additions & 0 deletions tests/Fixtures/CODEOWNERS.example
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ apps/ @octocat
# directory in the root of your repository.
/docs/ @doctocat

# A leading "**" followed by a slash means match in all directories.
# For example, "**/foo" matches file or directory "foo" anywhere,
# the same as pattern "foo". "**/foo/bar" matches file or directory
# "bar" anywhere that is directly under directory "foo".
**/foo @doctocat

# A trailing "/**" matches everything inside.
# For example, "abc/**" matches all files inside directory "abc"
# with infinite depth.
abc/** @doctocat

# A slash followed by two consecutive asterisks then a slash ma
# zero or more directories. For example, "a/**/b" matches "a/b",
# "a/x/b", "a/x/y/b" and so on.
a/**/b @doctocat

# In this example nobody owns the files
# This line should be ignored by the parser.
/src
6 changes: 6 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public function testParsingResultsInPatterns()
new Pattern('docs/*', ['[email protected]']),
new Pattern('apps/', ['@octocat']),
new Pattern('/docs/', ['@doctocat']),
new Pattern('**/foo', ['@doctocat']),
new Pattern('abc/**', ['@doctocat']),
new Pattern('a/**/b', ['@doctocat']),
], $patterns);
}

Expand All @@ -66,6 +69,9 @@ public function testParsingStringResultsInPatterns()
new Pattern('docs/*', ['[email protected]']),
new Pattern('apps/', ['@octocat']),
new Pattern('/docs/', ['@doctocat']),
new Pattern('**/foo', ['@doctocat']),
new Pattern('abc/**', ['@doctocat']),
new Pattern('a/**/b', ['@doctocat']),
], $patterns);
}
}
5 changes: 5 additions & 0 deletions tests/PatternMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public function provideCorrectMatchIsReturnedForFilename(): array
// does NOT match "foo/bar/file.ext"

// **
[new Pattern('**/c', ['@owner']), 'a/c'],
[new Pattern('**/c', ['@owner']), 'b/c'],
[new Pattern('**/c', ['@owner']), 'a/b/c'],
[new Pattern('a/**', ['@owner']), 'a/b'],
[new Pattern('a/**', ['@owner']), 'a/b/c'],
[new Pattern('a/**/b', ['@owner']), 'a/b'],
[new Pattern('a/**/b', ['@owner']), 'a/x/b'],
[new Pattern('a/**/b', ['@owner']), 'a/x/y/b'],
Expand Down