Skip to content

Commit 863dc83

Browse files
committed
compare normalized pathes in early exit
1 parent bdfc774 commit 863dc83

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

fs/move.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .errors import FSError
1111
from .opener import manage_fs
1212
from .osfs import OSFS
13-
from .path import frombase
13+
from .path import frombase, normpath
1414

1515
if typing.TYPE_CHECKING:
1616
from typing import Text, Union
@@ -64,7 +64,8 @@ def move_file(
6464
with manage_fs(src_fs, writeable=True) as _src_fs:
6565
with manage_fs(dst_fs, writeable=True, create=True) as _dst_fs:
6666
if _src_fs is _dst_fs:
67-
if src_path == dst_path:
67+
# Exit early if source and destination are the same file
68+
if normpath(src_path) == normpath(dst_path):
6869
return
6970

7071
# Same filesystem, may be optimized

tests/test_move.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,45 @@ def test_move_file_read_only_mem_dest(self):
151151
dst_ro.exists("target.txt"), "file should not have been copied over"
152152
)
153153

154-
@parameterized.expand([("temp://",), ("mem://",)])
155-
def test_move_file_overwrite(self, fs_url):
156-
# we use TempFS and MemoryFS in order to make sure the optimised code path
157-
# behaves like the regular one
154+
@parameterized.expand([("temp", "temp://"), ("mem", "mem://")])
155+
def test_move_file_overwrite(self, _, fs_url):
156+
# we use TempFS and MemoryFS in order to make sure the optimized code path
157+
# behaves like the regular one (TempFS tests the optmized code path).
158158
with open_fs(fs_url) as src, open_fs(fs_url) as dst:
159159
src.writetext("file.txt", "source content")
160160
dst.writetext("target.txt", "target content")
161+
self.assertTrue(src.exists("file.txt"))
162+
self.assertFalse(src.exists("target.txt"))
163+
self.assertFalse(dst.exists("file.txt"))
164+
self.assertTrue(dst.exists("target.txt"))
161165
fs.move.move_file(src, "file.txt", dst, "target.txt")
162166
self.assertFalse(src.exists("file.txt"))
163167
self.assertFalse(src.exists("target.txt"))
164168
self.assertFalse(dst.exists("file.txt"))
165169
self.assertTrue(dst.exists("target.txt"))
166170
self.assertEquals(dst.readtext("target.txt"), "source content")
167171

168-
@parameterized.expand([("temp://",), ("mem://",)])
169-
def test_move_file_overwrite_itself(self, fs_url):
170-
# we use TempFS and MemoryFS in order to make sure the optimised code path
171-
# behaves like the regular one
172+
@parameterized.expand([("temp", "temp://"), ("mem", "mem://")])
173+
def test_move_file_overwrite_itself(self, _, fs_url):
174+
# we use TempFS and MemoryFS in order to make sure the optimized code path
175+
# behaves like the regular one (TempFS tests the optmized code path).
172176
with open_fs(fs_url) as tmp:
173177
tmp.writetext("file.txt", "content")
174178
fs.move.move_file(tmp, "file.txt", tmp, "file.txt")
175179
self.assertTrue(tmp.exists("file.txt"))
176180
self.assertEquals(tmp.readtext("file.txt"), "content")
177181

182+
@parameterized.expand([("temp", "temp://"), ("mem", "mem://")])
183+
def test_move_file_overwrite_itself_relpath(self, _, fs_url):
184+
# we use TempFS and MemoryFS in order to make sure the optimized code path
185+
# behaves like the regular one (TempFS tests the optmized code path).
186+
with open_fs(fs_url) as tmp:
187+
new_dir = tmp.makedir("dir")
188+
new_dir.writetext("file.txt", "content")
189+
fs.move.move_file(tmp, "dir/../dir/file.txt", tmp, "dir/file.txt")
190+
self.assertTrue(tmp.exists("dir/file.txt"))
191+
self.assertEquals(tmp.readtext("dir/file.txt"), "content")
192+
178193
@parameterized.expand([(True,), (False,)])
179194
def test_move_file_cleanup_on_error(self, cleanup):
180195
with open_fs("mem://") as src, open_fs("mem://") as dst:

0 commit comments

Comments
 (0)