Skip to content

Commit

Permalink
refactor: moves configure_test_logger to testutils
Browse files Browse the repository at this point in the history
The test_logger setup should not be packaged with the
trestlebot code

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Mar 26, 2024
1 parent 8909531 commit cdb39ec
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
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
3 changes: 1 addition & 2 deletions tests/trestlebot/entrypoints/test_autosync.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

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
from trestlebot.entrypoints.log import configure_test_logger


@pytest.fixture
Expand Down
3 changes: 1 addition & 2 deletions tests/trestlebot/entrypoints/test_create_cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

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
from trestlebot.entrypoints.log import configure_test_logger


@pytest.fixture
Expand Down
8 changes: 6 additions & 2 deletions tests/trestlebot/entrypoints/test_create_ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
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
from trestlebot.entrypoints.log import configure_test_logger


@pytest.fixture
Expand Down
8 changes: 6 additions & 2 deletions tests/trestlebot/entrypoints/test_sync_upstreams.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
import pytest
from git import Repo

from tests.testutils import args_dict_to_list, clean, prepare_upstream_repo
from trestlebot.entrypoints.log import configure_test_logger
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
18 changes: 2 additions & 16 deletions trestlebot/entrypoints/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,10 @@ 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 = False
_logger.setLevel(level=level)
for handler in configure_handlers():
_logger.addHandler(handler)


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.
"""
_logger.propagate = True
_logger.propagate = propagate
_logger.setLevel(level=level)
for handler in configure_handlers():
_logger.addHandler(handler)
Expand Down

0 comments on commit cdb39ec

Please sign in to comment.