From c38c867fa3a06b4726e3f2ef25fd6f729dfd0c4b Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Wed, 6 Jul 2022 23:03:13 -0400 Subject: [PATCH 1/5] Adding an already stored path for tests --- tests/utils.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/utils.py b/tests/utils.py index 7feaa9f..cefe149 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -9,15 +9,26 @@ 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(): + """ + Create the following directory structure: + temp/ + ├── source/ + │ └── .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() with TemporaryDirectory("tests-temp") as td: try: @@ -25,11 +36,20 @@ def setup_env(): os.mkdir(TARGET_DIR) os.mkdir(STORE_DIR) + os.mkdir(f"{STORE_DIR}/{STORED_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_path1 = pathlib.Path(TARGET_DIR).joinpath( + DEFAULT_CACHE_FILENAME + ) + with open(str(target_cache_path1), "w") as f: json.dump(CACHE_FILE_CONTENTS, f) + + target_cache_path2 = pathlib.Path(f"{STORE_DIR}/{STORED_DIR}").joinpath( + DEFAULT_CACHE_FILENAME + ) + target_cache_path2.write_text(target_cache_path1.read_text()) + yield finally: os.chdir(old_dir) -- 2.30.1 From 1c0fe49906a95b588503de537fa949d2f725df7d Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Wed, 6 Jul 2022 23:03:38 -0400 Subject: [PATCH 2/5] Adding missing tests for utils --- tests/test_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index 0a9e972..aead436 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -19,6 +19,8 @@ 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 -- 2.30.1 From 3f38d7e021e0b1964f99b13291d0f7bd5ecc1b44 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Wed, 6 Jul 2022 23:04:41 -0400 Subject: [PATCH 3/5] Splitting store and restore tests --- tests/test_transpose.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/tests/test_transpose.py b/tests/test_transpose.py index 61b3068..7911f4c 100644 --- a/tests/test_transpose.py +++ b/tests/test_transpose.py @@ -1,11 +1,12 @@ import json import pathlib import pytest +import shutil from transpose import Transpose, version, DEFAULT_CACHE_FILENAME from transpose.exceptions import TransposeError -from .utils import STORE_DIR, TARGET_DIR, setup_env +from .utils import STORE_DIR, STORED_DIR, TARGET_DIR, setup_env @setup_env() @@ -28,7 +29,7 @@ def test_init(): def test_apply(): store_path = pathlib.Path(STORE_DIR) target_path = pathlib.Path(TARGET_DIR) - store_path.rmdir() + shutil.rmtree(store_path) target_path.rename(store_path) t = Transpose( @@ -57,29 +58,33 @@ def test_apply(): @setup_env() -def test_store_restore(): +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_env() +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 +92,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() -- 2.30.1 From 03fcb895c3f8973c6dd02d991153c7dda5629b73 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Sat, 9 Jul 2022 18:13:47 -0400 Subject: [PATCH 4/5] Reworking tests to include more specific contexts and coverage --- pyproject.toml | 3 +- tests/test_transpose.py | 19 +++++++----- tests/test_utils.py | 14 ++++----- tests/utils.py | 68 ++++++++++++++++++++++++++++++++++------- 4 files changed, 78 insertions(+), 26 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 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: -- 2.30.1 From cf4f22e91d7e42fc7fd70c966e9da8441c85132d Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Sat, 9 Jul 2022 18:18:48 -0400 Subject: [PATCH 5/5] Removing unused imports --- tests/test_transpose.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_transpose.py b/tests/test_transpose.py index 5e86d93..2176671 100644 --- a/tests/test_transpose.py +++ b/tests/test_transpose.py @@ -1,9 +1,8 @@ import json import pathlib import pytest -import shutil -from transpose import Transpose, version, DEFAULT_CACHE_FILENAME +from transpose import Transpose from transpose.exceptions import TransposeError from .utils import ( -- 2.30.1