@ -1,87 +1,41 @@ | |||
import json | |||
import pathlib | |||
from transpose import version, DEFAULT_CACHE_FILENAME | |||
from transpose.utils import check_path, create_cache, get_cache, move, remove, symlink | |||
from transpose import version | |||
from transpose.utils import move, remove, symlink | |||
from .utils import CACHE_FILE_CONTENTS, STORE_DIR, SYMLINK_DIR, TARGET_DIR, setup_store | |||
@setup_store() | |||
def test_check_path(): | |||
existing_dir = pathlib.Path(TARGET_DIR) | |||
nonexisting_dir = pathlib.Path("nonexistent") | |||
symlink_dir = pathlib.Path(SYMLINK_DIR) | |||
cache_path = pathlib.Path(TARGET_DIR).joinpath(DEFAULT_CACHE_FILENAME) | |||
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_store() | |||
def test_cache_create(): | |||
cache_file = "test_cache_file.json" | |||
cache_path = pathlib.Path(TARGET_DIR).joinpath(cache_file) | |||
original_path = pathlib.Path("/tmp/some/random/path") | |||
create_cache(cache_path=cache_path, original_path=original_path) | |||
cache = json.load(open(cache_path, "r")) | |||
assert cache_path.exists() | |||
assert cache["original_path"] == str(original_path) | |||
assert cache["version"] == version | |||
@setup_store() | |||
def test_cache_get(): | |||
cache_path = pathlib.Path(TARGET_DIR).joinpath(DEFAULT_CACHE_FILENAME) | |||
cache = get_cache(cache_path) | |||
assert cache["version"] == CACHE_FILE_CONTENTS["version"] | |||
assert cache["original_path"] == CACHE_FILE_CONTENTS["original_path"] | |||
from .utils import ( | |||
TARGET_PATH, | |||
ENTRY_STORE_PATH, | |||
STORE_PATH, | |||
SYMLINK_TEST_PATH, | |||
setup_store, | |||
) | |||
@setup_store() | |||
def test_file_move(): | |||
source_path = pathlib.Path(TARGET_DIR) | |||
destination_path = pathlib.Path(STORE_DIR) | |||
move(source=source_path.absolute(), destination=destination_path.absolute()) | |||
assert not source_path.exists() | |||
assert destination_path.exists() | |||
destination = STORE_PATH.joinpath("test_move") | |||
move(source=TARGET_PATH.absolute(), destination=destination.absolute()) | |||
assert not TARGET_PATH.exists() | |||
assert destination.exists() | |||
@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) | |||
target_filepath = pathlib.Path(TARGET_DIR) | |||
SYMLINK_TEST_PATH.symlink_to(ENTRY_STORE_PATH) | |||
remove(path=TARGET_PATH) | |||
remove(path=SYMLINK_TEST_PATH) | |||
remove(path=cache_path) | |||
remove(path=symlink_filepath) | |||
remove(path=target_filepath) | |||
assert not cache_path.exists() # Should be able to remove files | |||
assert not symlink_filepath.exists() # Should be able to remove symlinks | |||
assert target_filepath.exists() # Should not be able to remove directories | |||
assert TARGET_PATH.exists() # Should not be able to remove directories | |||
assert not ENTRY_STORE_PATH.exists() # Should be able to remove symlinks | |||
@setup_store() | |||
def test_file_symlink(): | |||
symlink_name = "test_link" | |||
symlink_filepath = pathlib.Path(symlink_name) | |||
target_filepath = pathlib.Path(TARGET_DIR) | |||
symlink(target_path=target_filepath, symlink_path=symlink_filepath) | |||
symlink(target_path=TARGET_PATH, symlink_path=SYMLINK_TEST_PATH) | |||
assert target_filepath.exists() | |||
assert symlink_filepath.is_symlink() | |||
assert symlink_filepath.readlink() == target_filepath.resolve() | |||
assert TARGET_PATH.exists() | |||
assert SYMLINK_TEST_PATH.is_symlink() | |||
assert SYMLINK_TEST_PATH.readlink() == TARGET_PATH.resolve() |
@ -1,101 +1,94 @@ | |||
import os | |||
import json | |||
import pathlib | |||
from contextlib import contextmanager | |||
from pathlib import Path | |||
from shutil import rmtree | |||
from tempfile import TemporaryDirectory | |||
from transpose import DEFAULT_CACHE_FILENAME, version | |||
from transpose import version | |||
STORE_DIR = "store" | |||
STORED_DIR = "my_app" # Directory already in storage | |||
SYMLINK_DIR = "symlink_test" | |||
TARGET_DIR = "source" | |||
ENTRY_NAME = "MyName" | |||
TESTS_PATH = Path("tests-temp") | |||
STORE_PATH = TESTS_PATH.joinpath("store") | |||
TARGET_PATH = TESTS_PATH.joinpath("source") | |||
SYMLINK_TEST_PATH = TESTS_PATH.joinpath("symlink_test") | |||
CACHE_FILE_CONTENTS = {"version": version, "original_path": TARGET_DIR} | |||
ENTRY_STORE_PATH = STORE_PATH.joinpath(ENTRY_NAME) | |||
TRANSPOSE_CONFIG_PATH = STORE_PATH.joinpath("transpose.json") | |||
TRANSPOSE_CONFIG = { | |||
"version": version, | |||
"entries": {ENTRY_NAME: {"name": ENTRY_NAME, "path": str(TARGET_PATH)}}, | |||
} | |||
@contextmanager | |||
def setup_apply(): | |||
""" | |||
Create the following directory structure: | |||
temp/ | |||
tests-temp/ | |||
├── store/ | |||
│ └── transpose.json | |||
├── 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) | |||
try: | |||
with TemporaryDirectory(str(TESTS_PATH)): | |||
STORE_PATH.mkdir(parents=True, exist_ok=True) | |||
TARGET_PATH.mkdir(parents=True, exist_ok=True) | |||
SYMLINK_TEST_PATH.symlink_to(TARGET_PATH.resolve()) | |||
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) | |||
with open(str(TRANSPOSE_CONFIG_PATH), "w") as f: | |||
json.dump(TRANSPOSE_CONFIG, f) | |||
yield | |||
finally: | |||
os.chdir(old_dir) | |||
finally: | |||
# This shouldn't be necessary but is for some reason | |||
rmtree(TESTS_PATH) | |||
@contextmanager | |||
def setup_restore(): | |||
""" | |||
Create the following directory structure: | |||
temp/ | |||
├── source/ | |||
tests-temp/ | |||
└── store/ | |||
└── my_app/ | |||
└── .transpose.json # contains {"version": version, "original_path": "source/"} | |||
├── MyName/ | |||
└── transpose.json | |||
""" | |||
old_dir = os.getcwd() | |||
with TemporaryDirectory("tests-temp") as td: | |||
try: | |||
os.chdir(td) | |||
try: | |||
with TemporaryDirectory(str(TESTS_PATH)): | |||
ENTRY_STORE_PATH.mkdir(parents=True, exist_ok=True) | |||
TARGET_PATH.mkdir(parents=True, exist_ok=True) | |||
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) | |||
with open(str(TRANSPOSE_CONFIG_PATH), "w") as f: | |||
json.dump(TRANSPOSE_CONFIG, f) | |||
yield | |||
finally: | |||
os.chdir(old_dir) | |||
finally: | |||
# This shouldn't be necessary but is for some reason | |||
rmtree(TESTS_PATH) | |||
@contextmanager | |||
def setup_store(): | |||
""" | |||
Create the following directory structure: | |||
temp/ | |||
tests-temp/ | |||
├── source/ | |||
│ └── .transpose.json # contains {"version": version, "original_path": "source/"} | |||
└── store/ | |||
└── transpose.json | |||
""" | |||
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) | |||
try: | |||
with TemporaryDirectory(str(TESTS_PATH)): | |||
TARGET_PATH.mkdir(parents=True, exist_ok=True) | |||
STORE_PATH.mkdir(parents=True, exist_ok=True) | |||
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) | |||
with open(str(TRANSPOSE_CONFIG_PATH), "w") as f: | |||
json.dump(TRANSPOSE_CONFIG, f) | |||
yield | |||
finally: | |||
os.chdir(old_dir) | |||
finally: | |||
# This shouldn't be necessary but is for some reason | |||
rmtree(TESTS_PATH) |