Skip to content

Commit

Permalink
fix: sys.exit with errorcode when exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
qduanmu committed Dec 23, 2024
1 parent d701aab commit 2c2df3d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
7 changes: 5 additions & 2 deletions tests/e2e/test_e2e_ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions tests/trestlebot/cli/test_autosync_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
72 changes: 39 additions & 33 deletions trestlebot/cli/commands/autosync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""

Expand All @@ -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)

0 comments on commit 2c2df3d

Please sign in to comment.