add --exit-code flag

This commit is contained in:
2023-01-09 15:55:30 +02:00
parent 9760a055de
commit e3ee5de0e1
7 changed files with 39 additions and 4 deletions

View File

@ -10,7 +10,7 @@ echo -e '[arcanisrepo]\nServer = http://repo.arcanis.me/$arch\nSigLevel = Never'
# refresh the image # refresh the image
pacman --noconfirm -Syu pacman --noconfirm -Syu
# main dependencies # main dependencies
pacman --noconfirm -Sy base-devel devtools git pyalpm python-aur python-passlib python-setuptools python-srcinfo sudo pacman --noconfirm -Sy base-devel devtools git pyalpm python-cerberus python-inflection python-passlib python-requests python-setuptools python-srcinfo sudo
# make dependencies # make dependencies
pacman --noconfirm -Sy python-build python-installer python-wheel pacman --noconfirm -Sy python-build python-installer python-wheel
# optional dependencies # optional dependencies
@ -39,6 +39,8 @@ sed -i "s/handlers = syslog_handler/handlers = console_handler/g" /etc/ahriman.i
# initial setup command as root # initial setup command as root
[[ -z $MINIMAL_INSTALL ]] && WEB_ARGS=("--web-port" "8080") [[ -z $MINIMAL_INSTALL ]] && WEB_ARGS=("--web-port" "8080")
ahriman -a x86_64 repo-setup --packager "ahriman bot <ahriman@example.com>" --repository "github" "${WEB_ARGS[@]}" ahriman -a x86_64 repo-setup --packager "ahriman bot <ahriman@example.com>" --repository "github" "${WEB_ARGS[@]}"
# validate configuration
ahriman -a x86_64 repo-config-validate --exit-code
# enable services # enable services
systemctl enable ahriman-web@x86_64 systemctl enable ahriman-web@x86_64
systemctl enable ahriman@x86_64.timer systemctl enable ahriman@x86_64.timer

View File

@ -460,10 +460,15 @@ usage: ahriman repo\-config [\-h]
dump configuration for the specified architecture dump configuration for the specified architecture
.SH COMMAND \fI\,'ahriman repo\-config\-validate'\/\fR .SH COMMAND \fI\,'ahriman repo\-config\-validate'\/\fR
usage: ahriman repo\-config\-validate [\-h] usage: ahriman repo\-config\-validate [\-h] [\-e]
validate configuration and print found errors validate configuration and print found errors
.SH OPTIONS \fI\,'ahriman repo\-config\-validate'\/\fR
.TP
\fB\-e\fR, \fB\-\-exit\-code\fR
return non\-zero exit status if configuration is invalid
.SH COMMAND \fI\,'ahriman repo\-rebuild'\/\fR .SH COMMAND \fI\,'ahriman repo\-rebuild'\/\fR
usage: ahriman repo\-rebuild [\-h] [\-\-depends\-on DEPENDS_ON] [\-\-dry\-run] [\-\-from\-database] [\-e] usage: ahriman repo\-rebuild [\-h] [\-\-depends\-on DEPENDS_ON] [\-\-dry\-run] [\-\-from\-database] [\-e]

View File

