Browse Source

Correcting force option and creating test case

pull/2/head
Ryan Reed 2 years ago
parent
commit
318a2e8098
2 changed files with 22 additions and 6 deletions
  1. +17
    -2
      tests/test_project.py
  2. +5
    -4
      transpose/transpose.py

+ 17
- 2
tests/test_project.py View File

@ -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()

+ 5
- 4
transpose/transpose.py View File

@ -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)


Loading…
Cancel
Save