def test_replace_existing_file_on_move(self): # The temporary files need to be closed, but not deleted when .close() # is called. Otherwise, Windows will raise a PermissionError. tf1 = tempfile.NamedTemporaryFile(delete=False) tf2 = tempfile.NamedTemporaryFile(delete=False) tf1.close() tf2.close() f1 = File(tf1.name) f2 = File(tf2.name) # Try to replace the second temporary file with the first one with TemporaryFileHandler(tf1): with TemporaryFileHandler(tf2): try: f1.move( f2.parent, new_file_name=f2.name, replace_existing_file=True ) except FileExistsError: self.fail( "The file should have been replaced, but a " "FileExistsError was raised instead." ) else: self.assertFalse(os.path.exists(tf1.name)) self.assertTrue(os.path.exists(tf2.name)) return
def test_move_file_updates_path(self): """When a file is moved, its path should be updated.""" td1 = tempfile.TemporaryDirectory() td2 = tempfile.TemporaryDirectory() tf = tempfile.NamedTemporaryFile() f = File(tf.name) with TemporaryDirectoryHandler(td1): with TemporaryDirectoryHandler(td2): with TemporaryFileHandler(tf): expected_moved_file_path = os.path.join(td2.name, f.name) f.move(td2.name) self.assertEqual(f.path, expected_moved_file_path) return
def test_move_file(self): td1 = tempfile.TemporaryDirectory() td2 = tempfile.TemporaryDirectory() tf = tempfile.NamedTemporaryFile() f = File(tf.name) expected_moved_file_path = os.path.join(td2.name, f.name) self.assertFalse(os.path.exists(expected_moved_file_path)) with TemporaryDirectoryHandler(td1): with TemporaryDirectoryHandler(td2): with TemporaryFileHandler(tf): f.move(td2.name) self.assertTrue(os.path.exists(expected_moved_file_path)) return