From 117b13a89a9939dbe8aa0690aadc198f237fe491 Mon Sep 17 00:00:00 2001 From: codeasier Date: Fri, 18 Sep 2026 13:03:10 +0800 Subject: [PATCH] fix: return non-zero exit for missing --template-info templates typer.Exit() defaults to 0, so scripts and agent preflight could not tell a missing template from a successful lookup. --- src/pt_snap_cli/cli.py | 5 ++--- tests/test_cli.py | 5 ++--- tests/test_contract_cli_mcp.py | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/pt_snap_cli/cli.py b/src/pt_snap_cli/cli.py index 04f24a9..497cae2 100644 --- a/src/pt_snap_cli/cli.py +++ b/src/pt_snap_cli/cli.py @@ -413,9 +413,8 @@ def query_database( if template_info: try: info = query_service.get_template_info(template_info) - except TemplateNotFoundError: - typer.secho(f"Error: Template '{template_info}' not found", fg=typer.colors.RED) - raise typer.Exit() from None + except TemplateNotFoundError as e: + _error(str(e)) typer.secho(f"Template: {info.name}", fg=typer.colors.GREEN, bold=True) typer.echo(f"Description: {info.description}") diff --git a/tests/test_cli.py b/tests/test_cli.py index c787929..7c61261 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -322,9 +322,8 @@ class TestQueryCommandErrors: def test_query_template_info_not_found(self, sample_db: Path) -> None: """Test 'query --template-info' with non-existent template.""" result = runner.invoke(app, ["query", str(sample_db), "--template-info", "nonexistent"]) - assert result.exit_code == 0 - assert "Error" in result.stdout - assert "not found" in result.stdout + assert result.exit_code == 1 + assert "Error: Template 'nonexistent' not found" in result.stdout def test_query_without_template_use(self, sample_db: Path) -> None: """Test 'query' command without --template-use raises error.""" diff --git a/tests/test_contract_cli_mcp.py b/tests/test_contract_cli_mcp.py index 3863f97..9273c52 100644 --- a/tests/test_contract_cli_mcp.py +++ b/tests/test_contract_cli_mcp.py @@ -426,7 +426,7 @@ def test_missing_template_error_contract_matches_cli_and_mcp_semantics( ) -> None: template_name = "does_not_exist" cli_result = runner.invoke(app, ["query", "--template-info", template_name]) - assert cli_result.exit_code == 0 + assert cli_result.exit_code == 1 assert _normalize_cli_missing_template(cli_result.stdout) == mcp_server.get_template_info( template_name