def test_rename_directory(self): td = tempfile.TemporaryDirectory() with TemporaryDirectoryHandler(td): # The base directory will be the same for both the original and # renamed directory. BASE_DIRECTORY = str(pathlib.Path(td.name).parent) # Rename the Directory renamed_directory_name = ( utils.get_random_directory_name(BASE_DIRECTORY) ) expected_new_directory_path = os.path.join( BASE_DIRECTORY, renamed_directory_name ) self.assertFalse( os.path.exists(expected_new_directory_path), msg=( "The directory can't be renamed because the path already " "exists." ) ) d = Directory(td.name) d.rename(renamed_directory_name) self.assertTrue( os.path.exists(expected_new_directory_path), msg="The directory was not renamed" ) return
def test_rename_updates_path(self): """If a Directory object is renamed, then its path should be updated""" td = tempfile.TemporaryDirectory() with TemporaryDirectoryHandler(td): BASE_DIRECTORY = str(pathlib.Path(td.name).parent) d = Directory(td.name) new_directory_name = utils.get_random_directory_name(BASE_DIRECTORY) expected_path = os.path.join(BASE_DIRECTORY, new_directory_name) d.rename(new_directory_name) self.assertEqual(d.path, expected_path) return
def test_raise_exception_on_rename_to_existing_path(self): # The Existing Path Refers to an Existing File td = tempfile.TemporaryDirectory() tf = tempfile.NamedTemporaryFile() tf_name = os.path.split(tf.name)[1] # Try to rename the second temporary file to that of the first with TemporaryDirectoryHandler(td): with TemporaryFileHandler(tf): d = Directory(td.name) with self.assertRaises(FileExistsError, msg="Existing file assert failed"): d.rename(tf_name) # The Existing Path Refers to an Existing Directory td1 = tempfile.TemporaryDirectory() td2 = tempfile.TemporaryDirectory() td2_name = os.path.split(td1.name)[1] # Try to rename the temporary file to that of the temporary directory with TemporaryDirectoryHandler(td1): with TemporaryDirectoryHandler(td2): d = Directory(td1.name) with self.assertRaises(IsADirectoryError, msg="Existing directory assert failed"): d.rename(td2_name) return