Skip to content

Commit 6270a7e

Browse files
committed
fix: normalize ZIP paths in pipe_file and find
1 parent 13b0bce commit 6270a7e

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

fsspec/implementations/tests/test_zip.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ def test_write_seek(m):
6666
assert fs.cat("another") == b"hi"
6767

6868

69+
@pytest.mark.parametrize("prefix", ["", "/", "zip://", "zip:///"])
70+
def test_pipe_file_normalizes_path(m, prefix):
71+
fs = fsspec.filesystem("zip", fo="memory://out.zip", mode="w")
72+
fs.pipe_file(f"{prefix}reports/result.csv", b"total\n12\n")
73+
fs.close()
74+
75+
fs = fsspec.filesystem("zip", fo="memory://out.zip")
76+
assert fs.find("") == ["reports/result.csv"]
77+
assert fs.cat("reports/result.csv") == b"total\n12\n"
78+
fs.close()
79+
80+
6981
def test_rw(m):
7082
# extra arg to zip means "create archive"
7183
with fsspec.open(
@@ -457,13 +469,15 @@ def test_find_returns_expected_result_detail_false_include_dirs(zip_file):
457469
assert result == expected_result
458470

459471

460-
def test_find_returns_expected_result_path_set(zip_file):
472+
@pytest.mark.parametrize("prefix", ["/", "zip://", "zip:///"])
473+
@pytest.mark.parametrize("detail", [False, True])
474+
def test_find_returns_expected_result_path_set(zip_file, prefix, detail):
461475
zip_file_system = ZipFileSystem(zip_file)
462476

463-
result = zip_file_system.find("/dir2")
477+
result = zip_file_system.find(f"{prefix}dir2", detail=detail)
464478
expected_result = ["dir2/file3.txt"]
465479

466-
assert result == expected_result
480+
assert list(result) == expected_result
467481

468482

469483
def test_find_with_and_without_slash_should_return_same_result(zip_file):
@@ -472,10 +486,11 @@ def test_find_with_and_without_slash_should_return_same_result(zip_file):
472486
assert zip_file_system.find("/dir2/") == zip_file_system.find("/dir2")
473487

474488

475-
def test_find_should_return_file_if_exact_match(zip_file):
489+
@pytest.mark.parametrize("prefix", ["/", "zip://", "zip:///"])
490+
def test_find_should_return_file_if_exact_match(zip_file, prefix):
476491
zip_file_system = ZipFileSystem(zip_file)
477492

478-
result = zip_file_system.find("/dir2startwithsamename.txt", detail=False)
493+
result = zip_file_system.find(f"{prefix}dir2startwithsamename.txt", detail=False)
479494
expected_result = ["dir2startwithsamename.txt"]
480495

481496
assert result == expected_result

fsspec/implementations/zip.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def _get_dirs(self):
111111

112112
def pipe_file(self, path, value, **kwargs):
113113
# override upstream, because we know the exact file size in this case
114+
path = self._strip_protocol(path)
114115
self.zip.writestr(path, value, **kwargs)
115116

116117
def _open(
@@ -146,9 +147,7 @@ def to_parts(_path: str):
146147
if not isinstance(path, str):
147148
path = str(path)
148149

149-
# Remove the leading slash, as the zip file paths are always
150-
# given without a leading slash
151-
path = path.lstrip("/")
150+
path = self._strip_protocol(path)
152151
path_parts = to_parts(path)
153152
path_depth = len(path_parts)
154153

0 commit comments

Comments
 (0)