From a9149dee0896cf860576368694426d805b8c2d9b Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Thu, 4 Jan 2024 17:15:49 -0500 Subject: [PATCH] chore: adds trestle init to tmp_repo fixture Reduces code duplication in the end to end tests and any test that has git and trestle checks Signed-off-by: Jennifer Power --- tests/conftest.py | 24 ++++++++++++++++-------- tests/e2e/test_e2e_compdef.py | 26 -------------------------- tests/e2e/test_e2e_ssp.py | 14 -------------- 3 files changed, 16 insertions(+), 48 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 8f0c6509..443e652b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -50,20 +50,28 @@ T = TypeVar("T") YieldFixture = Generator[T, None, None] -_TEST_CONTENTS = """ -test file -""" -_TEST_FILENAME = "test.txt" _TEST_PREFIX = "trestlebot_tests" @pytest.fixture(scope="function") def tmp_repo() -> YieldFixture[Tuple[str, Repo]]: - """Create a temporary git repository""" + """Create a temporary git repository with an initialized trestle workspace root""" with TemporaryDirectory(prefix=_TEST_PREFIX) as tmpdir: - test_file = os.path.join(tmpdir, _TEST_FILENAME) - with open(test_file, "x", encoding="utf8") as file: - file.write(_TEST_CONTENTS) + tmp_path = pathlib.Path(tmpdir) + try: + args = argparse.Namespace( + verbose=0, + trestle_root=tmp_path, + full=True, + local=False, + govdocs=False, + ) + init = InitCmd() + init._run(args) + except Exception as e: + raise TrestleError( + f"Initialization failed for temporary trestle directory: {e}." + ) repo = Repo.init(tmpdir) with repo.config_writer() as config: config.set_value("user", "email", "test@example.com") diff --git a/tests/e2e/test_e2e_compdef.py b/tests/e2e/test_e2e_compdef.py index b8f5c893..cc9d84a9 100644 --- a/tests/e2e/test_e2e_compdef.py +++ b/tests/e2e/test_e2e_compdef.py @@ -16,7 +16,6 @@ """E2E tests for commands for component definition authoring.""" -import argparse import logging import pathlib import subprocess @@ -25,7 +24,6 @@ import pytest from git.repo import Repo from trestle.common.model_utils import ModelUtils -from trestle.core.commands.init import InitCmd from trestle.core.models.file_content_type import FileContentType from trestle.oscal.component import ComponentDefinition from trestle.oscal.profile import Profile @@ -103,20 +101,8 @@ def test_rules_transform_e2e( logger.info(f"Running test: {test_name}") tmp_repo_str, repo = tmp_repo - tmp_repo_path = pathlib.Path(tmp_repo_str) - # Create a trestle workspace in the temporary git repository - args = argparse.Namespace( - verbose=0, - trestle_root=tmp_repo_path, - full=True, - local=False, - govdocs=False, - ) - init = InitCmd() - init._run(args) - # Setup the rules directory setup_rules_view(tmp_repo_path, test_comp_name) @@ -241,20 +227,8 @@ def test_create_cd_e2e( logger.info(f"Running test: {test_name}") tmp_repo_str, repo = tmp_repo - tmp_repo_path = pathlib.Path(tmp_repo_str) - # Create a trestle workspace in the temporary git repository - args = argparse.Namespace( - verbose=0, - trestle_root=tmp_repo_path, - full=True, - local=False, - govdocs=False, - ) - init = InitCmd() - init._run(args) - # Load profiles into the environment _ = setup_for_profile(tmp_repo_path, test_prof, "") load_from_json(tmp_repo_path, test_filter_prof, test_filter_prof, Profile) diff --git a/tests/e2e/test_e2e_ssp.py b/tests/e2e/test_e2e_ssp.py index 480a4321..bdad7c7e 100644 --- a/tests/e2e/test_e2e_ssp.py +++ b/tests/e2e/test_e2e_ssp.py @@ -21,7 +21,6 @@ Any model specific test should be under workflows. """ -import argparse import logging import os import pathlib @@ -32,7 +31,6 @@ from git.repo import Repo from trestle.common.model_utils import ModelUtils from trestle.core.commands.author.ssp import SSPGenerate -from trestle.core.commands.init import InitCmd from trestle.core.models.file_content_type import FileContentType from trestle.oscal.ssp import SystemSecurityPlan @@ -109,20 +107,8 @@ def test_ssp_editing_e2e( logger.info(f"Running test: {test_name}") tmp_repo_str, repo = tmp_repo - tmp_repo_path = pathlib.Path(tmp_repo_str) - # Create a trestle workspace in the temporary git repository - args = argparse.Namespace( - verbose=0, - trestle_root=tmp_repo_path, - full=True, - local=False, - govdocs=False, - ) - init = InitCmd() - init._run(args) - args = setup_for_ssp(tmp_repo_path, test_prof, [test_comp_name], test_ssp_md) # Create or generate the SSP