Skip to content

Commit

Permalink
formatting and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Flom <[email protected]>
  • Loading branch information
afflom committed Jul 6, 2023
1 parent aec9a5d commit 66d7cd6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 23 deletions.
23 changes: 9 additions & 14 deletions tests/trestlebot/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

"""Test for CLI"""

import logging
import sys
from typing import List

Expand Down Expand Up @@ -47,7 +46,7 @@ def args_dict_to_list(args_dict: dict) -> List[str]:
return args


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

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


def test_no_ssp_index(monkeypatch, valid_args_dict, caplog):
def test_no_ssp_index(monkeypatch, valid_args_dict, capsys):
"""Test invalid assemble model"""
args_dict = valid_args_dict
args_dict["assemble-model"] = "ssp"
Expand All @@ -74,9 +72,6 @@ def test_no_ssp_index(monkeypatch, valid_args_dict, caplog):
with pytest.raises(SystemExit):
cli_main()

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

assert "Must set ssp_index_path when using SSP as assemble model." in captured.err
3 changes: 1 addition & 2 deletions trestlebot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
from trestlebot.tasks.base_task import TaskBase


logger = logging.getLogger('trestlebot')

logger = logging.getLogger("trestlebot")


def _parse_cli_arguments() -> argparse.Namespace:
Expand Down
23 changes: 16 additions & 7 deletions trestlebot/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
from types import TracebackType
from typing import Optional, Type


# Singleton logger instance
# All other CLI sub module will inherit settings of this logger as long as
# sub-module instantiates a logger with a prefix 'trestlebot' or __name__
_logger = logging.getLogger('trestlebot')
_logger = logging.getLogger("trestlebot")


class SpecificLevelFilter(logging.Filter):
"""
Filter for the same level as provided by setLevel for a log handler.
Python by default logs all levels above to a given destination. This makes it easy to split levels where you might
log all levels to file and only errors to std.err, however, does not allow logging a specific level elsewhere.
Python by default logs all levels above to a given destination.
This makes it easy to split levels where you might
log all levels to file and only errors to std.err,
however, does not allow logging a specific level elsewhere.
"""

def __init__(self, level: int) -> None:
Expand Down Expand Up @@ -67,8 +70,12 @@ def set_global_logging_levels(level: int = logging.INFO) -> None:
console_error_handler = logging.StreamHandler(sys.stderr)
console_error_handler.setLevel(logging.WARNING)
# create formatters
error_formatter = logging.Formatter('%(name)s:%(lineno)d %(levelname)s: %(message)s')
debug_formatter = logging.Formatter('%(name)s:%(lineno)d %(levelname)s: %(message)s')
error_formatter = logging.Formatter(
"%(name)s:%(lineno)d %(levelname)s: %(message)s"
)
debug_formatter = logging.Formatter(
"%(name)s:%(lineno)d %(levelname)s: %(message)s"
)
console_debug_handler.setFormatter(debug_formatter)
console_error_handler.setFormatter(error_formatter)
# add ch to logger
Expand All @@ -78,7 +85,9 @@ def set_global_logging_levels(level: int = logging.INFO) -> None:


def _exception_handler(
exception_type: Type[BaseException], exception: BaseException, traceback: Optional[TracebackType]
exception_type: Type[BaseException],
exception: BaseException,
traceback: Optional[TracebackType],
) -> None:
"""Empty exception handler to prevent stack traceback in quiet mode."""
logging.warning(exception)
Expand Down Expand Up @@ -111,7 +120,7 @@ def get_current_verbosity_level(logger: logging.Logger) -> int:
return 0


class Trace():
class Trace:
"""Class allowing low priority trace message when verbose > 1 and log level below DEBUG."""

def __init__(self, logger: logging.Logger) -> None:
Expand Down
1 change: 1 addition & 0 deletions trestlebot/tasks/authored/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
AuthorObjectBase,
)


class AuthoredCatalog(AuthorObjectBase):
"""
Functions for authoring OSCAL catalogs in automation
Expand Down
1 change: 1 addition & 0 deletions trestlebot/tasks/authored/compdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
AuthorObjectBase,
)


class AuthoredComponentsDefinition(AuthorObjectBase):
"""
Functions for authoring OSCAL Component Definitions in automation
Expand Down
1 change: 1 addition & 0 deletions trestlebot/tasks/authored/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
AuthorObjectBase,
)


class AuthoredProfile(AuthorObjectBase):
"""
Functions for authoring OSCAL Profiles in automation
Expand Down
1 change: 1 addition & 0 deletions trestlebot/tasks/authored/ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
AuthorObjectBase,
)


class AuthoredSSP(AuthorObjectBase):
"""
Functions for authoring OSCAL SSPs in automation
Expand Down

0 comments on commit 66d7cd6

Please sign in to comment.