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..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}" @@ -606,3 +628,30 @@ 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_pipe_file_relative_path(fs, tmp_path, monkeypatch): + # The mock server resolves relative paths against the current directory + monkeypatch.chdir(tmp_path) + fs.pipe_file("a.txt", b"data") + assert (tmp_path / "a.txt").read_bytes() == b"data" + + +def test_pipe_file_top_level_path(fs, monkeypatch): + # The tests can't write to "/", so the file is written to memory + written = {} + + class File: + def __init__(self, path): + self.path = path + + async def write(self, data): + written[self.path] = data + + @asynccontextmanager + async def open(self, path, mode): + yield File(path) + + monkeypatch.setattr(SFTPClient, "open", open) + fs.pipe_file("/a.txt", b"data") + assert written == {"/a.txt": b"data"}