diff --git a/pyproject.toml b/pyproject.toml index 997cd04..550ab3c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,9 +13,10 @@ python = "^3.7" black = "==22.6" flake8 = "==3.8.4" pre-commit = "*" -python-dotenv = "*" pytest = "*" +pytest-cov = "*" pytest-sugar = "*" +python-dotenv = "*" [tool.poetry.scripts] transpose = "transpose.console:entry_point" diff --git a/tests/test_transpose.py b/tests/test_transpose.py index 7911f4c..5e86d93 100644 --- a/tests/test_transpose.py +++ b/tests/test_transpose.py @@ -6,10 +6,17 @@ import shutil from transpose import Transpose, version, DEFAULT_CACHE_FILENAME from transpose.exceptions import TransposeError -from .utils import STORE_DIR, STORED_DIR, TARGET_DIR, setup_env +from .utils import ( + STORE_DIR, + STORED_DIR, + TARGET_DIR, + setup_restore, + setup_store, + setup_apply, +) -@setup_env() +@setup_store() def test_init(): t = Transpose( target_path=TARGET_DIR, @@ -25,12 +32,10 @@ def test_init(): assert t.cache_path == pathlib.Path(TARGET_DIR).joinpath(".transpose.txt") -@setup_env() +@setup_apply() def test_apply(): store_path = pathlib.Path(STORE_DIR) target_path = pathlib.Path(TARGET_DIR) - shutil.rmtree(store_path) - target_path.rename(store_path) t = Transpose( target_path=STORE_DIR, @@ -57,7 +62,7 @@ def test_apply(): assert target_path.is_dir() and target_path.is_symlink() -@setup_env() +@setup_store() def test_store(): t = Transpose( target_path=TARGET_DIR, @@ -74,7 +79,7 @@ def test_store(): assert t.cache_path.is_file() -@setup_env() +@setup_restore() def test_restore(): target_path = pathlib.Path(TARGET_DIR) stored_path = pathlib.Path(STORE_DIR).joinpath(STORED_DIR) diff --git a/tests/test_utils.py b/tests/test_utils.py index aead436..62a1c46 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -5,10 +5,10 @@ from transpose import version, DEFAULT_CACHE_FILENAME from transpose.utils import check_path, create_cache, get_cache, move, remove, symlink -from .utils import CACHE_FILE_CONTENTS, STORE_DIR, SYMLINK_DIR, TARGET_DIR, setup_env +from .utils import CACHE_FILE_CONTENTS, STORE_DIR, SYMLINK_DIR, TARGET_DIR, setup_store -@setup_env() +@setup_store() def test_check_path(): existing_dir = pathlib.Path(TARGET_DIR) nonexisting_dir = pathlib.Path("nonexistent") @@ -24,7 +24,7 @@ def test_check_path(): assert check_path(cache_path) is False -@setup_env() +@setup_store() def test_cache_create(): cache_file = "test_cache_file.json" @@ -40,7 +40,7 @@ def test_cache_create(): assert cache["version"] == version -@setup_env() +@setup_store() def test_cache_get(): cache_path = pathlib.Path(TARGET_DIR).joinpath(DEFAULT_CACHE_FILENAME) cache = get_cache(cache_path) @@ -49,7 +49,7 @@ def test_cache_get(): assert cache["original_path"] == CACHE_FILE_CONTENTS["original_path"] -@setup_env() +@setup_store() def test_file_move(): source_path = pathlib.Path(TARGET_DIR) destination_path = pathlib.Path(STORE_DIR) @@ -59,7 +59,7 @@ def test_file_move(): assert destination_path.exists() -@setup_env() +@setup_store() def test_file_remove(): cache_path = pathlib.Path(TARGET_DIR).joinpath(DEFAULT_CACHE_FILENAME) symlink_filepath = pathlib.Path(TARGET_DIR).joinpath(SYMLINK_DIR) @@ -74,7 +74,7 @@ def test_file_remove(): assert target_filepath.exists() # Should not be able to remove directories -@setup_env() +@setup_store() def test_file_symlink(): symlink_name = "test_link" symlink_filepath = pathlib.Path(symlink_name) diff --git a/tests/utils.py b/tests/utils.py index cefe149..898d900 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -6,7 +6,6 @@ from contextlib import contextmanager from tempfile import TemporaryDirectory from transpose import DEFAULT_CACHE_FILENAME, version -from transpose.exceptions import TransposeError STORE_DIR = "store" @@ -18,15 +17,12 @@ CACHE_FILE_CONTENTS = {"version": version, "original_path": TARGET_DIR} @contextmanager -def setup_env(): +def setup_apply(): """ Create the following directory structure: temp/ - ├── source/ + ├── target/ │ └── .transpose.json # contains {"version": version, "original_path": "source/"} - ├── store/ - │ └── my_app/ - │ └── .transpose.json # contains {"version": version, "original_path": "source/"} └── symlink_test/ -> source/ """ old_dir = os.getcwd() @@ -34,21 +30,71 @@ def setup_env(): try: os.chdir(td) + os.mkdir(STORE_DIR) + os.symlink(STORE_DIR, SYMLINK_DIR) + + target_cache_path = pathlib.Path(STORE_DIR).joinpath(DEFAULT_CACHE_FILENAME) + with open(str(target_cache_path), "w") as f: + json.dump(CACHE_FILE_CONTENTS, f) + + yield + finally: + os.chdir(old_dir) + + +@contextmanager +def setup_restore(): + """ + Create the following directory structure: + temp/ + ├── source/ + └── store/ + └── my_app/ + └── .transpose.json # contains {"version": version, "original_path": "source/"} + """ + old_dir = os.getcwd() + with TemporaryDirectory("tests-temp") as td: + try: + os.chdir(td) + os.mkdir(TARGET_DIR) os.mkdir(STORE_DIR) os.mkdir(f"{STORE_DIR}/{STORED_DIR}") - os.symlink(TARGET_DIR, SYMLINK_DIR) - target_cache_path1 = pathlib.Path(TARGET_DIR).joinpath( + target_cache_path = pathlib.Path(f"{STORE_DIR}/{STORED_DIR}").joinpath( DEFAULT_CACHE_FILENAME ) - with open(str(target_cache_path1), "w") as f: + with open(str(target_cache_path), "w") as f: json.dump(CACHE_FILE_CONTENTS, f) - target_cache_path2 = pathlib.Path(f"{STORE_DIR}/{STORED_DIR}").joinpath( + yield + finally: + os.chdir(old_dir) + + +@contextmanager +def setup_store(): + """ + Create the following directory structure: + temp/ + ├── source/ + │ └── .transpose.json # contains {"version": version, "original_path": "source/"} + └── store/ + """ + old_dir = os.getcwd() + with TemporaryDirectory("tests-temp") as td: + try: + os.chdir(td) + + os.mkdir(TARGET_DIR) + os.mkdir(STORE_DIR) + os.symlink(TARGET_DIR, SYMLINK_DIR) + + target_cache_path = pathlib.Path(TARGET_DIR).joinpath( DEFAULT_CACHE_FILENAME ) - target_cache_path2.write_text(target_cache_path1.read_text()) + with open(str(target_cache_path), "w") as f: + json.dump(CACHE_FILE_CONTENTS, f) yield finally: