From 8835243577de087c89850e14ae8f798c712817ff Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Dec 2023 15:26:56 +0100 Subject: [PATCH] move stuff to Envmanager --- src/coordinator.py | 41 +++------------------- src/{env_file_helper.py => env_manager.py} | 31 ++++++++++++++++ 2 files changed, 36 insertions(+), 36 deletions(-) rename src/{env_file_helper.py => env_manager.py} (57%) diff --git a/src/coordinator.py b/src/coordinator.py index 0baf934..6e7c035 100644 --- a/src/coordinator.py +++ b/src/coordinator.py @@ -1,28 +1,18 @@ import shutil from pathlib import Path -from dotenv import dotenv_values from loguru import logger from src.dirmanager import DirManager -from src.env_file_helper import DependencyRule, EnvFile, sort_env_files_by_rule +from src.env_manager import EnvFile, EnvManager from src.html_helper import merge_html_files from src.runner import Runner -from src.tests_authentik.runner_authentik import RunnerAuthentik -from src.tests_nextcloud.runner_nextcloud import RunnerNextcloud -from src.tests_wordpress.runner_wordpress import RunnerWordpress from src.utils import rmtree -# Register all runners here. Each .env file with TYPE=authentik will be run with RunnerAuthentik -RUNNER_DICT: dict[str, type[Runner]] = { - "authentik": RunnerAuthentik, - "wordpress": RunnerWordpress, - "nextcloud": RunnerNextcloud, -} - class Coordinator: def __init__(self, env_paths_list: list[Path], output_dir: Path, session_id: str) -> None: + # logging out_string = "".join([e.name + "\n" for e in env_paths_list]) out_string += f"output_dir = {output_dir}\n" out_string += f"session_id = {session_id}" @@ -33,30 +23,9 @@ class Coordinator: self.session_id = session_id # parse env files - self.env_files: list[EnvFile] = self._getn_env_files_list(env_paths_list) - self.dependency_rules: list[DependencyRule] = self._get_dependency_rules(self.env_files) - - @staticmethod - def _getn_env_files_list(env_paths: list[Path]) -> list[EnvFile]: - """Returns a list of EnvFile objects created from the given env files""" - env_files: list[EnvFile] = [] - for env_path in env_paths: - assert env_path.is_file(), f"the env file {env_path} does not exist" - config: dict[str, str] = dotenv_values(env_path) # type: ignore - assert "TYPE" in config, f"the env file {env_path} does not specify the required TYPE key." - env_type = config["TYPE"] - env_files.append(EnvFile(env_path=env_path, config=config, env_type=env_type)) - return env_files - - @staticmethod - def _get_dependency_rules(env_files: list[EnvFile]) -> list[DependencyRule]: - dependency_rules: list[DependencyRule] = [] - for env_file in env_files: - child_runner_class = RUNNER_DICT[env_file.env_type] - for dependency in child_runner_class.dependencies: - dependency_rule = DependencyRule(child=child_runner_class.name, dependency=dependency.name) - dependency_rules.append(dependency_rule) - return dependency_rules + # self.env_files: list[EnvFile] = self._getn_env_files_list(env_paths_list) + # self.dependency_rules: list[DependencyRule] = self._get_dependency_rules(self.env_files) + self.ENV = EnvManager(env_paths_list) def setup_test(self) -> None: logger.info("calling setup_test()") diff --git a/src/env_file_helper.py b/src/env_manager.py similarity index 57% rename from src/env_file_helper.py rename to src/env_manager.py index 9ce4f77..5bca1f6 100644 --- a/src/env_file_helper.py +++ b/src/env_manager.py @@ -1,6 +1,9 @@ from pathlib import Path from typing import NamedTuple +from dotenv import dotenv_values +from loguru import logger + class EnvFile(NamedTuple): env_path: Path @@ -56,3 +59,31 @@ def sort_env_files_by_rule(env_list: list[EnvFile], rules: list[DependencyRule]) raise ValueError( "Could not resolve test order. This is possibly due to a circular dependency (a on b, b on c, c on a)" ) + + +class EnvManager: + def __init__(self, env_paths_list: list[Path]): + self.env_files: list[EnvFile] = self._getn_env_files_list(env_paths_list) + self.dependency_rules: list[DependencyRule] = self._get_dependency_rules(self.env_files) + + @staticmethod + def _getn_env_files_list(env_paths: list[Path]) -> list[EnvFile]: + """Returns a list of EnvFile objects created from the given env files""" + env_files: list[EnvFile] = [] + for env_path in env_paths: + assert env_path.is_file(), f"the env file {env_path} does not exist" + config: dict[str, str] = dotenv_values(env_path) # type: ignore + assert "TYPE" in config, f"the env file {env_path} does not specify the required TYPE key." + env_type = config["TYPE"] + env_files.append(EnvFile(env_path=env_path, config=config, env_type=env_type)) + return env_files + + @staticmethod + def _get_dependency_rules(env_files: list[EnvFile]) -> list[DependencyRule]: + dependency_rules: list[DependencyRule] = [] + for env_file in env_files: + child_runner_class = RUNNER_DICT[env_file.env_type] + for dependency in child_runner_class.dependencies: + dependency_rule = DependencyRule(child=child_runner_class.name, dependency=dependency.name) + dependency_rules.append(dependency_rule) + return dependency_rules