Skip to content

Commit

Permalink
chore: adds trestle init to tmp_repo fixture
Browse files Browse the repository at this point in the history
Reduces code duplication in the end to end tests and any test that
has git and trestle checks

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Jan 4, 2024
1 parent 8a852af commit a9149de
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 48 deletions.
24 changes: 16 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]")
Expand Down
26 changes: 0 additions & 26 deletions tests/e2e/test_e2e_compdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

"""E2E tests for commands for component definition authoring."""

import argparse
import logging
import pathlib
import subprocess
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/test_e2e_ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
Any model specific test should be under workflows.
"""

import argparse
import logging
import os
import pathlib
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a9149de

Please sign in to comment.