add file change check to copy_env_files, add test cases for copy_env_files
This commit is contained in:
parent
5102cf2d91
commit
8e926d4e64
3 changed files with 159 additions and 6 deletions
137
tests/test_env_manager.py
Normal file
137
tests/test_env_manager.py
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from pytest_abra.dir_manager import DirManager
|
||||
from pytest_abra.env_manager import EnvManager
|
||||
from pytest_abra.utils import files_are_same
|
||||
|
||||
ENV_PATHS = [
|
||||
Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress
|
||||
Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik
|
||||
Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmp_output(tmp_path_factory: pytest.TempPathFactory) -> Path:
|
||||
return tmp_path_factory.mktemp("output")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmp_recipes(tmp_path_factory: pytest.TempPathFactory) -> Path:
|
||||
return tmp_path_factory.mktemp("recipes")
|
||||
|
||||
|
||||
def test_copy_env_files(tmp_output: Path, tmp_recipes: Path):
|
||||
# create dirs in output
|
||||
DIR = DirManager(output_dir=tmp_output, session_id="abc", recipes_dir=tmp_recipes)
|
||||
DIR.create_all_dirs()
|
||||
|
||||
# confirm dir is empty
|
||||
assert len(list(DIR.ENV_FILES.iterdir())) == 0
|
||||
|
||||
# copy env files
|
||||
env_files = EnvManager._get_env_files(ENV_PATHS)
|
||||
EnvManager.copy_env_files(env_files, DIR)
|
||||
|
||||
# check that each env file is present in DIR.ENV_FILES with correct contents
|
||||
assert len(list(DIR.ENV_FILES.iterdir())) == len(env_files)
|
||||
for index, env_path in enumerate(ENV_PATHS):
|
||||
matching_files = [f for f in DIR.ENV_FILES.iterdir() if index == int(f.name.split("-")[0])]
|
||||
assert len(matching_files) == 1
|
||||
assert files_are_same(env_path, matching_files[0])
|
||||
|
||||
|
||||
def test_copy_env_files_twice(tmp_output: Path, tmp_recipes: Path):
|
||||
"""Copy the same env files twice"""
|
||||
# create dirs in output
|
||||
DIR = DirManager(output_dir=tmp_output, session_id="abc", recipes_dir=tmp_recipes)
|
||||
DIR.create_all_dirs()
|
||||
|
||||
# confirm dir is empty
|
||||
assert len(list(DIR.ENV_FILES.iterdir())) == 0
|
||||
|
||||
# copy env files
|
||||
env_files = EnvManager._get_env_files(ENV_PATHS)
|
||||
EnvManager.copy_env_files(env_files, DIR)
|
||||
|
||||
# check that each env file is present in DIR.ENV_FILES with correct contents
|
||||
assert len(list(DIR.ENV_FILES.iterdir())) == len(env_files)
|
||||
|
||||
# copy env files again
|
||||
EnvManager.copy_env_files(env_files, DIR)
|
||||
|
||||
for index, env_path in enumerate(ENV_PATHS):
|
||||
matching_files = [f for f in DIR.ENV_FILES.iterdir() if index == int(f.name.split("-")[0])]
|
||||
assert len(matching_files) == 1
|
||||
assert files_are_same(env_path, matching_files[0])
|
||||
|
||||
|
||||
def test_copy_env_files_twice_with_content_change(tmp_output: Path, tmp_recipes: Path, tmp_path: Path):
|
||||
# copy env files to tmp_path
|
||||
assert len(list(tmp_path.iterdir())) == 0
|
||||
for f in ENV_PATHS:
|
||||
shutil.copy(f, tmp_path / f.name)
|
||||
ENV_PATHS_NEW = list(tmp_path.iterdir())
|
||||
assert len(ENV_PATHS_NEW) > 0
|
||||
|
||||
# create dirs in output
|
||||
DIR = DirManager(output_dir=tmp_output, session_id="abc", recipes_dir=tmp_recipes)
|
||||
DIR.create_all_dirs()
|
||||
|
||||
# confirm dir is empty
|
||||
assert len(list(DIR.ENV_FILES.iterdir())) == 0
|
||||
|
||||
# copy env files from tmp_path to tmp_output
|
||||
env_files = EnvManager._get_env_files(ENV_PATHS_NEW)
|
||||
EnvManager.copy_env_files(env_files, DIR)
|
||||
|
||||
# check that each env file is present in DIR.ENV_FILES with correct contents
|
||||
assert len(list(DIR.ENV_FILES.iterdir())) == len(env_files)
|
||||
|
||||
# change content of one env_file in tmp_path
|
||||
file_path = next(tmp_path.iterdir())
|
||||
with open(file_path, "w") as file:
|
||||
file.write("This is the new content")
|
||||
|
||||
# copy env files again
|
||||
with pytest.raises(AssertionError) as excinfo:
|
||||
EnvManager.copy_env_files(env_files, DIR)
|
||||
|
||||
assert "input env files have changed" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_copy_env_files_twice_with_name_change(tmp_output: Path, tmp_recipes: Path, tmp_path: Path):
|
||||
# copy env files to tmp_path
|
||||
assert len(list(tmp_path.iterdir())) == 0
|
||||
for f in ENV_PATHS:
|
||||
shutil.copy(f, tmp_path / f.name)
|
||||
ENV_PATHS_NEW = list(tmp_path.iterdir())
|
||||
assert len(ENV_PATHS_NEW) > 0
|
||||
|
||||
# create dirs in output
|
||||
DIR = DirManager(output_dir=tmp_output, session_id="abc", recipes_dir=tmp_recipes)
|
||||
DIR.create_all_dirs()
|
||||
|
||||
# confirm dir is empty
|
||||
assert len(list(DIR.ENV_FILES.iterdir())) == 0
|
||||
|
||||
# copy env files from tmp_path to tmp_output
|
||||
env_files = EnvManager._get_env_files(ENV_PATHS_NEW)
|
||||
EnvManager.copy_env_files(env_files, DIR)
|
||||
|
||||
# check that each env file is present in DIR.ENV_FILES with correct contents
|
||||
assert len(list(DIR.ENV_FILES.iterdir())) == len(env_files)
|
||||
|
||||
# change name of one env_file in tmp_path
|
||||
file_path = next(tmp_path.iterdir())
|
||||
file_path.rename(file_path.parent / (file_path.stem + "-other" + file_path.suffix))
|
||||
|
||||
# copy env files from tmp_path to tmp_output again
|
||||
with pytest.raises(AssertionError) as excinfo:
|
||||
env_files = EnvManager._get_env_files(list(tmp_path.iterdir()))
|
||||
EnvManager.copy_env_files(env_files, DIR)
|
||||
|
||||
assert "input env files have changed" in str(excinfo.value)
|
||||
Loading…
Add table
Add a link
Reference in a new issue