Skip to content

Commit d1d2268

Browse files
authored
Fix nested subdirectory listing #1430 (#1433)
1 parent 23b275e commit d1d2268

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

fsspec/implementations/reference.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -985,16 +985,24 @@ def _dircache_from_items(self):
985985
elif len(part) == 1:
986986
size = None
987987
else:
988-
_, start, size = part
988+
_, _, size = part
989989
par = path.rsplit("/", 1)[0] if "/" in path else ""
990990
par0 = par
991+
subdirs = [par0]
991992
while par0 and par0 not in self.dircache:
992-
# build parent directories
993-
self.dircache[par0] = []
994-
self.dircache.setdefault(
995-
par0.rsplit("/", 1)[0] if "/" in par0 else "", []
996-
).append({"name": par0, "type": "directory", "size": 0})
993+
# collect parent directories
997994
par0 = self._parent(par0)
995+
subdirs.append(par0)
996+
997+
subdirs = subdirs[::-1]
998+
for parent, child in zip(subdirs, subdirs[1:]):
999+
# register newly discovered directories
1000+
assert child not in self.dircache
1001+
assert parent in self.dircache
1002+
self.dircache[parent].append(
1003+
{"name": child, "type": "directory", "size": 0}
1004+
)
1005+
self.dircache[child] = []
9981006

9991007
self.dircache[par].append({"name": path, "type": "file", "size": size})
10001008

fsspec/implementations/tests/test_reference.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
def test_simple(server): # noqa: F811
13-
1413
refs = {
1514
"a": b"data",
1615
"b": (realfile, 0, 5),
@@ -53,6 +52,16 @@ def test_ls(server): # noqa: F811
5352
}
5453

5554

55+
def test_nested_dirs_ls():
56+
# issue #1430
57+
refs = {"a": "A", "B/C/b": "B", "B/C/d": "d", "B/_": "_"}
58+
fs = fsspec.filesystem("reference", fo=refs)
59+
assert len(fs.ls("")) == 2
60+
assert set(e["name"] for e in fs.ls("")) == set(["a", "B"])
61+
assert len(fs.ls("B")) == 2
62+
assert set(e["name"] for e in fs.ls("B")) == set(["B/C", "B/_"])
63+
64+
5665
def test_info(server): # noqa: F811
5766
refs = {
5867
"a": b"data",

0 commit comments

Comments
 (0)