@ -30,8 +30,8 @@ _shtab_ahriman_repo_clean_option_strings=('-h' '--help' '--cache' '--no-cache' '
_shtab_ahriman_clean_option_strings=('-h' '--help' '--cache' '--no-cache' '--chroot' '--no-chroot' '--manual' '--no-manual' '--packages' '--no-packages' '--pacman' '--no-pacman') _shtab_ahriman_clean_option_strings=('-h' '--help' '--cache' '--no-cache' '--chroot' '--no-chroot' '--manual' '--no-manual' '--packages' '--no-packages' '--pacman' '--no-pacman')
_shtab_ahriman_repo_config_option_strings=('-h' '--help') _shtab_ahriman_repo_config_option_strings=('-h' '--help')
_shtab_ahriman_config_option_strings=('-h' '--help') _shtab_ahriman_config_option_strings=('-h' '--help')
_shtab_ahriman_repo_config_validate_option_strings=('-h' '--help') _shtab_ahriman_repo_config_validate_option_strings=('-h' '--help' '-e' '--exit-code')
_shtab_ahriman_config_validate_option_strings=('-h' '--help') _shtab_ahriman_config_validate_option_strings=('-h' '--help' '-e' '--exit-code')
_shtab_ahriman_repo_rebuild_option_strings=('-h' '--help' '--depends-on' '--dry-run' '--from-database' '-e' '--exit-code') _shtab_ahriman_repo_rebuild_option_strings=('-h' '--help' '--depends-on' '--dry-run' '--from-database' '-e' '--exit-code')
_shtab_ahriman_rebuild_option_strings=('-h' '--help' '--depends-on' '--dry-run' '--from-database' '-e' '--exit-code') _shtab_ahriman_rebuild_option_strings=('-h' '--help' '--depends-on' '--dry-run' '--from-database' '-e' '--exit-code')
_shtab_ahriman_repo_remove_unknown_option_strings=('-h' '--help' '--dry-run') _shtab_ahriman_repo_remove_unknown_option_strings=('-h' '--help' '--dry-run')
@ -253,8 +253,12 @@ _shtab_ahriman_config__h_nargs=0
_shtab_ahriman_config___help_nargs=0 _shtab_ahriman_config___help_nargs=0
_shtab_ahriman_repo_config_validate__h_nargs=0 _shtab_ahriman_repo_config_validate__h_nargs=0
_shtab_ahriman_repo_config_validate___help_nargs=0 _shtab_ahriman_repo_config_validate___help_nargs=0
_shtab_ahriman_repo_config_validate__e_nargs=0
_shtab_ahriman_repo_config_validate___exit_code_nargs=0
_shtab_ahriman_config_validate__h_nargs=0 _shtab_ahriman_config_validate__h_nargs=0
_shtab_ahriman_config_validate___help_nargs=0 _shtab_ahriman_config_validate___help_nargs=0
_shtab_ahriman_config_validate__e_nargs=0
_shtab_ahriman_config_validate___exit_code_nargs=0
_shtab_ahriman_repo_rebuild__h_nargs=0 _shtab_ahriman_repo_rebuild__h_nargs=0
_shtab_ahriman_repo_rebuild___help_nargs=0 _shtab_ahriman_repo_rebuild___help_nargs=0
_shtab_ahriman_repo_rebuild___dry_run_nargs=0 _shtab_ahriman_repo_rebuild___dry_run_nargs=0

View File

@ -117,6 +117,7 @@ _shtab_ahriman_config_options=(
_shtab_ahriman_config_validate_options=( _shtab_ahriman_config_validate_options=(
"(- : *)"{-h,--help}"[show this help message and exit]" "(- : *)"{-h,--help}"[show this help message and exit]"
{-e,--exit-code}"[return non-zero exit status if configuration is invalid]"
) )
_shtab_ahriman_daemon_options=( _shtab_ahriman_daemon_options=(
@ -277,6 +278,7 @@ _shtab_ahriman_repo_config_options=(
_shtab_ahriman_repo_config_validate_options=( _shtab_ahriman_repo_config_validate_options=(
"(- : *)"{-h,--help}"[show this help message and exit]" "(- : *)"{-h,--help}"[show this help message and exit]"
{-e,--exit-code}"[return non-zero exit status if configuration is invalid]"
) )
_shtab_ahriman_repo_init_options=( _shtab_ahriman_repo_init_options=(

View File

@ -551,6 +551,8 @@ def _set_repo_config_validate_parser(root: SubParserAction) -> argparse.Argument
parser = root.add_parser("repo-config-validate", aliases=["config-validate"], help="validate system configuration", parser = root.add_parser("repo-config-validate", aliases=["config-validate"], help="validate system configuration",
description="validate configuration and print found errors", description="validate configuration and print found errors",
formatter_class=_formatter) formatter_class=_formatter)
parser.add_argument("-e", "--exit-code", help="return non-zero exit status if configuration is invalid",
action="store_true")
parser.set_defaults(handler=handlers.Validate, lock=None, report=False, quiet=True, unsafe=True) parser.set_defaults(handler=handlers.Validate, lock=None, report=False, quiet=True, unsafe=True)
return parser return parser

View File

@ -54,11 +54,15 @@ class Validate(Handler):
""" """
schema = Validate.schema(architecture, configuration) schema = Validate.schema(architecture, configuration)
validator = Validator(instance=configuration, schema=schema) validator = Validator(instance=configuration, schema=schema)
if validator.validate(configuration.dump()): if validator.validate(configuration.dump()):
return # no errors found return # no errors found
for node, errors in validator.errors.items(): for node, errors in validator.errors.items():
ValidationPrinter(node, errors).print(verbose=True) ValidationPrinter(node, errors).print(verbose=True)
# as we reach this part it means that we always have errors
Validate.check_if_empty(args.exit_code, True)
@staticmethod @staticmethod
def schema(architecture: str, configuration: Configuration) -> Dict[str, Any]: def schema(architecture: str, configuration: Configuration) -> Dict[str, Any]:
""" """

View File

@ -9,10 +9,25 @@ from ahriman.core.configuration.schema import CONFIGURATION_SCHEMA, GITREMOTE_RE
from ahriman.core.configuration.validator import Validator from ahriman.core.configuration.validator import Validator
def _default_args(args: argparse.Namespace) -> argparse.Namespace:
"""
default arguments for these test cases
Args:
args(argparse.Namespace): command line arguments fixture
Returns:
argparse.Namespace: generated arguments for these test cases
"""
args.exit_code = False
return args
def test_run(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None: def test_run(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
""" """
must run command must run command
""" """
args = _default_args(args)
mocker.patch.object(Validator, "errors", {"node": ["error"]}) mocker.patch.object(Validator, "errors", {"node": ["error"]})
print_mock = mocker.patch("ahriman.core.formatters.Printer.print") print_mock = mocker.patch("ahriman.core.formatters.Printer.print")
application_mock = mocker.patch("ahriman.core.configuration.validator.Validator.validate", return_value=False) application_mock = mocker.patch("ahriman.core.configuration.validator.Validator.validate", return_value=False)
@ -27,6 +42,7 @@ def test_run_skip(args: argparse.Namespace, configuration: Configuration, mocker
""" """
must skip print if no errors found must skip print if no errors found
""" """
args = _default_args(args)
mocker.patch("ahriman.core.configuration.validator.Validator.validate", return_value=True) mocker.patch("ahriman.core.configuration.validator.Validator.validate", return_value=True)
print_mock = mocker.patch("ahriman.core.formatters.Printer.print") print_mock = mocker.patch("ahriman.core.formatters.Printer.print")