test(cli): cover UsageError path in main() and drop redundant handler
The mro-based UsageError check inside the Exception block already catches BadParameter (it inherits from UsageError) — the separate typer.BadParameter handler was redundant. Added an in-process Behave step that calls main() directly and captures err_console output, plus a scenario that runs `plan use --no-such-flag` to cover the UsageError branch (subprocess steps do not count toward unit-test coverage).
This commit is contained in:
@@ -35,3 +35,9 @@ Feature: CLI main() error handling paths
|
||||
Scenario: _print_basic_help prints without error
|
||||
When I call the main _print_basic_help
|
||||
Then the main _print_basic_help completes ok
|
||||
|
||||
@coverage
|
||||
Scenario: unknown option triggers UsageError handler with exit code 2
|
||||
When I call main with arguments ["plan", "use", "--no-such-flag", "x"]
|
||||
Then the main cli exit code should be 2
|
||||
And the main cli output contains "No such option"
|
||||
|
||||
@@ -85,6 +85,23 @@ def step_print_basic_help(context: Context) -> None:
|
||||
context.help_output = buf.getvalue()
|
||||
|
||||
|
||||
@when("I call main with arguments {args}")
|
||||
def step_main_in_process(context: Context, args: str) -> None:
|
||||
"""Call ``main(args)`` in-process so coverage tracks the exception paths."""
|
||||
from cleveragents.cli.main import get_err_console, main
|
||||
|
||||
cmd_args = ast.literal_eval(args) if isinstance(args, str) else []
|
||||
console = get_err_console()
|
||||
buf = StringIO()
|
||||
original_file = console.file
|
||||
console.file = buf
|
||||
try:
|
||||
context.exit_code = main(cmd_args)
|
||||
finally:
|
||||
console.file = original_file
|
||||
context.output = buf.getvalue()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Then steps — unique step texts to avoid collisions with other suites
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -813,10 +813,6 @@ def main(args: list[str] | None = None) -> int:
|
||||
err_console = get_err_console()
|
||||
err_console.print("\n[yellow]Interrupted by user[/yellow]")
|
||||
return 130
|
||||
except typer.BadParameter as e:
|
||||
err_console = get_err_console()
|
||||
err_console.print(f"Error: {e.format_message()}")
|
||||
return 2
|
||||
except Exception as e:
|
||||
if any(c.__name__ == "UsageError" for c in type(e).__mro__) and hasattr(
|
||||
e, "format_message"
|
||||
|
||||
Reference in New Issue
Block a user