From a4c71f5943e009c074fe85c2e2db9f45e31b2521 Mon Sep 17 00:00:00 2001 From: Rudra Prasad Bhuyan Date: Tue, 17 Nov 2026 05:57:51 +0000 Subject: [PATCH 1/2] fix: preserve source file when move fallback copy fails --- sshfs/spec.py | 6 +-- tests/test_sshfs.py | 91 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/sshfs/spec.py b/sshfs/spec.py index 99dbfa5..95f4b4a 100644 --- a/sshfs/spec.py +++ b/sshfs/spec.py @@ -223,10 +223,8 @@ async def _mv(self, lpath, rpath, **kwargs): # In that case we are going to copy the file and delete # it. - try: - await self._cp_file(lpath, rpath) - finally: - await self._rm_file(lpath) + await self._cp_file(lpath, rpath) + await self._rm_file(lpath) @wrap_exceptions async def _put_file( diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index adf048e..51923d5 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -11,7 +11,7 @@ import fsspec import pytest -from asyncssh.sftp import SFTPAttrs, SFTPFailure +from asyncssh.sftp import SFTPAttrs, SFTPClient, SFTPFailure, SFTPOpUnsupported from sshfs import SSHFileSystem from sshfs.file import SSHFile @@ -172,10 +172,24 @@ def test_decode_attributes_missing_timestamps(fs): assert details["mtime"] is None -def test_move(fs, remote_dir): +def test_move(fs, remote_dir, monkeypatch): fs.touch(remote_dir + "/a.txt") initial_info = fs.info(remote_dir + "/a.txt") + async def posix_rename(self, lpath, rpath): + await self.rename(lpath, rpath) + + cp_file = fs._cp_file + cp_file_called = False + + async def track_cp_file(*args, **kwargs): + nonlocal cp_file_called + cp_file_called = True + return await cp_file(*args, **kwargs) + + monkeypatch.setattr(SFTPClient, "posix_rename", posix_rename) + monkeypatch.setattr(fs, "_cp_file", track_cp_file) + fs.move(remote_dir + "/a.txt", remote_dir + "/b.txt") secondary_info = fs.info(remote_dir + "/b.txt") @@ -196,6 +210,79 @@ def test_move(fs, remote_dir): seconds=1 ), "atime differs more than expected" assert initial_info == secondary_info + assert not cp_file_called + + +def test_move_fallback_copy_failure_preserves_source( + fs, remote_dir, monkeypatch +): + source = remote_dir + "/a.txt" + destination = remote_dir + "/missing/b.txt" + data = b"source data" + fs.pipe_file(source, data) + + async def posix_rename_unsupported(*args, **kwargs): + raise SFTPOpUnsupported("posix rename is unsupported") + + rm_file = fs._rm_file + rm_file_called = False + + async def track_rm_file(*args, **kwargs): + nonlocal rm_file_called + rm_file_called = True + return await rm_file(*args, **kwargs) + + monkeypatch.setattr(SFTPClient, "posix_rename", posix_rename_unsupported) + monkeypatch.setattr(fs, "_rm_file", track_rm_file) + + with pytest.raises(FileNotFoundError): + fs.mv(source, destination) + + assert fs.exists(source) + assert fs.cat_file(source) == data + assert not fs.exists(destination) + assert not rm_file_called + + +def test_move_fallback_copies_then_removes_source(fs, remote_dir, monkeypatch): + source = remote_dir + "/a.txt" + destination = remote_dir + "/b.txt" + data = b"source data" + fs.pipe_file(source, data) + + async def posix_rename_unsupported(*args, **kwargs): + raise SFTPOpUnsupported("posix rename is unsupported") + + monkeypatch.setattr(SFTPClient, "posix_rename", posix_rename_unsupported) + + fs.mv(source, destination) + + assert not fs.exists(source) + assert fs.cat_file(destination) == data + + +def test_move_fallback_propagates_copy_error(fs, remote_dir, monkeypatch): + source = remote_dir + "/a.txt" + destination = remote_dir + "/b.txt" + copy_error = RuntimeError("copy failed") + + async def posix_rename_unsupported(*args, **kwargs): + raise SFTPOpUnsupported("posix rename is unsupported") + + async def copy_file_failed(*args, **kwargs): + raise copy_error + + async def rm_file_not_called(*args, **kwargs): + pytest.fail("source removal must not run after a failed copy") + + monkeypatch.setattr(SFTPClient, "posix_rename", posix_rename_unsupported) + monkeypatch.setattr(fs, "_cp_file", copy_file_failed) + monkeypatch.setattr(fs, "_rm_file", rm_file_not_called) + + with pytest.raises(RuntimeError) as raised: + fs.mv(source, destination) + + assert raised.value is copy_error def test_copy(fs, remote_dir): From 2313303f2fbc9ef042255992bb2d6eec03bf1992 Mon Sep 17 00:00:00 2001 From: Ivan Shcheklein Date: Tue, 17 Nov 2026 15:23:31 -0800 Subject: [PATCH 2/2] tests: simplify mv tests - Rename test_move to test_move_posix_rename. A real rename keeps the file's timestamps, so compare info exactly instead of allowing a 1-second drift (only needed when mv took the copy fallback). - Share the unsupported posix_rename patch via a no_posix_rename fixture. - Drop the call-tracking wrappers; the content assertions already show the source survives and the copy isn't used. - Drop the mocked copy-error test; the real copy-failure test covers it. Co-Authored-By: Claude Opus 5.5 --- tests/test_sshfs.py | 124 ++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 92 deletions(-) diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index 51923d5..b87a534 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -4,7 +4,7 @@ import tempfile import warnings from concurrent import futures -from datetime import UTC, datetime, timedelta +from datetime import UTC, datetime from importlib.metadata import entry_points from pathlib import Path from types import SimpleNamespace @@ -172,117 +172,57 @@ def test_decode_attributes_missing_timestamps(fs): assert details["mtime"] is None -def test_move(fs, remote_dir, monkeypatch): - fs.touch(remote_dir + "/a.txt") - initial_info = fs.info(remote_dir + "/a.txt") +@pytest.fixture +def no_posix_rename(monkeypatch): + # Force the copy + delete fallback in SSHFileSystem._mv. + async def posix_rename(*args, **kwargs): + raise SFTPOpUnsupported("posix_rename is unsupported") + + monkeypatch.setattr(SFTPClient, "posix_rename", posix_rename) - async def posix_rename(self, lpath, rpath): - await self.rename(lpath, rpath) - cp_file = fs._cp_file - cp_file_called = False +def test_move_posix_rename(fs, remote_dir, monkeypatch): + # The mock server doesn't advertise posix-rename, so without this + # patch mv would always take the copy fallback. + async def posix_rename(self, oldpath, newpath): + await self.rename(oldpath, newpath) - async def track_cp_file(*args, **kwargs): - nonlocal cp_file_called - cp_file_called = True - return await cp_file(*args, **kwargs) + async def cp_file(*args, **kwargs): + pytest.fail("mv must not fall back to copying") monkeypatch.setattr(SFTPClient, "posix_rename", posix_rename) - monkeypatch.setattr(fs, "_cp_file", track_cp_file) + monkeypatch.setattr(fs, "_cp_file", cp_file) - fs.move(remote_dir + "/a.txt", remote_dir + "/b.txt") - secondary_info = fs.info(remote_dir + "/b.txt") + fs.touch(remote_dir + "/a.txt") + initial_info = fs.info(remote_dir + "/a.txt") - assert not fs.exists(remote_dir + "/a.txt") - assert fs.exists(remote_dir + "/b.txt") + fs.mv(remote_dir + "/a.txt", remote_dir + "/b.txt") + assert not fs.exists(remote_dir + "/a.txt") + secondary_info = fs.info(remote_dir + "/b.txt") initial_info.pop("name") secondary_info.pop("name") - - initial_mtime = initial_info.pop("mtime") - initial_atime = initial_info.pop("time") - secondary_mtime = secondary_info.pop("mtime") - secondary_atime = secondary_info.pop("time") - assert abs(secondary_mtime - initial_mtime) <= timedelta( - seconds=1 - ), "mtime differs more than expected" - assert abs(secondary_atime - initial_atime) <= timedelta( - seconds=1 - ), "atime differs more than expected" assert initial_info == secondary_info - assert not cp_file_called -def test_move_fallback_copy_failure_preserves_source( - fs, remote_dir, monkeypatch -): - source = remote_dir + "/a.txt" - destination = remote_dir + "/missing/b.txt" - data = b"source data" - fs.pipe_file(source, data) +def test_move_copy_fallback(fs, remote_dir, no_posix_rename): + fs.pipe_file(remote_dir + "/a.txt", b"data") - async def posix_rename_unsupported(*args, **kwargs): - raise SFTPOpUnsupported("posix rename is unsupported") + fs.mv(remote_dir + "/a.txt", remote_dir + "/b.txt") - rm_file = fs._rm_file - rm_file_called = False + assert not fs.exists(remote_dir + "/a.txt") + assert fs.cat_file(remote_dir + "/b.txt") == b"data" - async def track_rm_file(*args, **kwargs): - nonlocal rm_file_called - rm_file_called = True - return await rm_file(*args, **kwargs) - monkeypatch.setattr(SFTPClient, "posix_rename", posix_rename_unsupported) - monkeypatch.setattr(fs, "_rm_file", track_rm_file) +def test_move_copy_fallback_failure_keeps_source( + fs, remote_dir, no_posix_rename +): + fs.pipe_file(remote_dir + "/a.txt", b"data") with pytest.raises(FileNotFoundError): - fs.mv(source, destination) - - assert fs.exists(source) - assert fs.cat_file(source) == data - assert not fs.exists(destination) - assert not rm_file_called - - -def test_move_fallback_copies_then_removes_source(fs, remote_dir, monkeypatch): - source = remote_dir + "/a.txt" - destination = remote_dir + "/b.txt" - data = b"source data" - fs.pipe_file(source, data) - - async def posix_rename_unsupported(*args, **kwargs): - raise SFTPOpUnsupported("posix rename is unsupported") - - monkeypatch.setattr(SFTPClient, "posix_rename", posix_rename_unsupported) - - fs.mv(source, destination) - - assert not fs.exists(source) - assert fs.cat_file(destination) == data - - -def test_move_fallback_propagates_copy_error(fs, remote_dir, monkeypatch): - source = remote_dir + "/a.txt" - destination = remote_dir + "/b.txt" - copy_error = RuntimeError("copy failed") - - async def posix_rename_unsupported(*args, **kwargs): - raise SFTPOpUnsupported("posix rename is unsupported") - - async def copy_file_failed(*args, **kwargs): - raise copy_error - - async def rm_file_not_called(*args, **kwargs): - pytest.fail("source removal must not run after a failed copy") - - monkeypatch.setattr(SFTPClient, "posix_rename", posix_rename_unsupported) - monkeypatch.setattr(fs, "_cp_file", copy_file_failed) - monkeypatch.setattr(fs, "_rm_file", rm_file_not_called) - - with pytest.raises(RuntimeError) as raised: - fs.mv(source, destination) + fs.mv(remote_dir + "/a.txt", remote_dir + "/missing/b.txt") - assert raised.value is copy_error + assert fs.cat_file(remote_dir + "/a.txt") == b"data" def test_copy(fs, remote_dir):