@@ -151,6 +151,45 @@ def test_move_file_read_only_mem_dest(self):
151
151
dst_ro .exists ("target.txt" ), "file should not have been copied over"
152
152
)
153
153
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).
158
+ with open_fs (fs_url ) as src , open_fs (fs_url ) as dst :
159
+ src .writetext ("file.txt" , "source content" )
160
+ 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" ))
165
+ fs .move .move_file (src , "file.txt" , dst , "target.txt" )
166
+ self .assertFalse (src .exists ("file.txt" ))
167
+ self .assertFalse (src .exists ("target.txt" ))
168
+ self .assertFalse (dst .exists ("file.txt" ))
169
+ self .assertTrue (dst .exists ("target.txt" ))
170
+ self .assertEquals (dst .readtext ("target.txt" ), "source content" )
171
+
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).
176
+ with open_fs (fs_url ) as tmp :
177
+ tmp .writetext ("file.txt" , "content" )
178
+ fs .move .move_file (tmp , "file.txt" , tmp , "file.txt" )
179
+ self .assertTrue (tmp .exists ("file.txt" ))
180
+ self .assertEquals (tmp .readtext ("file.txt" ), "content" )
181
+
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
+
154
193
@parameterized .expand ([(True ,), (False ,)])
155
194
def test_move_file_cleanup_on_error (self , cleanup ):
156
195
with open_fs ("mem://" ) as src , open_fs ("mem://" ) as dst :
0 commit comments