Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit tests for missed code paths #126

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/trestlebot/entrypoints/test_autosync.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@

"""Test for Autosync CLI"""

import argparse
import logging
from typing import Any, Dict
from unittest.mock import patch

import pytest

from tests.testutils import args_dict_to_list
from trestlebot.entrypoints.autosync import AutoSyncEntrypoint
from trestlebot.entrypoints.autosync import main as cli_main
from trestlebot.entrypoints.entrypoint_base import EntrypointInvalidArgException


@pytest.fixture
Expand All @@ -48,6 +51,26 @@ def test_invalid_oscal_model(valid_args_dict: Dict[str, str]) -> None:
cli_main()


def test_validate_args_invalid_model(valid_args_dict: Dict[str, str]) -> None:
"""
Test invalid oscal model with validate args function.
This is a separate test from test_invalid_oscal_model because
it args are make invalid after the args are parsed.
"""
args_dict = valid_args_dict
with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]):
with pytest.raises(
EntrypointInvalidArgException,
match="Invalid args --oscal-model: Invalid value fake. "
"Please use one of catalog, profile, ssp, compdef",
):
parser = argparse.ArgumentParser()
auto_sync = AutoSyncEntrypoint(parser=parser)
args = parser.parse_args()
args.oscal_model = "fake"
auto_sync.validate_args(args)


def test_no_ssp_index(valid_args_dict: Dict[str, str], caplog: Any) -> None:
"""Test missing index file for ssp"""
args_dict = valid_args_dict
Expand Down
11 changes: 11 additions & 0 deletions tests/trestlebot/entrypoints/test_entrypoint_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ def test_set_git_provider_with_none() -> None:
args = argparse.Namespace(target_branch=None, with_token=None)
provider = EntrypointBase.set_git_provider(args=args)
assert provider is None


def test_set_provider_with_no_token() -> None:
"""Test set_git_provider function with no token"""
args = argparse.Namespace(target_branch="main", with_token=None)
with pytest.raises(
EntrypointInvalidArgException,
match="Invalid args --with-token: "
"with-token flag must be set when using target-branch",
):
EntrypointBase.set_git_provider(args=args)
11 changes: 11 additions & 0 deletions tests/trestlebot/tasks/authored/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""Test author types for Trestlebot"""

import os
from unittest.mock import Mock

import pytest

Expand Down Expand Up @@ -98,3 +99,13 @@ def test_invalid_authored_type(tmp_trestle_dir: str) -> None:
match="Invalid authored type fake",
):
_ = types.get_authored_object("fake", tmp_trestle_dir, "")


def test_get_model_dir_with_invalid_type() -> None:
"""Test triggering an error with an invalid type when getting the model dir."""
with pytest.raises(
AuthoredObjectException,
match="Invalid authored object <class 'unittest.mock.Mock'>",
):
mock = Mock(spec=AuthoredObjectBase)
_ = types.get_trestle_model_dir(mock)
11 changes: 6 additions & 5 deletions trestlebot/entrypoints/autosync.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, parser: argparse.ArgumentParser) -> None:
"""Initialize."""
# Setup base arguments
super().__init__(parser)
self.supported_models: List[str] = [model.value for model in types.AuthoredType]
self.setup_autosync_arguments()

def setup_autosync_arguments(self) -> None:
Expand All @@ -66,7 +67,7 @@ def setup_autosync_arguments(self) -> None:
"--oscal-model",
required=True,
type=str,
choices=["catalog", "profile", "compdef", "ssp"],
choices=self.supported_models,
help="OSCAL model type to run tasks on.",
)
self.parser.add_argument(
Expand Down Expand Up @@ -98,12 +99,12 @@ def setup_autosync_arguments(self) -> None:

def validate_args(self, args: argparse.Namespace) -> None:
"""Validate the arguments for the autosync entrypoint."""
authored_list: List[str] = [model.value for model in types.AuthoredType]
if args.oscal_model not in authored_list:
supported_models_str = ", ".join(self.supported_models)
if args.oscal_model not in self.supported_models:
raise EntrypointInvalidArgException(
"--oscal-model",
f"Value {args.oscal_model} is not valid."
f"Please use one of {authored_list}",
f"Invalid value {args.oscal_model}. "
f"Please use one of {supported_models_str}",
)

if not args.markdown_path:
Expand Down
4 changes: 3 additions & 1 deletion trestlebot/tasks/authored/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,6 @@ def get_trestle_model_dir(authored_object: AuthoredObjectBase) -> str:
elif isinstance(authored_object, AuthoredSSP):
return const.MODEL_DIR_SSP
else:
raise AuthoredObjectException(f"Invalid authored object {authored_object}")
raise AuthoredObjectException(
f"Invalid authored object {type(authored_object)}"
)