File tree Expand file tree Collapse file tree
src/schnee/controllers/cli
tests/schnee/controllers/cli Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11from __future__ import annotations
22
3- from typing import TYPE_CHECKING
3+ from typing import TYPE_CHECKING , NoReturn
44
55import typer
66
1010SERVICE_ERROR_EXIT_CODE = 1
1111
1212
13- def exit_for_service_error (exc : ServiceError ) -> typer . Exit :
14- """Render a service-level error and return a CLI exit exception ."""
13+ def exit_for_service_error (exc : ServiceError ) -> NoReturn :
14+ """Render a service-level error and exit the CLI."""
1515 typer .echo (exc .msg , err = True )
16- return typer .Exit (code = SERVICE_ERROR_EXIT_CODE )
16+ raise typer .Exit (code = SERVICE_ERROR_EXIT_CODE )
Original file line number Diff line number Diff line change 1+ """Tests for CLI error handling."""
2+
3+ import pytest
4+ import typer
5+
6+ from schnee .controllers .cli .errors import (
7+ SERVICE_ERROR_EXIT_CODE ,
8+ exit_for_service_error ,
9+ )
10+ from schnee .services .base import ServiceError
11+
12+
13+ class ExampleServiceError (ServiceError ):
14+ """Service error used by CLI tests."""
15+
16+ msg = "example service failure"
17+
18+
19+ def test_exit_for_service_error_writes_error_and_exits (
20+ capsys : pytest .CaptureFixture [str ],
21+ ) -> None :
22+ """Service errors are rendered to stderr and terminate the CLI."""
23+ with pytest .raises (typer .Exit ) as exc_info :
24+ exit_for_service_error (ExampleServiceError ())
25+
26+ captured = capsys .readouterr ()
27+
28+ assert exc_info .value .exit_code == SERVICE_ERROR_EXIT_CODE , (
29+ "service errors should terminate with the configured CLI error code"
30+ )
31+ assert ExampleServiceError .msg in captured .err , (
32+ "service errors should be rendered to stderr before exiting"
33+ )
You can’t perform that action at this time.
0 commit comments