Skip to content

Commit

Permalink
DirFS: Handle paths with no leading / (#1638)
Browse files Browse the repository at this point in the history
  • Loading branch information
metadaddy committed Jun 27, 2024
1 parent 262f664 commit 0f1013a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions fsspec/implementations/dirfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ def _relpath(self, path):
return path
if path == self.path:
return ""
# S3FileSystem returns paths that do not start with a '/', so we
# need to remove the leading '/' from self.path if there is one there
# but not on the incoming path
prefix = self.path + self.fs.sep
if not path.startswith(self.fs.sep) and self.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
12 changes: 12 additions & 0 deletions fsspec/implementations/tests/test_dirfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def test_path(fs, root, rel, full):
assert dirfs._relpath(full) == rel


@pytest.mark.parametrize(
"root, rel, full",
[
("/root", "foo", "root/foo"),
("/root", "foo", "/root/foo"),
],
)
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 0f1013a

Please sign in to comment.