Skip to content

Commit

Permalink
Merge pull request #41 from jpower432/chore/update-logging
Browse files Browse the repository at this point in the history
refactor: updates logging to import log logic from trestle
  • Loading branch information
Alex Flom committed Sep 1, 2023
2 parents 8a7c4f2 + e319263 commit e6e0d6f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 158 deletions.
44 changes: 24 additions & 20 deletions tests/trestlebot/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

"""Test for CLI"""

import logging
import sys
from typing import List
from unittest.mock import patch
Expand Down Expand Up @@ -47,7 +48,7 @@ def args_dict_to_list(args_dict: dict) -> List[str]:
return args


def test_invalid_oscal_model(monkeypatch, valid_args_dict, capsys):
def test_invalid_oscal_model(monkeypatch, valid_args_dict, caplog):
"""Test invalid oscal model"""
args_dict = valid_args_dict
args_dict["oscal-model"] = "fake"
Expand All @@ -56,14 +57,15 @@ def test_invalid_oscal_model(monkeypatch, valid_args_dict, capsys):
with pytest.raises(SystemExit):
cli_main()

captured = capsys.readouterr()
assert (
"Invalid value fake for oscal model. Please use catalog, profile, compdef, or ssp."
in captured.err
assert any(
record.levelno == logging.ERROR
and record.message
== "Invalid value fake for oscal model. Please use catalog, profile, compdef, or ssp."
for record in caplog.records
)


def test_no_ssp_index(monkeypatch, valid_args_dict, capsys):
def test_no_ssp_index(monkeypatch, valid_args_dict, caplog):
"""Test missing index file for ssp"""
args_dict = valid_args_dict
args_dict["oscal-model"] = "ssp"
Expand All @@ -73,12 +75,14 @@ def test_no_ssp_index(monkeypatch, valid_args_dict, capsys):
with pytest.raises(SystemExit):
cli_main()

captured = capsys.readouterr()

assert "Must set ssp_index_path when using SSP as oscal model." in captured.err
assert any(
record.levelno == logging.ERROR
and record.message == "Must set ssp_index_path when using SSP as oscal model."
for record in caplog.records
)


def test_no_markdown_path(monkeypatch, valid_args_dict, capsys):
def test_no_markdown_path(monkeypatch, valid_args_dict, caplog):
"""Test without a markdown file passed as a flag"""
args_dict = valid_args_dict
args_dict["markdown-path"] = ""
Expand All @@ -87,12 +91,14 @@ def test_no_markdown_path(monkeypatch, valid_args_dict, capsys):
with pytest.raises(SystemExit):
cli_main()

captured = capsys.readouterr()

assert "Must set markdown path with oscal model." in captured.err
assert any(
record.levelno == logging.ERROR
and record.message == "Must set markdown path with oscal model."
for record in caplog.records
)


def test_with_target_branch(monkeypatch, valid_args_dict, capsys):
def test_with_target_branch(monkeypatch, valid_args_dict, caplog):
"""Test with target branch set an an unsupported Git provider"""
args_dict = valid_args_dict
args_dict["target-branch"] = "main"
Expand All @@ -106,13 +112,11 @@ def test_with_target_branch(monkeypatch, valid_args_dict, capsys):
with pytest.raises(SystemExit):
cli_main()

captured = capsys.readouterr()

expected_string = (
"target-branch flag is set with an unset git provider. "
assert any(
record.levelno == logging.ERROR
and record.message == "target-branch flag is set with an unset git provider. "
"To test locally, set the GITHUB_ACTIONS or GITLAB_CI environment variable."
for record in caplog.records
)

assert expected_string in captured.err

mock_check.assert_called_once()
2 changes: 1 addition & 1 deletion trestlebot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from trestlebot.tasks.base_task import TaskBase, TaskException


logger = logging.getLogger("trestle")
logger = logging.getLogger(__name__)


class RepoException(Exception):
Expand Down
6 changes: 4 additions & 2 deletions trestlebot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import sys
from typing import List, Optional

from trestlebot import bot, const, log
import trestle.common.log as log

from trestlebot import bot, const
from trestlebot.github import GitHub, is_github_actions
from trestlebot.gitlab import GitLab, get_gitlab_root_url, is_gitlab_ci
from trestlebot.provider import GitProvider
Expand All @@ -32,7 +34,7 @@
from trestlebot.tasks.regenerate_task import RegenerateTask


logger = logging.getLogger("trestle")
logger = logging.getLogger(__name__)


def _parse_cli_arguments() -> argparse.Namespace:
Expand Down
134 changes: 0 additions & 134 deletions trestlebot/log.py

This file was deleted.

2 changes: 1 addition & 1 deletion trestlebot/tasks/authored/ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)


logger = logging.getLogger("trestle")
logger = logging.getLogger(__name__)


class SSPIndex:
Expand Down

0 comments on commit e6e0d6f

Please sign in to comment.