Skip to content

Commit

Permalink
Merge pull request #12 from jpower432/test/cli
Browse files Browse the repository at this point in the history
test: adds cli.py testing to test invalid arg responses
  • Loading branch information
Alex Flom authored Jun 28, 2023
2 parents 48fb4db + d011804 commit a6bf0f1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/trestlebot/tasks/authored/test_ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_assemble(tmp_trestle_dir: str) -> None:


def test_assemble_no_ssp_entry(tmp_trestle_dir: str) -> None:
"""Test to test assemble functionality for SSPs"""
"""Test to trigger failure for missing SSP index"""
# Prepare the workspace and generate the markdown
trestle_root = pathlib.Path(tmp_trestle_dir)
md_path = f"{markdown_dir}/{test_ssp_output}"
Expand Down
5 changes: 5 additions & 0 deletions tests/trestlebot/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@


def test_stage_files(tmp_repo: Tuple[str, Repo]) -> None:
"""Test staging files by patterns"""
repo_path, repo = tmp_repo

# Create test files
Expand All @@ -48,6 +49,7 @@ def test_stage_files(tmp_repo: Tuple[str, Repo]) -> None:


def test_local_commit(tmp_repo: Tuple[str, Repo]) -> None:
"""Test local commit function"""
repo_path, repo = tmp_repo

# Create a test file
Expand Down Expand Up @@ -78,6 +80,7 @@ def test_local_commit(tmp_repo: Tuple[str, Repo]) -> None:


def test_local_commit_with_committer(tmp_repo: Tuple[str, Repo]) -> None:
"""Test setting committer information for commits"""
repo_path, repo = tmp_repo

# Create a test file
Expand Down Expand Up @@ -108,6 +111,7 @@ def test_local_commit_with_committer(tmp_repo: Tuple[str, Repo]) -> None:


def test_local_commit_with_author(tmp_repo: Tuple[str, Repo]) -> None:
"""Test setting author for commits"""
repo_path, repo = tmp_repo

# Create a test file
Expand Down Expand Up @@ -140,6 +144,7 @@ def test_local_commit_with_author(tmp_repo: Tuple[str, Repo]) -> None:


def test_run_dry_run(tmp_repo: Tuple[str, Repo]) -> None:
"""Test bot run with dry run"""
repo_path, repo = tmp_repo

# Create a test file
Expand Down
82 changes: 82 additions & 0 deletions tests/trestlebot/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/python

# Copyright 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Test for CLI"""

import logging
import sys
from typing import List

import pytest

from trestlebot.cli import run as cli_main


@pytest.fixture
def valid_args_dict() -> dict:
return {
"branch": "main",
"markdown-path": "/my/path",
"assemble-model": "profile",
"committer-name": "test",
"committer-email": "[email protected]",
"working-dir": "tmp",
"patterns": ".",
}


def args_dict_to_list(args_dict: dict) -> List[str]:
args = []
for k, v in args_dict.items():
args.append(f"--{k}")
if v is not None:
args.append(v)
return args


def test_invalid_assemble_model(monkeypatch, valid_args_dict, caplog):
"""Test invalid assemble model"""
args_dict = valid_args_dict
args_dict["assemble-model"] = "fake"
monkeypatch.setattr(sys, "argv", ["trestlebot", *args_dict_to_list(args_dict)])

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
)


def test_no_ssp_index(monkeypatch, valid_args_dict, caplog):
"""Test invalid assemble model"""
args_dict = valid_args_dict
args_dict["assemble-model"] = "ssp"
args_dict["ssp-index-path"] = ""
monkeypatch.setattr(sys, "argv", ["trestlebot", *args_dict_to_list(args_dict)])

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
)
4 changes: 2 additions & 2 deletions trestlebot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def run() -> None:
assembled_type = types.check_authored_type(args.assemble_model)
except ValueError:
logging.error(
f"Invalid value {args.assemble_model} for assemble model. \
Please use catalog, profile, compdef, or ssp."
f"Invalid value {args.assemble_model} for assemble model. "
f"Please use catalog, profile, compdef, or ssp."
)
sys.exit(1)

Expand Down

0 comments on commit a6bf0f1

Please sign in to comment.