Skip to content

Commit 1234891

Browse files
andy31415pull[bot]
authored andcommitted
Fix glob matching of "{,xyz}" matching empty string (#10716)
Add logic allowing a group matcher to match empty strings and associated unit test.
1 parent abd5b8e commit 1234891

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

scripts/build/glob_matcher.py

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ def _GlobMatch(glob: str, value: str) -> bool:
5252
return False
5353
glob, value = glob[1:], value[1:]
5454

55+
# if value is empty it has a chance to match subgroups
56+
if not value and glob.startswith('{') and glob.endswith('}'):
57+
for choice in glob[1: -1].split(','):
58+
if _GlobMatch(choice, value):
59+
return True
60+
5561
return glob == '*' or (not glob and not value)
5662

5763

scripts/build/test_glob_matcher.py

+17
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ def test_group(self):
7979
self.assertFalse(GlobMatcher('{a,b}x{c,d}').matches('axe'))
8080
self.assertFalse(GlobMatcher('{a,b}x{c,d}').matches('exd'))
8181

82+
def test_combined(self):
83+
self.assertTrue(GlobMatcher('a{,bc}').matches('a'))
84+
self.assertTrue(GlobMatcher('a{,bc}').matches('abc'))
85+
self.assertTrue(GlobMatcher('ab{c*d,ef}xz').matches('abcdxz'))
86+
self.assertTrue(GlobMatcher('ab{c*d,ef}xz').matches('abc1234dxz'))
87+
self.assertTrue(GlobMatcher('ab{c*d,ef}xz').matches('abefxz'))
88+
89+
self.assertFalse(GlobMatcher('a{,bc}').matches('ab'))
90+
self.assertFalse(GlobMatcher('a{,bc}').matches('ax'))
91+
self.assertFalse(GlobMatcher('a{,bc}').matches('abcd'))
92+
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abxz'))
93+
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abcxz'))
94+
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abdxz'))
95+
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abxz'))
96+
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abexz'))
97+
self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abfxz'))
98+
8299

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

0 commit comments

Comments
 (0)