-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into update-actions
- Loading branch information
Showing
22 changed files
with
1,947 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# Copyright (c) 2024 Red Hat, Inc. | ||
|
||
|
||
"""Testing module for trestlebot autosync command""" | ||
import pathlib | ||
from typing import Tuple | ||
|
||
from click.testing import CliRunner | ||
from git import Repo | ||
|
||
from trestlebot.cli.commands.autosync import autosync_cmd | ||
from trestlebot.cli.config import TrestleBotConfig, write_to_file | ||
|
||
|
||
def test_invalid_oscal_model(tmp_repo: Tuple[str, Repo]) -> None: | ||
"""Test invalid OSCAl model option.""" | ||
|
||
repo_path, _ = tmp_repo | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
autosync_cmd, | ||
[ | ||
"--oscal-model", | ||
"invalid", | ||
"--repo-path", | ||
repo_path, | ||
"--markdown-dir", | ||
"markdown", | ||
"--branch", | ||
"main", | ||
"--committer-name", | ||
"Test User", | ||
"--committer-email", | ||
"[email protected]", | ||
], | ||
) | ||
assert "Invalid value for '--oscal-model'" in result.output | ||
assert result.exit_code == 2 | ||
|
||
|
||
def test_missing_ssp_index_file_option(tmp_repo: Tuple[str, Repo]) -> None: | ||
"""Test missing ssp_index_file option for autosync ssp.""" | ||
repo_path, _ = tmp_repo | ||
runner = CliRunner() | ||
cmd_options = [ | ||
"--oscal-model", | ||
"ssp", | ||
"--repo-path", | ||
repo_path, | ||
"--markdown-dir", | ||
"markdown", | ||
"--branch", | ||
"main", | ||
"--committer-name", | ||
"Test User", | ||
"--committer-email", | ||
"[email protected]", | ||
] | ||
result = runner.invoke(autosync_cmd, cmd_options) | ||
assert result.exit_code == 1 | ||
assert "Missing option '--ssp-index-file'" in result.output | ||
|
||
|
||
def test_missing_markdown_dir_option(tmp_repo: Tuple[str, Repo]) -> None: | ||
# When no markdown_dir setting in trestlebot config file. | ||
repo_path, _ = tmp_repo | ||
runner = CliRunner() | ||
filepath = pathlib.Path(repo_path).joinpath("config.yml") | ||
config_obj = TrestleBotConfig(repo_path=repo_path) | ||
write_to_file(config_obj, filepath) | ||
cmd_options = [ | ||
"--oscal-model", | ||
"compdef", | ||
"--repo-path", | ||
repo_path, | ||
"--branch", | ||
"main", | ||
"--committer-name", | ||
"Test User", | ||
"--committer-email", | ||
"[email protected]", | ||
"--config", | ||
str(filepath), | ||
] | ||
result = runner.invoke(autosync_cmd, cmd_options) | ||
assert result.exit_code == 2 | ||
assert "Error: Missing option '--markdown-dir'" in result.output | ||
|
||
# With 'markdown_dir' setting in config.yml | ||
config_obj = TrestleBotConfig(markdown_dir="markdown") | ||
write_to_file(config_obj, filepath) | ||
result = runner.invoke(autosync_cmd, cmd_options) | ||
assert result.exit_code == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# Copyright (c) 2024 Red Hat, Inc. | ||
|
||
|
||
"""Unit tests for CLI config module""" | ||
import pathlib | ||
|
||
import pytest | ||
import yaml | ||
|
||
from trestlebot.cli.config import ( | ||
TrestleBotConfig, | ||
TrestleBotConfigError, | ||
UpstreamsConfig, | ||
load_from_file, | ||
make_config, | ||
write_to_file, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def config_obj() -> TrestleBotConfig: | ||
return TrestleBotConfig( | ||
repo_path="/tmp", | ||
markdown_dir="markdown", | ||
upstreams=UpstreamsConfig(sources=["repo@main"]), | ||
) | ||
|
||
|
||
def test_invalid_config_raises_errors() -> None: | ||
"""Test create config with invalid directory to raise error.""" | ||
|
||
with pytest.raises(TrestleBotConfigError) as ex: | ||
_ = make_config(dict(repo_path="0")) | ||
|
||
assert ( | ||
str(ex.value) | ||
== "Invalid config value for repo_path. Path does not point to a directory." | ||
) | ||
|
||
|
||
def test_make_config_raises_no_errors(tmp_init_dir: str) -> None: | ||
"""Test create a valid config object.""" | ||
values = { | ||
"repo_path": tmp_init_dir, | ||
"markdown_dir": "markdown", | ||
"committer_name": "committer-name", | ||
"committer_email": "committer-email", | ||
"upstreams": {"sources": ["https://test@main"], "skip_validation": True}, | ||
} | ||
config = make_config(values) | ||
assert isinstance(config, TrestleBotConfig) | ||
assert config.upstreams is not None | ||
assert config.upstreams.sources == ["https://test@main"] | ||
assert config.upstreams.skip_validation is True | ||
assert config.repo_path == pathlib.Path(tmp_init_dir) | ||
assert config.markdown_dir == values["markdown_dir"] | ||
assert config.committer_name == values["committer_name"] | ||
assert config.committer_email == values["committer_email"] | ||
|
||
|
||
def test_config_write_to_file(config_obj: TrestleBotConfig, tmp_init_dir: str) -> None: | ||
"""Test config is written to yaml file.""" | ||
filepath = pathlib.Path(tmp_init_dir).joinpath("config.yml") | ||
write_to_file(config_obj, filepath) | ||
with open(filepath, "r") as f: | ||
yaml_data = yaml.safe_load(f) | ||
|
||
assert yaml_data == config_obj.to_yaml_dict() | ||
|
||
|
||
def test_config_load_from_file(config_obj: TrestleBotConfig, tmp_init_dir: str) -> None: | ||
"""Test config is read from yaml file into config object.""" | ||
filepath = pathlib.Path(tmp_init_dir).joinpath("config.yml") | ||
with filepath.open("w") as config_file: | ||
yaml.dump(config_obj.to_yaml_dict(), config_file) | ||
|
||
config = load_from_file(filepath) | ||
assert isinstance(config, TrestleBotConfig) | ||
assert config == config_obj |
Oops, something went wrong.