diff --git a/tests/test_tsdb.py b/tests/test_tsdb.py index 351ffe7..ce64de5 100644 --- a/tests/test_tsdb.py +++ b/tests/test_tsdb.py @@ -49,7 +49,13 @@ def test_3_dataset_purging(self): tsdb.delete_cache("physionet_2012") # delete single tsdb.delete_cache() # delete all - def test_4_logging(self): + def test_4_migrate(self): + os.makedirs("dir_for_migration") + with open("dir_for_migration/test.txt", "a") as f: + f.write("hello world") + tsdb.migrate("dir_for_migration", "new_dir/put_it_here") + + def test_5_logging(self): # different level logging self.logger.debug("debug") self.logger.info("info") diff --git a/tsdb/utils/file.py b/tsdb/utils/file.py index dc2c5a2..f8dbdd9 100644 --- a/tsdb/utils/file.py +++ b/tsdb/utils/file.py @@ -112,6 +112,7 @@ def migrate(old_path: str, new_path: str) -> None: """ if not os.path.exists(old_path): raise FileNotFoundError(f"Given old_path {old_path} does not exist.") + if os.path.exists(new_path): logger.warning(f"Please note that new_path {new_path} already exists.") # if new_path exists, we have to move everything from old_path into it @@ -123,10 +124,12 @@ def migrate(old_path: str, new_path: str) -> None: shutil.copytree(old_f_path, new_f_path) else: shutil.move(old_f_path, new_path) - shutil.rmtree(old_path, ignore_errors=True) else: # if new_path does not exist, just rename the old_path into it + new_parent_dir = os.path.abspath(os.path.join(new_path, "..")) + if not os.path.exists(new_parent_dir): + os.makedirs(new_parent_dir, exist_ok=True) os.rename(old_path, new_path) config = ConfigParser()