@@ -244,3 +244,50 @@ def test_ls_with_folders(compression: str, tmp_path: Path):
244244 "d/e/f.pdf" ,
245245 "d/g.pdf" ,
246246 ]
247+
248+
249+ @pytest .mark .parametrize (
250+ "compression" , ["" , "gz" , "bz2" , "xz" ], ids = ["tar" , "tar-gz" , "tar-bz2" , "tar-xz" ]
251+ )
252+ def test_ls_with_duplicate_slashes (compression : str , tmp_path : Path ):
253+ """
254+ Members whose names contain redundant duplicate slashes (e.g.
255+ ``"a/b//c.txt"``) must still be reachable through the directory listing
256+ and openable, rather than becoming silently invisible to
257+ ``find``/``glob``/``ls``/``walk``. Regression test for
258+ https://github.com/fsspec/filesystem_spec/issues/1947.
259+ """
260+ tar_data : dict [str , bytes ] = {
261+ "path/with/extra/slash//test.txt" : b"Hello slash!" ,
262+ "regular/file.txt" : b"Hello regular!" ,
263+ }
264+ if compression :
265+ temp_archive_file = tmp_path / f"test_tar_file.tar.{ compression } "
266+ else :
267+ temp_archive_file = tmp_path / "test_tar_file.tar"
268+ with open (temp_archive_file , "wb" ) as fd :
269+ with tarfile .open (fileobj = fd , mode = f"w:{ compression } " ) as tf :
270+ for tar_file_path , data in tar_data .items ():
271+ info = tarfile .TarInfo (name = tar_file_path )
272+ info .size = len (data )
273+ tf .addfile (info , BytesIO (data ))
274+
275+ with open (temp_archive_file , "rb" ) as fd :
276+ fs = TarFileSystem (fd )
277+
278+ # The duplicate-slash member is discoverable with its slashes collapsed.
279+ assert fs .find ("/" ) == [
280+ "path/with/extra/slash/test.txt" ,
281+ "regular/file.txt" ,
282+ ]
283+ assert fs .glob ("path/**/*.txt" ) == ["path/with/extra/slash/test.txt" ]
284+ assert fs .ls ("path/with/extra/slash" , detail = False ) == [
285+ "path/with/extra/slash/test.txt"
286+ ]
287+
288+ # The intermediate directory is inferred without a trailing slash.
289+ assert fs .isdir ("path/with/extra/slash" )
290+
291+ # It can be opened both by its normalised name and its original name.
292+ assert fs .cat ("path/with/extra/slash/test.txt" ) == b"Hello slash!"
293+ assert fs .cat ("path/with/extra/slash//test.txt" ) == b"Hello slash!"
0 commit comments