From 1234891080b6cbb5d4af5b27a5678ef627f5e928 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Wed, 20 Oct 2021 17:21:54 -0400 Subject: [PATCH] Fix glob matching of "{,xyz}" matching empty string (#10716) Add logic allowing a group matcher to match empty strings and associated unit test. --- scripts/build/glob_matcher.py | 6 ++++++ scripts/build/test_glob_matcher.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/scripts/build/glob_matcher.py b/scripts/build/glob_matcher.py index cabd07fcee5a3a..7bef6f190dac54 100644 --- a/scripts/build/glob_matcher.py +++ b/scripts/build/glob_matcher.py @@ -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) diff --git a/scripts/build/test_glob_matcher.py b/scripts/build/test_glob_matcher.py index 3041059585fea9..e175ff4347d9bb 100644 --- a/scripts/build/test_glob_matcher.py +++ b/scripts/build/test_glob_matcher.py @@ -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()