Skip to content

Commit f56fcc3

Browse files
authored
Transport: Bug fix in rename method (#6735)
`rename()` method gets two arguments as input, `old_path` and `new_path`. Previously, it was raising an `OSError` in case `new_path` wouldn't exist before renaming. This bug is fixed in both `ssh.py` and `local.py`. Additional test has been added to `test_all_plugins.py` to verify the correct behaviour.
1 parent 8c5c709 commit f56fcc3

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

src/aiida/transports/plugins/local.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ def rename(self, oldpath: TransportPath, newpath: TransportPath):
866866
:param str oldpath: existing name of the file or folder
867867
:param str newpath: new name for the file or folder
868868
869-
:raises OSError: if src/dst is not found
869+
:raises OSError: if oldpath is not found or newpath already exists
870870
:raises ValueError: if src/dst is not a valid string
871871
"""
872872
oldpath = str(oldpath)
@@ -877,8 +877,8 @@ def rename(self, oldpath: TransportPath, newpath: TransportPath):
877877
raise ValueError(f'Destination {newpath} is not a valid string')
878878
if not os.path.exists(oldpath):
879879
raise OSError(f'Source {oldpath} does not exist')
880-
if not os.path.exists(newpath):
881-
raise OSError(f'Destination {newpath} does not exist')
880+
if os.path.exists(newpath):
881+
raise OSError(f'Destination {newpath} already exists.')
882882

883883
shutil.move(oldpath, newpath)
884884

src/aiida/transports/plugins/ssh.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,8 +1357,8 @@ def rename(self, oldpath: TransportPath, newpath: TransportPath):
13571357
:param str oldpath: existing name of the file or folder
13581358
:param str newpath: new name for the file or folder
13591359
1360-
:raises OSError: if oldpath/newpath is not found
1361-
:raises ValueError: if sroldpathc/newpath is not a valid path
1360+
:raises OSError: if oldpath is not found or newpath already exists
1361+
:raises ValueError: if oldpath/newpath is not a valid path
13621362
"""
13631363
if not oldpath:
13641364
raise ValueError(f'Source {oldpath} is not a valid path')
@@ -1371,13 +1371,9 @@ def rename(self, oldpath: TransportPath, newpath: TransportPath):
13711371
if not self.isfile(oldpath):
13721372
if not self.isdir(oldpath):
13731373
raise OSError(f'Source {oldpath} does not exist')
1374-
# TODO: this seems to be a bug (?)
1375-
# why to raise an OSError if the newpath does not exist?
1376-
# ofcourse newpath shouldn't exist, that's why we are renaming it!
1377-
# issue opened here: https://github.com/aiidateam/aiida-core/issues/6725
1378-
if not self.isfile(newpath):
1379-
if not self.isdir(newpath):
1380-
raise OSError(f'Destination {newpath} does not exist')
1374+
1375+
if self.path_exists(newpath):
1376+
raise OSError(f'Destination {newpath} already exist')
13811377

13821378
return self.sftp.rename(oldpath, newpath)
13831379

tests/transports/test_all_plugins.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,3 +1232,28 @@ def test_asynchronous_execution(custom_transport, tmp_path):
12321232
except ProcessLookupError:
12331233
# If the process is already dead (or has never run), I just ignore the error
12341234
pass
1235+
1236+
1237+
def test_rename(custom_transport, tmp_path_remote):
1238+
"""Test the rename function of the transport plugin."""
1239+
with custom_transport as transport:
1240+
old_file = tmp_path_remote / 'oldfile.txt'
1241+
new_file = tmp_path_remote / 'newfile.txt'
1242+
another_file = tmp_path_remote / 'anotherfile.txt'
1243+
1244+
# Create a test file to rename
1245+
old_file.touch()
1246+
another_file.touch()
1247+
assert old_file.exists()
1248+
assert another_file.exists()
1249+
1250+
# Perform rename operation
1251+
transport.rename(old_file, new_file)
1252+
1253+
# Verify rename was successful
1254+
assert not old_file.exists()
1255+
assert new_file.exists()
1256+
1257+
# Perform rename operation if new file already exists
1258+
with pytest.raises(OSError, match='already exist|destination exists'):
1259+
transport.rename(new_file, another_file)

0 commit comments

Comments
 (0)