diff options
author | Stephen Rosen <sirosen@uchicago.edu> | 2018-08-26 17:41:23 -0400 |
---|---|---|
committer | Stephen Rosen <sirosen@uchicago.edu> | 2018-08-26 18:41:13 -0400 |
commit | 1602436cd1bc65841ac0ac3b2ebe9ff94e7d1941 (patch) | |
tree | 7555681938004ab34ceeae2bb9e3f7f08ad71be2 /tests/test_context.py | |
parent | 225736b9eaa5cea30356d3a48e5c2bbe788e6298 (diff) | |
download | click-1602436cd1bc65841ac0ac3b2ebe9ff94e7d1941.tar.gz |
Simplify Exit Exception
= Refactor BaseCommand to simplify Exit handling
BaseCommand.main() will get the return value from `self.invoke` and
pass it to `ctx.exit()`, so that commands which `return 1` will
`ctx.exit(1)`
This is not a behavioral change -- `ctx.exit()` already does this.
Do not reraise Exit exceptions when invoked non-standalone. Instead,
return their exit codes as the result of `BaseCommand.main`. So, a
command which explicitly invokes `ctx.exit(0)` in its execution will
effectively `return 0` instead of raising a specialized exception.
= Make Exit its own RuntimeError subtype, don't pollute stdio on exit
Exit exceptions should not be a subtype of ClickException with the
output pretty-printing (`show()`) which happens when they are raised in
non-standalone mode. That would make `ctx.exit(...)` needlessly pollute
stderr. This would have downstream impact on everyone using context exit
calls, and generally be a Bad Thing (tm).
Instead, like Abort, Exit is a subclass of RuntimeError with very little
"meat on its bones". It's an exception containing a single integer (exit
code) which is then interpreted in that way in BaseCommand.main
closes #533
closes #667
Diffstat (limited to 'tests/test_context.py')
-rw-r--r-- | tests/test_context.py | 17 |
1 files changed, 4 insertions, 13 deletions
diff --git a/tests/test_context.py b/tests/test_context.py index cad2ae3..35933be 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -import pytest - import click @@ -195,6 +193,7 @@ def test_close_before_pop(runner): @click.pass_context def cli(ctx): ctx.obj = 'test' + @ctx.call_on_close def foo(): assert click.get_current_context().obj == 'test' @@ -243,25 +242,17 @@ def test_make_pass_decorator_args(runner): assert result.output == 'foocmd\n' -def test_exit_not_standalone_failure(): +def test_exit_not_standalone(): @click.command() @click.pass_context def cli(ctx): ctx.exit(1) - with pytest.raises(click.exceptions.Exit) as e: - cli.main([], 'test_exit_not_standalone', standalone_mode=False) - assert e.value.exit_code == 1 - + assert cli.main([], 'test_exit_not_standalone', standalone_mode=False) == 1 -def test_exit_not_standalone_success(): @click.command() @click.pass_context def cli(ctx): ctx.exit(0) - assert cli.main( - [], - 'test_exit_not_standalone', - standalone_mode=False, - ) is None + assert cli.main([], 'test_exit_not_standalone', standalone_mode=False) == 0 |