diff --git a/newsfragments/121.bugfix.rst b/newsfragments/121.bugfix.rst new file mode 100644 index 0000000..3ad6d13 --- /dev/null +++ b/newsfragments/121.bugfix.rst @@ -0,0 +1 @@ +Also match directories in Path.glob. \ No newline at end of file diff --git a/tests/test_path.py b/tests/test_path.py index 6bb59a6..a0a58f6 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -469,6 +469,18 @@ def test_glob_recursive(self, alpharep): assert list(root.glob("**/*.txt")) == list(root.rglob("*.txt")) + @pass_alpharep + def test_glob_dirs(self, alpharep): + root = zipfile.Path(alpharep) + assert list(root.glob('b')) == [zipfile.Path(alpharep, "b/")] + assert list(root.glob('b*')) == [zipfile.Path(alpharep, "b/")] + + @pass_alpharep + def test_glob_subdir(self, alpharep): + root = zipfile.Path(alpharep) + assert list(root.glob('g/h')) == [zipfile.Path(alpharep, "g/h/")] + assert list(root.glob('g*/h*')) == [zipfile.Path(alpharep, "g/h/")] + @pass_alpharep def test_glob_subdirs(self, alpharep): root = zipfile.Path(alpharep) diff --git a/zipp/__init__.py b/zipp/__init__.py index d65297b..52ee74c 100644 --- a/zipp/__init__.py +++ b/zipp/__init__.py @@ -470,8 +470,7 @@ def glob(self, pattern): prefix = re.escape(self.at) tr = Translator(seps='/') matches = re.compile(prefix + tr.translate(pattern)).fullmatch - names = (data.filename for data in self.root.filelist) - return map(self._next, filter(matches, names)) + return map(self._next, filter(matches, self.root.namelist())) def rglob(self, pattern): return self.glob(f'**/{pattern}') diff --git a/zipp/glob.py b/zipp/glob.py index 69c41d7..4320f1c 100644 --- a/zipp/glob.py +++ b/zipp/glob.py @@ -28,7 +28,7 @@ def translate(self, pattern): """ Given a glob pattern, produce a regex that matches it. """ - return self.extend(self.translate_core(pattern)) + return self.extend(self.match_dirs(self.translate_core(pattern))) def extend(self, pattern): r""" @@ -41,6 +41,14 @@ def extend(self, pattern): """ return rf'(?s:{pattern})\Z' + def match_dirs(self, pattern): + """ + Ensure that zipfile.Path directory names are matched. + + zipfile.Path directory names always end in a slash. + """ + return rf'{pattern}[/]?' + def translate_core(self, pattern): r""" Given a glob pattern, produce a regex that matches it.