Skip to content

Commit 0e3ed72

Browse files
committed
assert DestinationExists when using overwrite=False
1 parent 56bed83 commit 0e3ed72

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

fs/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,9 @@ def move(self, src_path, dst_path, overwrite=False, preserve_time=False):
11611161
raise errors.DestinationExists(dst_path)
11621162
if self.getinfo(src_path).is_dir:
11631163
raise errors.FileExpected(src_path)
1164+
if normpath(src_path) == normpath(dst_path):
1165+
# early exit when moving a file onto itself
1166+
return
11641167
if self.getmeta().get("supports_rename", False):
11651168
try:
11661169
src_sys_path = self.getsyspath(src_path)

fs/osfs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ def _check_copy(self, src_path, dst_path, overwrite=False):
409409
if self.gettype(src_path) is not ResourceType.file:
410410
raise errors.FileExpected(src_path)
411411
# check dst_path does not exist if we are not overwriting
412-
if not overwrite and _src_path != _dst_path and self.exists(_dst_path):
412+
if not overwrite and self.exists(_dst_path):
413413
raise errors.DestinationExists(dst_path)
414414
# check parent dir of _dst_path exists and is a directory
415415
if self.gettype(dirname(dst_path)) is not ResourceType.directory:

fs/test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,23 +1816,35 @@ def test_move_file_onto_itself(self):
18161816
self.fs.move("file.txt", "file.txt", overwrite=True)
18171817
self.assert_text("file.txt", "Hello")
18181818

1819+
with self.assertRaises(errors.DestinationExists):
1820+
self.fs.move("file.txt", "file.txt", overwrite=False)
1821+
18191822
def test_move_file_onto_itself_relpath(self):
18201823
subdir = self.fs.makedir("sub")
18211824
subdir.writetext("file.txt", "Hello")
18221825
self.fs.move("sub/file.txt", "sub/../sub/file.txt", overwrite=True)
18231826
self.assert_text("sub/file.txt", "Hello")
18241827

1828+
with self.assertRaises(errors.DestinationExists):
1829+
self.fs.move("sub/file.txt", "sub/../sub/file.txt", overwrite=False)
1830+
18251831
def test_copy_file_onto_itself(self):
18261832
self.fs.writetext("file.txt", "Hello")
18271833
self.fs.copy("file.txt", "file.txt", overwrite=True)
18281834
self.assert_text("file.txt", "Hello")
18291835

1836+
with self.assertRaises(errors.DestinationExists):
1837+
self.fs.copy("file.txt", "file.txt", overwrite=False)
1838+
18301839
def test_copy_file_onto_itself_relpath(self):
18311840
subdir = self.fs.makedir("sub")
18321841
subdir.writetext("file.txt", "Hello")
18331842
self.fs.copy("sub/file.txt", "sub/../sub/file.txt", overwrite=True)
18341843
self.assert_text("sub/file.txt", "Hello")
18351844

1845+
with self.assertRaises(errors.DestinationExists):
1846+
self.fs.copy("sub/file.txt", "sub/../sub/file.txt", overwrite=False)
1847+
18361848
def test_copydir(self):
18371849
self.fs.makedirs("foo/bar/baz/egg")
18381850
self.fs.writetext("foo/bar/foofoo.txt", "Hello")

0 commit comments

Comments
 (0)