Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion sshfs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ def __init__(
def _strip_protocol(cls, path):
# Remove components such as host and username from path.
inferred_path = infer_storage_options(path)["path"]
return super()._strip_protocol(inferred_path)
stripped = super()._strip_protocol(inferred_path)
# super() collapses a bare "/" to the (empty) root_marker. Restore it
# only when the caller explicitly supplied the absolute root, so that
# walk("/") lists from the root while relative paths (and "") still
# resolve against the home directory.
if not stripped and inferred_path.startswith("/"):
Comment thread
shcheklein marked this conversation as resolved.
return "/"
return stripped

@staticmethod
def _get_kwargs_from_urls(urlpath):
Expand Down
39 changes: 39 additions & 0 deletions tests/test_sshfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,45 @@ def test_ls(fs, remote_dir):
assert dirs == expected


def test_walk(fs, remote_dir):
fs.mkdir(remote_dir + "/a")
fs.mkdir(remote_dir + "/a/b")
fs.touch(remote_dir + "/a/f1")
fs.touch(remote_dir + "/a/b/f2")

result = {
root: (sorted(dirs), sorted(files))
for root, dirs, files in fs.walk(remote_dir + "/a")
}
assert result == {
remote_dir + "/a": (["b"], ["f1"]),
remote_dir + "/a/b": ([], ["f2"]),
}


def test_root(fs):
# An explicit "/" must resolve to the filesystem root, not the
# user's home directory.
assert fs.exists("/")
assert fs.isdir("/")
assert fs.info("/")["name"] == "/"


def test_strip_protocol():
strip = SSHFileSystem._strip_protocol
# An explicit absolute root is preserved so walk("/") lists from the root.
assert strip("/") == "/"
assert strip("ssh://host/") == "/"
# Empty and relative paths still resolve against the home directory
# instead of being redirected to the root.
assert strip("") == ""
assert strip("ssh://host") == ""
assert strip("foo/bar") == "foo/bar"
# Regular absolute paths are unaffected.
assert strip("/foo/bar") == "/foo/bar"
assert strip("ssh://host/foo/bar") == "/foo/bar"


def test_mkdir(fs, remote_dir):
fs.mkdir(remote_dir + "dir/")
assert fs.isdir(remote_dir + "dir/")
Expand Down
Loading