Skip to content

Commit

Permalink
Make sure find("dir/") == find("dir")
Browse files Browse the repository at this point in the history
  • Loading branch information
johandahlberg committed Aug 21, 2024
1 parent e228176 commit 3e2b3fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions fsspec/implementations/tests/test_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 9 additions & 1 deletion fsspec/implementations/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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):
Expand Down

0 comments on commit 3e2b3fe

Please sign in to comment.