Skip to content

Commit

Permalink
test: adds back unit tests for directory validation
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Sep 12, 2024
1 parent 23db548 commit 3ff376e
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions tests/trestlebot/entrypoints/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import pytest
from trestle.common.const import TRESTLE_CONFIG_DIR, TRESTLE_KEEP_FILE
from trestle.common.file_utils import is_hidden

from tests.testutils import args_dict_to_list, configure_test_logger, setup_for_init
from trestlebot.const import TRESTLEBOT_CONFIG_DIR
Expand Down Expand Up @@ -88,3 +89,81 @@ def test_call_trestle_init(tmp_init_dir: str) -> None:
trestle_dir = tmp_dir / pathlib.Path(TRESTLE_CONFIG_DIR)
keep_file = trestle_dir / pathlib.Path(TRESTLE_KEEP_FILE)
assert keep_file.exists() is True


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_init_compdef(
tmp_init_dir: str, args_dict: Dict[str, str], caplog: pytest.LogCaptureFixture
) -> None:
"""Tests for expected init command directories and files"""
args_dict["working-dir"] = tmp_init_dir
args_dict["oscal-model"] = model_types.AuthoredType.COMPDEF.value

setup_for_init(pathlib.Path(tmp_init_dir))

with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]):
with pytest.raises(SystemExit, match="0"):
cli_main()

# directories for compdef model should exist
tmp_dir = pathlib.Path(tmp_init_dir)
model_dirs = [d.name for d in tmp_dir.iterdir() if not is_hidden(d)]
expected = InitEntrypoint.MODEL_DIRS[args_dict["oscal-model"]] + [
InitEntrypoint.MARKDOWN_DIR
]
assert sorted(model_dirs) == sorted(expected)

# markdown directories should exist
markdown_dir = tmp_dir.joinpath(InitEntrypoint.MARKDOWN_DIR)
expected_subdirs = InitEntrypoint.MODEL_DIRS[args_dict["oscal-model"]]
markdown_subdirs = [f.name for f in markdown_dir.iterdir()]
assert sorted(markdown_subdirs) == sorted(expected_subdirs)

assert any(
record.levelno == logging.INFO
and f"Initialized trestlebot project successfully in {tmp_init_dir}"
in record.message
for record in caplog.records
)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_init_ssp(
tmp_init_dir: str, args_dict: Dict[str, str], caplog: pytest.LogCaptureFixture
) -> None:
"""Tests for expected init command directories and files"""
args_dict["working-dir"] = tmp_init_dir
args_dict["oscal-model"] = model_types.AuthoredType.SSP.value

setup_for_init(pathlib.Path(tmp_init_dir))

with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]):
with pytest.raises(SystemExit, match="0"):
cli_main()

# directories for compdef model should exist
tmp_dir = pathlib.Path(tmp_init_dir)
model_dirs = [d.name for d in tmp_dir.iterdir() if not is_hidden(d)]
expected = InitEntrypoint.MODEL_DIRS[args_dict["oscal-model"]] + [
InitEntrypoint.MARKDOWN_DIR
]
assert sorted(model_dirs) == sorted(expected)

# markdown directories should exist
markdown_dir = tmp_dir.joinpath(InitEntrypoint.MARKDOWN_DIR)
expected_subdirs = InitEntrypoint.MODEL_DIRS[args_dict["oscal-model"]]
markdown_subdirs = [f.name for f in markdown_dir.iterdir()]
assert sorted(markdown_subdirs) == sorted(expected_subdirs)

assert any(
record.levelno == logging.INFO
and f"Initialized trestlebot project successfully in {tmp_init_dir}"
in record.message
for record in caplog.records
)

0 comments on commit 3ff376e

Please sign in to comment.