Skip to content

Commit 0c8d185

Browse files
committed
matchglob: Fix edge case with leading ** and multiple trailing **
1 parent ce5861e commit 0c8d185

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

domdf_python_tools/paths.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ def traverse_to_file(base_directory: _P, *filename: PathLike, height: int = -1)
863863
raise FileNotFoundError(f"'{filename[0]!s}' not found in {base_directory}")
864864

865865

866-
def matchglob(filename: PathLike, pattern):
866+
def matchglob(filename: PathLike, pattern: str):
867867
"""
868868
Given a filename and a glob pattern, return whether the filename matches the glob.
869869
@@ -899,12 +899,19 @@ def matchglob(filename: PathLike, pattern):
899899
filename_part = filename_parts.popleft()
900900

901901
if pattern_part == "**":
902-
if not pattern_parts or not filename_parts:
902+
if not pattern_parts:
903903
return True
904904

905905
while pattern_part == "**":
906+
if not pattern_parts:
907+
return True
908+
906909
pattern_part = pattern_parts.popleft()
907910

911+
if pattern_parts and not filename_parts:
912+
# Filename must match everything after **
913+
return False
914+
908915
if fnmatch.fnmatchcase(filename_part, pattern_part):
909916
continue
910917
else:

tests/test_paths.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,17 @@ def test_iterchildren_no_exclusions(tmp_pathplus: PathPlus):
801801
True
802802
),
803803
("domdf_python_tools/**/*.py", "domdf_python_tools/domdf_python_tools/pagesizes/units.py", True),
804+
("**/*.py", ".pre-commit-config.yaml", False),
805+
("**/*.yaml", ".pre-commit-config.yaml", True),
806+
("./**/*.py", ".pre-commit-config.yaml", False),
807+
("./**/*.yaml", ".pre-commit-config.yaml", True),
808+
("foo/**/**/bar.py", "foo/bar.py", True),
809+
("foo/**/**/bar.py", "foo/baz/bar.py", True),
810+
("foo/**/**/bar.py", "foo/baz/baz/bar.py", True),
811+
("foo/**/**", "foo/", True),
812+
("foo/**/**", "foo/bar.py", True),
813+
("foo/**/**", "foo/baz/bar.py", True),
814+
("foo/**/**", "foo/baz/baz/bar.py", True),
804815
]
805816
)
806817
def test_globpath(pattern: str, filename: str, match: bool):

0 commit comments

Comments
 (0)