Skip to content
Myr

Add catch_exceptions parameter to CliRunner.invoke

Before adoption: this commit came in with the history the project adopted, so no landing records it.

In many cases, a full traceback reaching back into the command is very helpful.

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parent 6ec3e65 · Browse files · 2 files+35 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+35 added−3 removed

  • tests:+23 added−0 removed
  • other:+12 added−3 removed

2 files+35 added−3 removed

modified:click/testing.py+12 added-3 removed
212 unmodified lines
213 click.termui.visible_prompt_func = old_visible_prompt_func214 click.termui.hidden_prompt_func = old_hidden_prompt_func215 click.termui._getchar = old__getchar_func216217 def invoke(self, cli, args=None, input=None, env=None, **extra):217 def invoke(self, cli, args=None, input=None, env=None,218 catch_exceptions=True, **extra):219 """Invokes a command in an isolated environment. The arguments are220 forwarded directly to the command line script, the `extra` keyword221 arguments are passed to the :meth:`~click.Command.main` function of222 the command.223224 This returns a :class:`Result` object.225226 .. versionadded:: 3.0227 The ``catch_exceptions`` parameter was added.228229 :param cli: the command to invoke230 :param args: the arguments to invoke231 :param input: the input data for `sys.stdin`.232 :param env: the environment overrides.233 :param catch_exceptions: Whether to catch any other exceptions than234 ``SystemExit``.235 :param extra: the keyword arguments to pass to :meth:`main`.236 """237 with self.isolation(input=input, env=env) as out:238 exception = None
6 unmodified lines
245 if e.code != 0:246 exception = e247 exit_code = e.code248 except Exception as e:249 if not catch_exceptions:250 raise251 exception = e252 exit_code = -1253 finally:245 sys.stdout.flush()246 output = out.getvalue()254 sys.stdout.flush()255 output = out.getvalue()256257 return Result(runner=self,258 output_bytes=output,259 exit_code=exit_code,
18 unmodified lines
modified:tests/test_testing.py+23 added
1import pytest2import click34from click.testing import CliRunner5
82 unmodified lines
88 runner = CliRunner()89 result = runner.invoke(continue_it, input='y')90 assert not result.exception91 assert result.output == 'y\n'929394def test_catch_exceptions():95 class CustomError(Exception):96 pass9798 @click.command()99 def cli():100 raise CustomError(1)101102 runner = CliRunner()103104 result = runner.invoke(cli)105 assert isinstance(result.exception, CustomError)106107 with pytest.raises(CustomError):108 runner.invoke(cli, catch_exceptions=False)109110 CustomError = SystemExit111112 result = runner.invoke(cli)113 assert result.exit_code == 1