Skip to content

Commit

Permalink
Merge branch 'main' into fix/e2e-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
beatrizmcouto authored Mar 28, 2024
2 parents 2d8563c + 05b0c4c commit d4a7f59
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test-code-cov:
# https://github.com/python-poetry/poetry/issues/994#issuecomment-831598242
# Check for CVEs locally. For continuous dependency updates, we use dependabot.
dep-cve-check:
@poetry export -f requirements.txt --without-hashes | poetry run safety check --stdin
@poetry export -f requirements.txt --without-hashes | poetry run safety check --continue-on-error --stdin
.PHONY: dep-cve-check

security-check:
Expand Down
13 changes: 13 additions & 0 deletions tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""Helper functions for unit test setup and teardown."""

import argparse
import logging
import pathlib
import shutil
import tempfile
Expand All @@ -21,6 +22,7 @@
from trestle.oscal import profile as prof

from trestlebot.const import YAML_EXTENSION
from trestlebot.entrypoints.log import configure_logger


JSON_TEST_DATA_PATH = pathlib.Path("tests/data/json/").resolve()
Expand All @@ -32,6 +34,17 @@
TEST_REMOTE_REPO_URL = "http://localhost:8080/test.git"


def configure_test_logger(level: int = logging.INFO) -> None:
"""
Configure the logger for testing.
Notes: This is used to patch the logger in tests
so the caplog can be used to capture log messages.
This does not happen when propagate is set to False.
"""
configure_logger(level=level, propagate=True)


def clean(repo_path: str, repo: Optional[Repo]) -> None:
"""Clean up the temporary Git repository."""
if repo is not None:
Expand Down
24 changes: 22 additions & 2 deletions tests/trestlebot/entrypoints/test_autosync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import argparse
import logging
from typing import Any, Dict
from unittest.mock import patch
from unittest.mock import Mock, patch

import pytest

from tests.testutils import args_dict_to_list
from tests.testutils import args_dict_to_list, configure_test_logger
from trestlebot.entrypoints.autosync import AutoSyncEntrypoint
from trestlebot.entrypoints.autosync import main as cli_main
from trestlebot.entrypoints.entrypoint_base import EntrypointInvalidArgException
Expand Down Expand Up @@ -60,6 +60,10 @@ def test_validate_args_invalid_model(valid_args_dict: Dict[str, str]) -> None:
auto_sync.validate_args(args)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
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 All @@ -77,6 +81,10 @@ def test_no_ssp_index(valid_args_dict: Dict[str, str], caplog: Any) -> None:
)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_no_markdown_path(valid_args_dict: Dict[str, str], caplog: Any) -> None:
"""Test without a markdown file passed as a flag"""
args_dict = valid_args_dict
Expand All @@ -92,6 +100,10 @@ def test_no_markdown_path(valid_args_dict: Dict[str, str], caplog: Any) -> None:
)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_non_existent_working_dir(valid_args_dict: Dict[str, str], caplog: Any) -> None:
"""Test with a non-existent working directory"""
args_dict = valid_args_dict
Expand All @@ -107,6 +119,10 @@ def test_non_existent_working_dir(valid_args_dict: Dict[str, str], caplog: Any)
)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_invalid_working_dir(valid_args_dict: Dict[str, str], caplog: Any) -> None:
"""Test with directory that is not a trestle project root"""
args_dict = valid_args_dict
Expand All @@ -122,6 +138,10 @@ def test_invalid_working_dir(valid_args_dict: Dict[str, str], caplog: Any) -> No
)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_with_target_branch(
tmp_trestle_dir: str, valid_args_dict: Dict[str, str], caplog: Any
) -> None:
Expand Down
12 changes: 10 additions & 2 deletions tests/trestlebot/entrypoints/test_create_cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import logging
import pathlib
from typing import Any, Dict
from unittest.mock import patch
from unittest.mock import Mock, patch

import pytest

from tests.testutils import args_dict_to_list, setup_for_compdef
from tests.testutils import args_dict_to_list, configure_test_logger, setup_for_compdef
from trestlebot.entrypoints.create_cd import main as cli_main


Expand Down Expand Up @@ -49,6 +49,10 @@ def test_create_cd_with_missing_args(
cli_main()


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_create_cd_with_missing_profile(
tmp_trestle_dir: str, valid_args_dict: Dict[str, str], caplog: Any
) -> None:
Expand All @@ -72,6 +76,10 @@ def test_create_cd_with_missing_profile(
)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_create_cd_with_missing_filter_profile(
tmp_trestle_dir: str, valid_args_dict: Dict[str, str], caplog: Any
) -> None:
Expand Down
17 changes: 15 additions & 2 deletions tests/trestlebot/entrypoints/test_create_ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
import logging
import pathlib
from typing import Any, Dict, Tuple
from unittest.mock import patch
from unittest.mock import Mock, patch

import pytest
from git import Repo

from tests.testutils import TEST_YAML_HEADER, args_dict_to_list, setup_for_ssp
from tests.testutils import (
TEST_YAML_HEADER,
args_dict_to_list,
configure_test_logger,
setup_for_ssp,
)
from trestlebot.entrypoints.create_ssp import main as cli_main


Expand Down Expand Up @@ -43,6 +48,10 @@ def base_args_dict() -> Dict[str, str]:
]


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_create_ssp(
tmp_repo: Tuple[str, Repo], base_args_dict: Dict[str, str], caplog: Any
) -> None:
Expand Down Expand Up @@ -78,6 +87,10 @@ def test_create_ssp(
)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_create_ssp_with_error(
tmp_repo: Tuple[str, Repo], base_args_dict: Dict[str, str], caplog: Any
) -> None:
Expand Down
13 changes: 11 additions & 2 deletions tests/trestlebot/entrypoints/test_sync_upstreams.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

import logging
from typing import Any, Dict, Tuple
from unittest.mock import patch
from unittest.mock import Mock, patch

import pytest
from git import Repo

from tests.testutils import args_dict_to_list, clean, prepare_upstream_repo
from tests.testutils import (
args_dict_to_list,
clean,
configure_test_logger,
prepare_upstream_repo,
)
from trestlebot.entrypoints.sync_upstreams import main as cli_main


Expand Down Expand Up @@ -122,6 +127,10 @@ def test_with_exclude_model_names(
clean(source, None)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_with_no_sources(valid_args_dict: Dict[str, str], caplog: Any) -> None:
"""Test with an invalid source argument."""
args_dict = valid_args_dict
Expand Down
15 changes: 10 additions & 5 deletions trestlebot/entrypoints/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import argparse
import logging
import sys
from typing import List

import trestle.common.log as log

Expand All @@ -26,10 +27,17 @@ def set_log_level_from_args(args: argparse.Namespace) -> None:
configure_logger(logging.INFO)


def configure_logger(level: int = logging.INFO) -> None:
def configure_logger(level: int = logging.INFO, propagate: bool = False) -> None:
"""Configure the logger."""
# Prevent extra message
_logger.propagate = propagate
_logger.setLevel(level=level)
for handler in configure_handlers():
_logger.addHandler(handler)


def configure_handlers() -> List[logging.Handler]:
"""Configure the handlers."""
# Create a StreamHandler to send non-error logs to stdout
stdout_info_handler = logging.StreamHandler(sys.stdout)
stdout_info_handler.setLevel(logging.INFO)
Expand All @@ -49,7 +57,4 @@ def configure_logger(level: int = logging.INFO) -> None:
)
stdout_debug_handler.setFormatter(detailed_formatter)
stderr_handler.setFormatter(detailed_formatter)

_logger.addHandler(stdout_debug_handler)
_logger.addHandler(stdout_info_handler)
_logger.addHandler(stderr_handler)
return [stdout_debug_handler, stdout_info_handler, stderr_handler]

0 comments on commit d4a7f59

Please sign in to comment.