From 2c2df3d589750ac1a8af763b139a2e21b70bb59c Mon Sep 17 00:00:00 2001 From: Qingmin Duanmu Date: Mon, 23 Dec 2024 11:08:37 +0800 Subject: [PATCH] fix: sys.exit with errorcode when exceptions --- tests/e2e/test_e2e_ssp.py | 7 ++- tests/trestlebot/cli/test_autosync_cmd.py | 4 +- trestlebot/cli/commands/autosync.py | 72 ++++++++++++----------- 3 files changed, 46 insertions(+), 37 deletions(-) diff --git a/tests/e2e/test_e2e_ssp.py b/tests/e2e/test_e2e_ssp.py index c63c8369..07940949 100644 --- a/tests/e2e/test_e2e_ssp.py +++ b/tests/e2e/test_e2e_ssp.py @@ -191,6 +191,9 @@ def test_ssp_e2e_editing_failure( autosync_command = e2e_runner.build_test_command( tmp_repo_str, "autosync", valid_args_dict ) - exit_code, _, response_stderr = e2e_runner.invoke_command(autosync_command) + exit_code, response_stdout, response_stderr = e2e_runner.invoke_command( + autosync_command + ) assert exit_code == ERROR_EXIT_CODE - assert "SSP test_ssp does not exists in the index" in response_stderr + err_msg = "SSP test_ssp does not exists in the index" + assert err_msg in response_stdout or err_msg in response_stderr diff --git a/tests/trestlebot/cli/test_autosync_cmd.py b/tests/trestlebot/cli/test_autosync_cmd.py index e0d370ec..e9916f68 100644 --- a/tests/trestlebot/cli/test_autosync_cmd.py +++ b/tests/trestlebot/cli/test_autosync_cmd.py @@ -87,8 +87,8 @@ def test_missing_markdown_dir_option(tmp_repo: Tuple[str, Repo]) -> None: assert result.exit_code == 2 assert "Error: Missing option '--markdown-dir'" in result.output - # With 'markdown_dir' setting in config.yml + # With non-existent '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 + assert result.exit_code == 1 diff --git a/trestlebot/cli/commands/autosync.py b/trestlebot/cli/commands/autosync.py index 19df0bb4..921752cc 100644 --- a/trestlebot/cli/commands/autosync.py +++ b/trestlebot/cli/commands/autosync.py @@ -5,11 +5,12 @@ import logging import sys +import traceback from typing import Any, List import click -from trestlebot.cli.options.common import common_options, git_options, handle_exceptions +from trestlebot.cli.options.common import common_options, git_options from trestlebot.cli.utils import comma_sep_to_list, run_bot from trestlebot.const import ERROR_EXIT_CODE from trestlebot.tasks.assemble_task import AssembleTask @@ -69,7 +70,6 @@ type=str, required=False, ) -@handle_exceptions def autosync_cmd(ctx: click.Context, **kwargs: Any) -> None: """Command to autosync catalog, profile, compdef and ssp.""" @@ -87,37 +87,43 @@ def autosync_cmd(ctx: click.Context, **kwargs: Any) -> None: if kwargs.get("file_pattern"): kwargs.update({"patterns": comma_sep_to_list(kwargs["file_patterns"])}) - model_filter: ModelFilter = ModelFilter( - skip_patterns=comma_sep_to_list(kwargs.get("skip_items", "")), - include_patterns=["*"], - ) - authored_object: AuthoredObjectBase = types.get_authored_object( - oscal_model, - working_dir, - kwargs.get("ssp_index_file", ""), - ) - - # Assuming an edit has occurred assemble would be run before regenerate. - if not kwargs.get("skip_assemble"): - assemble_task: AssembleTask = AssembleTask( - authored_object=authored_object, - markdown_dir=markdown_dir, - version=kwargs.get("version", ""), - model_filter=model_filter, + try: + model_filter: ModelFilter = ModelFilter( + skip_patterns=comma_sep_to_list(kwargs.get("skip_items", "")), + include_patterns=["*"], ) - pre_tasks.append(assemble_task) - else: - logger.info("Assemble task skipped.") - - if not kwargs.get("skip_regenerate"): - regenerate_task: RegenerateTask = RegenerateTask( - authored_object=authored_object, - markdown_dir=markdown_dir, - model_filter=model_filter, + authored_object: AuthoredObjectBase = types.get_authored_object( + oscal_model, + working_dir, + kwargs.get("ssp_index_file", ""), ) - pre_tasks.append(regenerate_task) - else: - logger.info("Regeneration task skipped.") - results = run_bot(pre_tasks, kwargs) - logger.debug(f"Trestlebot results: {results}") + # Assuming an edit has occurred assemble would be run before regenerate. + if not kwargs.get("skip_assemble"): + assemble_task: AssembleTask = AssembleTask( + authored_object=authored_object, + markdown_dir=markdown_dir, + version=kwargs.get("version", ""), + model_filter=model_filter, + ) + pre_tasks.append(assemble_task) + else: + logger.info("Assemble task skipped.") + + if not kwargs.get("skip_regenerate"): + regenerate_task: RegenerateTask = RegenerateTask( + authored_object=authored_object, + markdown_dir=markdown_dir, + model_filter=model_filter, + ) + pre_tasks.append(regenerate_task) + else: + logger.info("Regeneration task skipped.") + + results = run_bot(pre_tasks, kwargs) + logger.debug(f"Trestlebot results: {results}") + except Exception as e: + traceback_str = traceback.format_exc() + logger.error(f"Trestle-bot Error: {str(e)}") + logger.debug(traceback_str) + sys.exit(ERROR_EXIT_CODE)