From 3e2b3fe011328ba993f75c3d5fce3cf17582c0ea Mon Sep 17 00:00:00 2001 From: Johan Dahlberg Date: Wed, 21 Aug 2024 14:53:54 +0200 Subject: [PATCH] Make sure find("dir/") == find("dir") --- fsspec/implementations/tests/test_zip.py | 6 ++++++ fsspec/implementations/zip.py | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/fsspec/implementations/tests/test_zip.py b/fsspec/implementations/tests/test_zip.py index 8402081d5..ecd082f3f 100644 --- a/fsspec/implementations/tests/test_zip.py +++ b/fsspec/implementations/tests/test_zip.py @@ -446,6 +446,12 @@ def test_find_returns_expected_result_path_set(zip_file): assert result == expected_result +def test_find_with_and_without_slash_should_return_same_result(zip_file): + zip_file_system = ZipFileSystem(zip_file) + + assert zip_file_system.find("/dir2/") == zip_file_system.find("/dir2") + + def test_find_should_return_file_if_exact_match(zip_file): zip_file_system = ZipFileSystem(zip_file) diff --git a/fsspec/implementations/zip.py b/fsspec/implementations/zip.py index f8b20825b..f8f440563 100644 --- a/fsspec/implementations/zip.py +++ b/fsspec/implementations/zip.py @@ -149,6 +149,14 @@ def _below_max_recursion_depth(path): # Remove the leading slash, as the zip file paths are always # given without a leading slash path = path.lstrip("/") + path_parts = list(filter(lambda s: bool(s), path.split("/"))) + + def _matching_starts(file_path): + file_parts = filter(lambda s: bool(s), file_path.split("/")) + for a, b in zip(path_parts, file_parts): + if a != b: + return False + return True self._get_dirs() @@ -159,7 +167,7 @@ def _below_max_recursion_depth(path): return result if detail else [path] for file_path, file_info in self.dir_cache.items(): - if not (path == "" or file_path.startswith(path + "/")): + if not (path == "" or _matching_starts(file_path)): continue if _below_max_recursion_depth(file_path):