Skip to content

Commit

Permalink
test: update cli unit tests to read error from log
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Aug 28, 2023
1 parent 2626bf1 commit e319263
Showing 1 changed file with 24 additions and 20 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()

0 comments on commit e319263

Please sign in to comment.