Skip to content

Commit

Permalink
Fix glob matching of "{,xyz}" matching empty string (#10716)
Browse files Browse the repository at this point in the history
Add logic allowing a group matcher to match empty strings
and associated unit test.
  • Loading branch information
andy31415 authored Oct 20, 2021
1 parent d47baf5 commit 91cf677
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scripts/build/glob_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def _GlobMatch(glob: str, value: str) -> bool:
return False
glob, value = glob[1:], value[1:]

# if value is empty it has a chance to match subgroups
if not value and glob.startswith('{') and glob.endswith('}'):
for choice in glob[1: -1].split(','):
if _GlobMatch(choice, value):
return True

return glob == '*' or (not glob and not value)


Expand Down
17 changes: 17 additions & 0 deletions scripts/build/test_glob_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ def test_group(self):
self.assertFalse(GlobMatcher('{a,b}x{c,d}').matches('axe'))
self.assertFalse(GlobMatcher('{a,b}x{c,d}').matches('exd'))

def test_combined(self):
self.assertTrue(GlobMatcher('a{,bc}').matches('a'))
self.assertTrue(GlobMatcher('a{,bc}').matches('abc'))
self.assertTrue(GlobMatcher('ab{c*d,ef}xz').matches('abcdxz'))
self.assertTrue(GlobMatcher('ab{c*d,ef}xz').matches('abc1234dxz'))
self.assertTrue(GlobMatcher('ab{c*d,ef}xz').matches('abefxz'))

self.assertFalse(GlobMatcher('a{,bc}').matches('ab'))
self.assertFalse(GlobMatcher('a{,bc}').matches('ax'))
self.assertFalse(GlobMatcher('a{,bc}').matches('abcd'))
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abxz'))
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abcxz'))
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abdxz'))
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abxz'))
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abexz'))
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abfxz'))


if __name__ == '__main__':
unittest.main()

0 comments on commit 91cf677

Please sign in to comment.