diff --git a/tests/test_project.py b/tests/test_project.py index 594987f..1a60753 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -1,5 +1,6 @@ import json import os +import pytest from pathlib import Path @@ -7,6 +8,7 @@ from contextlib import contextmanager from tempfile import TemporaryDirectory from transpose import Transpose, version, DEFAULT_CACHE_FILENAME +from transpose.exceptions import TransposeError from transpose.utils import check_path, create_cache, get_cache, move, remove, symlink @@ -157,5 +159,18 @@ def test_transpose_store_restore(): @setup() def test_transpose_restore_force(): - # TODO - pass + nonexistent_path = Path(STORE_DIR).joinpath("long/path") + + cache_path = Path(TARGET_DIR).joinpath(DEFAULT_CACHE_FILENAME) + cache = {"version": version, "original_path": str(nonexistent_path)} + with open(str(cache_path), "w") as f: + json.dump(cache, f) + + t = Transpose(target_path=TARGET_DIR, store_path=STORE_DIR) + with pytest.raises(TransposeError): + t.restore() + assert not nonexistent_path.exists() + + t = Transpose(target_path=TARGET_DIR, store_path=STORE_DIR, force=True) + t.restore() + assert nonexistent_path.exists() diff --git a/transpose/transpose.py b/transpose/transpose.py index 53da516..5ce95c0 100644 --- a/transpose/transpose.py +++ b/transpose/transpose.py @@ -43,15 +43,16 @@ class Transpose: f"Original path in cache file already exists: {original_path}" ) + if not original_path.parent.exists() and self.force: + original_path.parent.mkdir(parents=True) + try: - original_path.parent.mkdir(parents=self.force, exist_ok=True) + move(source=self.target_path, destination=original_path) except FileNotFoundError: raise TransposeError( - f"The parent directory for the original path, {original_path.parent} does not exist. Use '-f' to force the creation of this directory" + f"Original path, {original_path}, does not exist. Use '-f' to create the path" ) - move(source=self.target_path, destination=original_path) - new_cache_path = Path(original_path).joinpath(self.cache_filename) remove(new_cache_path)