From 3a5e15e87e327aa0655aa59cab68020efc4f0584 Mon Sep 17 00:00:00 2001 From: Ivan Shcheklein Date: Sat, 28 Nov 2026 14:46:22 -0800 Subject: [PATCH 1/2] fix: upload to relative and top-level paths put_file and pipe_file create the parent directory first. For a relative path such as "file" or a top-level path such as "/file", _parent returns "", and asyncssh's makedirs("") sends mkdir(""), which the server rejects, so the upload failed with FileNotFoundError. The parent of such a path is the home directory or the root, which always exists, so skip makedirs when the parent is "". Co-Authored-By: Claude Opus 5.5 --- sshfs/spec.py | 8 ++++++-- tests/test_sshfs.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/sshfs/spec.py b/sshfs/spec.py index 2338040..7128169 100644 --- a/sshfs/spec.py +++ b/sshfs/spec.py @@ -254,7 +254,9 @@ async def _put_file( **kwargs, ): rpath = self._strip_protocol(rpath) - await self._makedirs(self._parent(rpath), exist_ok=True) + # "" means the parent is the root or the home directory, which exist + if parent := self._parent(rpath): + await self._makedirs(parent, exist_ok=True) async with self._pool.get() as channel: await channel.put( lpath, @@ -422,7 +424,9 @@ async def _cat_file(self, path, **kwargs): async def _pipe_file(self, path, data, chunksize=50 * 2**20, **kwargs): """Asynchronously writes the given data to a remote file in chunks.""" path = self._strip_protocol(path) - await self._makedirs(self._parent(path), exist_ok=True) + # "" means the parent is the root or the home directory, which exist + if parent := self._parent(path): + await self._makedirs(parent, exist_ok=True) async with self._pool.get() as channel: async with channel.open(path, "wb") as f: diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index 494f523..26f413f 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -606,3 +606,36 @@ def test_pipe_file(fs, remote_dir): assert ( f.read() == test_data ), "The data read from the file does not match the data written." + + +def test_put_and_pipe_relative_path(fs, tmp_path, monkeypatch): + # The mock server resolves relative paths against the current directory + monkeypatch.chdir(tmp_path) + source = tmp_path / "source" + source.write_bytes(b"data") + + fs.put_file(str(source), "put-file") + fs.pipe_file("pipe-file", b"data") + + assert (tmp_path / "put-file").read_bytes() == b"data" + assert (tmp_path / "pipe-file").read_bytes() == b"data" + + +def test_put_file_top_level_path(fs, tmp_path, monkeypatch): + # The tests can't write to "/", so the upload is stubbed out + async def makedirs(*args, **kwargs): + pytest.fail("the root always exists") + + uploads = [] + + async def put(self, lpath, rpath, **kwargs): + uploads.append(rpath) + + monkeypatch.setattr(fs, "_makedirs", makedirs) + monkeypatch.setattr(SFTPClient, "put", put) + source = tmp_path / "source" + source.write_bytes(b"data") + + fs.put_file(str(source), "/file") + + assert uploads == ["/file"] From f3c2a83b24db8df9f334824bfe27e7c47afd67a1 Mon Sep 17 00:00:00 2001 From: Ivan Shcheklein Date: Sat, 28 Nov 2026 17:04:21 -0800 Subject: [PATCH 2/2] test: one upload test per method, stubbing only the SFTP calls - Split the relative-path test into put_file and pipe_file tests, each next to its existing test. - The top-level put_file test no longer mocks _makedirs. It stubs only SFTPClient.put, so without the fix it fails on the real makedirs(""). - Add the same top-level test for pipe_file, which stubs SFTPClient.open. Co-Authored-By: Claude Opus 5.5 --- tests/test_sshfs.py | 62 ++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index 26f413f..40ca1b8 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -4,6 +4,7 @@ import secrets import warnings from concurrent import futures +from contextlib import asynccontextmanager from datetime import UTC, datetime from importlib.metadata import entry_points from pathlib import Path @@ -542,6 +543,27 @@ def test_put_file(fs, remote_dir, file_path, tmp_path): assert stream.read() == b"data" +def test_put_file_relative_path(fs, tmp_path, monkeypatch): + # The mock server resolves relative paths against the current directory + monkeypatch.chdir(tmp_path) + (tmp_path / "upload.txt").write_bytes(b"data") + fs.put_file(str(tmp_path / "upload.txt"), "a.txt") + assert (tmp_path / "a.txt").read_bytes() == b"data" + + +def test_put_file_top_level_path(fs, tmp_path, monkeypatch): + # The tests can't write to "/", so the upload is stubbed out + uploads = [] + + async def put(self, lpath, rpath, **kwargs): + uploads.append(rpath) + + monkeypatch.setattr(SFTPClient, "put", put) + (tmp_path / "upload.txt").write_bytes(b"data") + fs.put_file(str(tmp_path / "upload.txt"), "/a.txt") + assert uploads == ["/a.txt"] + + @pytest.mark.parametrize("file_path", ["a.txt", "dir/a.txt"]) def test_get_file(fs, remote_dir, file_path, tmp_path): remote_file = remote_dir + f"/{file_path}" @@ -608,34 +630,28 @@ def test_pipe_file(fs, remote_dir): ), "The data read from the file does not match the data written." -def test_put_and_pipe_relative_path(fs, tmp_path, monkeypatch): +def test_pipe_file_relative_path(fs, tmp_path, monkeypatch): # The mock server resolves relative paths against the current directory monkeypatch.chdir(tmp_path) - source = tmp_path / "source" - source.write_bytes(b"data") - - fs.put_file(str(source), "put-file") - fs.pipe_file("pipe-file", b"data") - - assert (tmp_path / "put-file").read_bytes() == b"data" - assert (tmp_path / "pipe-file").read_bytes() == b"data" + fs.pipe_file("a.txt", b"data") + assert (tmp_path / "a.txt").read_bytes() == b"data" -def test_put_file_top_level_path(fs, tmp_path, monkeypatch): - # The tests can't write to "/", so the upload is stubbed out - async def makedirs(*args, **kwargs): - pytest.fail("the root always exists") - - uploads = [] +def test_pipe_file_top_level_path(fs, monkeypatch): + # The tests can't write to "/", so the file is written to memory + written = {} - async def put(self, lpath, rpath, **kwargs): - uploads.append(rpath) + class File: + def __init__(self, path): + self.path = path - monkeypatch.setattr(fs, "_makedirs", makedirs) - monkeypatch.setattr(SFTPClient, "put", put) - source = tmp_path / "source" - source.write_bytes(b"data") + async def write(self, data): + written[self.path] = data - fs.put_file(str(source), "/file") + @asynccontextmanager + async def open(self, path, mode): + yield File(path) - assert uploads == ["/file"] + monkeypatch.setattr(SFTPClient, "open", open) + fs.pipe_file("/a.txt", b"data") + assert written == {"/a.txt": b"data"}