From 8929889bbf3bf44d4b5485431ad2a6998b8e0fed Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Sat, 9 Jul 2022 18:21:16 -0400 Subject: [PATCH] Update and Split Tests (#4) Removing unused imports Reworking tests to include more specific contexts Splitting store and restore tests Adding missing tests for utils --- pyproject.toml | 3 +- tests/test_transpose.py | 46 ++++++++++++++++--------- tests/test_utils.py | 16 +++++---- tests/utils.py | 76 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 112 insertions(+), 29 deletions(-) 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 61b3068..2176671 100644 --- a/tests/test_transpose.py +++ b/tests/test_transpose.py @@ -2,13 +2,20 @@ import json import pathlib import pytest -from transpose import Transpose, version, DEFAULT_CACHE_FILENAME +from transpose import Transpose from transpose.exceptions import TransposeError -from .utils import STORE_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, @@ -24,12 +31,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) - store_path.rmdir() - target_path.rename(store_path) t = Transpose( target_path=STORE_DIR, @@ -56,30 +61,34 @@ def test_apply(): assert target_path.is_dir() and target_path.is_symlink() -@setup_env() -def test_store_restore(): +@setup_store() +def test_store(): t = Transpose( target_path=TARGET_DIR, store_path=STORE_DIR, ) + t.store("TestStore") target_path = pathlib.Path(TARGET_DIR) store_path = pathlib.Path(STORE_DIR).joinpath("TestStore") - t.store("TestStore") - # STORE ## Successful Store assert store_path.is_dir() and not store_path.is_symlink() assert target_path.is_dir() and target_path.is_symlink() assert t.cache_path.is_file() + +@setup_restore() +def test_restore(): + target_path = pathlib.Path(TARGET_DIR) + stored_path = pathlib.Path(STORE_DIR).joinpath(STORED_DIR) + t = Transpose( - target_path=str(store_path), + target_path=str(stored_path), store_path=STORE_DIR, ) - # RESTORE - ## Missing Cache File + # Missing Cache File cache = t.cache_path.read_text() t.cache_path.unlink() with pytest.raises(TransposeError): @@ -87,15 +96,20 @@ def test_store_restore(): t.cache_path.write_text(cache) cache = json.loads(cache) - ## Missing Target Path + # Missing Target Path (original path) t.target_path.rename("newpath") with pytest.raises(TransposeError): t.restore() pathlib.Path("newpath").rename(t.target_path) - ## Successful Restore + # Original Path is a symlink - Should be removed and successfully restore + original_path = pathlib.Path(cache["original_path"]) + original_path.rename("newpath") + original_path.symlink_to("newpath") + + # Successful t.restore() - assert not store_path.exists() assert target_path.is_dir() and not target_path.is_symlink() + assert not stored_path.exists() assert not t.cache_path.exists() diff --git a/tests/test_utils.py b/tests/test_utils.py index 0a9e972..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") @@ -19,10 +19,12 @@ def test_check_path(): assert check_path(existing_dir) is True assert check_path(nonexisting_dir) is False assert check_path(symlink_dir, is_symlink=True) is True + assert check_path(symlink_dir) is False + assert check_path(existing_dir, is_symlink=True) is False assert check_path(cache_path) is False -@setup_env() +@setup_store() def test_cache_create(): cache_file = "test_cache_file.json" @@ -38,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) @@ -47,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) @@ -57,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) @@ -72,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 7feaa9f..898d900 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -6,18 +6,81 @@ from contextlib import contextmanager from tempfile import TemporaryDirectory from transpose import DEFAULT_CACHE_FILENAME, version -from transpose.exceptions import TransposeError -TARGET_DIR = "source" STORE_DIR = "store" +STORED_DIR = "my_app" # Directory already in storage SYMLINK_DIR = "symlink_test" +TARGET_DIR = "source" CACHE_FILE_CONTENTS = {"version": version, "original_path": TARGET_DIR} @contextmanager -def setup_env(): +def setup_apply(): + """ + Create the following directory structure: + temp/ + ├── target/ + │ └── .transpose.json # contains {"version": version, "original_path": "source/"} + └── symlink_test/ -> source/ + """ + old_dir = os.getcwd() + with TemporaryDirectory("tests-temp") as td: + 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}") + + target_cache_path = pathlib.Path(f"{STORE_DIR}/{STORED_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_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: @@ -27,9 +90,12 @@ def setup_env(): os.mkdir(STORE_DIR) os.symlink(TARGET_DIR, SYMLINK_DIR) - cache_path = pathlib.Path(TARGET_DIR).joinpath(DEFAULT_CACHE_FILENAME) - with open(str(cache_path), "w") as f: + target_cache_path = pathlib.Path(TARGET_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)