Skip to content

Commit

Permalink
DirFS: Handle paths with no leading / (#1638) (#1639)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Durant <[email protected]>
  • Loading branch information
metadaddy and martindurant authored Jun 29, 2024
1 parent 262f664 commit 7fee7c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion fsspec/implementations/dirfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ def _relpath(self, path):
if isinstance(path, str):
if not self.path:
return path
if path == self.path:
# We need to account for S3FileSystem returning paths that do not
# start with a '/'
if path == self.path or (
self.path.startswith(self.fs.sep) and path == self.path[1:]
):
return ""
prefix = self.path + self.fs.sep
if self.path.startswith(self.fs.sep) and not path.startswith(self.fs.sep):
prefix = prefix[1:]
assert path.startswith(prefix)
return path[len(prefix) :]
return [self._relpath(_path) for _path in path]
Expand Down
14 changes: 14 additions & 0 deletions fsspec/implementations/tests/test_dirfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def test_dirfs(fs, asyncfs):
("", "foo", "foo"),
("root", "", "root"),
("root", "foo", "root/foo"),
("/root", "", "/root"),
("/root", "foo", "/root/foo"),
],
)
def test_path(fs, root, rel, full):
Expand All @@ -90,6 +92,18 @@ def test_path(fs, root, rel, full):
assert dirfs._relpath(full) == rel


@pytest.mark.parametrize(
"root, rel, full",
[
("/root", "foo", "root/foo"),
("/root", "", "root"),
],
)
def test_path_no_leading_slash(fs, root, rel, full):
dirfs = DirFileSystem(root, fs)
assert dirfs._relpath(full) == rel


def test_sep(mocker, dirfs):
sep = mocker.Mock()
dirfs.fs.sep = sep
Expand Down

0 comments on commit 7fee7c1

Please sign in to comment.