from pathlib import Path import pytest from pytest_abra.coordinator import Coordinator from pytest_abra.env_manager import DependencyRule, EnvFile, EnvManager RECIPES_DIR = Path("./recipes").resolve() RUNNER_DICT = Coordinator.create_runner_dict(RECIPES_DIR) def test_complex_sorting() -> None: demo_rules = [ # X depends on Y DependencyRule("a", "e"), DependencyRule("b", "e"), DependencyRule("b", "f"), DependencyRule("c", "e"), DependencyRule("d", "e"), DependencyRule("f", "e"), ] demo_types = ["a", "b", "c", "d", "e", "f", "g"] env_files = [EnvFile(env_type=t, env_path=Path(), env_config=dict()) for t in demo_types] EnvManager.sort_env_files_by_rule sorted_env_files = EnvManager.sort_env_files_by_rule(env_files, demo_rules) assert sorted_env_files[0].env_type == "e" def test_circular_import() -> None: """This test will raise ValueError because the example input cannot be correctly ordered""" demo_rules = [ DependencyRule("a", "b"), DependencyRule("b", "c"), DependencyRule("c", "a"), ] demo_types = ["a", "b", "c"] env_files = [EnvFile(env_type=t, env_path=Path(), env_config=dict()) for t in demo_types] with pytest.raises(ValueError): EnvManager.sort_env_files_by_rule(env_files, demo_rules) def test_real_env_files() -> None: """authentik should be first""" ENV_FILES = [ Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik ] env_files: list[EnvFile] = EnvManager._get_env_files(ENV_FILES) dependency_rules: list[DependencyRule] = EnvManager._get_dependency_rules(env_files, RUNNER_DICT) sorted_env_files = EnvManager.sort_env_files_by_rule(env_files, dependency_rules) assert sorted_env_files[0].env_type == "authentik" def test_real_env_files_duplicate() -> None: """authentik should be first""" ENV_FILES = [ 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 ] env_files: list[EnvFile] = EnvManager._get_env_files(ENV_FILES) dependency_rules: list[DependencyRule] = EnvManager._get_dependency_rules(env_files, RUNNER_DICT) sorted_env_files = EnvManager.sort_env_files_by_rule(env_files, dependency_rules) assert sorted_env_files[0].env_type == "authentik" assert sorted_env_files[1].env_type == "authentik" assert sorted_env_files[2].env_type == "wordpress" def test_real_env_files_duplicate_six() -> None: """authentik should be first""" ENV_FILES = [ Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik 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 Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress ] env_files: list[EnvFile] = EnvManager._get_env_files(ENV_FILES) dependency_rules: list[DependencyRule] = EnvManager._get_dependency_rules(env_files, RUNNER_DICT) sorted_env_files = EnvManager.sort_env_files_by_rule(env_files, dependency_rules) assert sorted_env_files[0].env_type == "authentik" assert sorted_env_files[1].env_type == "authentik" assert sorted_env_files[2].env_type == "authentik" assert sorted_env_files[3].env_type == "wordpress" assert sorted_env_files[4].env_type == "wordpress" assert sorted_env_files[5].env_type == "wordpress" def test_env_manager() -> None: env_paths_list = [ 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 ] ENV = EnvManager(env_paths_list, RUNNER_DICT) assert ENV.env_files[0].env_type == "authentik" assert ENV.env_files[1].env_type == "authentik" assert ENV.env_files[2].env_type == "wordpress" def test_RUNNER_DICT_missing_key() -> None: """RUNNER_DICT missing wordpress key while .env file with TYPE=wordpress given""" env_paths_list = [ 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 ] RUNNER_DICT_COPY = RUNNER_DICT.copy() del RUNNER_DICT_COPY["wordpress"] with pytest.raises(AssertionError) as excinfo: EnvManager(env_paths_list, RUNNER_DICT_COPY) assert "no runner for" in str(excinfo.value)