Add CliRunner default `catch_exceptions` parameter (#2818)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Éloi Rivard · committed by GitHub · parent 5961d31 · Browse files · 3 files·+38 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com>
- Co-authored-by: Andreas Backx <andreas@backx.org>
Diff stats
3 files+38 added−2 removed
- tests:+22 added−0 removed
- other:+16 added−2 removed
Provenance
- Commit
88328fc- Tree
954ee2a- Author
Éloi Rivard <eloi.rivard@nubla.fr>- Committer
GitHub <noreply@github.com>- Parent
5961d31
3 files changed+38 added−2 removed
modified:src/click/testing.py+13 added-2 removed
225 unmodified lines226 :param echo_stdin: if this is set to `True`, then reading from `<stdin>` writes227 to `<stdout>`. This is useful for showing examples in228 some circumstances. Note that regular prompts229 will automatically echo the input.230+ :param catch_exceptions: Whether to catch any exceptions other than231+ ``SystemExit`` when running :meth:`~CliRunner.invoke`.232 233 .. versionchanged:: 8.2234+ Added the ``catch_exceptions`` parameter.235+236+ .. versionchanged:: 8.2237 ``mix_stderr`` parameter has been removed.238 """239 240 def __init__(241 self,242 charset: str = "utf-8",243 env: cabc.Mapping[str, str | None] | None = None,244 echo_stdin: bool = False,245+ catch_exceptions: bool = True,246 ) -> None:247 self.charset = charset248 self.env: cabc.Mapping[str, str | None] = env or {}249 self.echo_stdin = echo_stdin250+ self.catch_exceptions = catch_exceptions251 252 def get_default_prog_name(self, cli: Command) -> str:253 """Given a command object it will return the default program name254 for it. The default is the `name` attribute or ``"root"`` if not161 unmodified lines416 cli: Command,417 args: str | cabc.Sequence[str] | None = None,418 input: str | bytes | t.IO[t.Any] | None = None,419 env: cabc.Mapping[str, str | None] | None = None,413- catch_exceptions: bool = True,420+ catch_exceptions: bool | None = None,421 color: bool = False,422 **extra: t.Any,423 ) -> Result:424 """Invokes a command in an isolated environment. The arguments are10 unmodified lines435 :func:`shlex.split`.436 :param input: the input data for `sys.stdin`.437 :param env: the environment overrides.438 :param catch_exceptions: Whether to catch any other exceptions than432- ``SystemExit``.439+ ``SystemExit``. If :data:`None`, the value440+ from :class:`CliRunner` is used.441 :param extra: the keyword arguments to pass to :meth:`main`.442 :param color: whether the output should contain color codes. The443 application can still override this explicitly.444 19 unmodified lines464 The result object has the ``exc_info`` attribute with the465 traceback if available.466 """467 exc_info = None468+ if catch_exceptions is None:469+ catch_exceptions = self.catch_exceptions470+471 with self.isolation(input=input, env=env, color=color) as outstreams:472 return_value = None473 exception: BaseException | None = None474 exit_code = 081 unmodified linesmodified:tests/test_testing.py+22 added
182 unmodified lines183 result = runner.invoke(cli)184 assert result.exit_code == 1185 186 187+def test_catch_exceptions_cli_runner():188+ """Test that invoke `catch_exceptions` takes the value from CliRunner if not set189+ explicitly."""190+191+ class CustomError(Exception):192+ pass193+194+ @click.command()195+ def cli():196+ raise CustomError(1)197+198+ runner = CliRunner(catch_exceptions=False)199+200+ result = runner.invoke(cli, catch_exceptions=True)201+ assert isinstance(result.exception, CustomError)202+ assert type(result.exc_info) is tuple203+ assert len(result.exc_info) == 3204+205+ with pytest.raises(CustomError):206+ runner.invoke(cli)207+208+209 def test_with_color():210 @click.command()211 def cli():212 click.secho("hello world", fg="blue")240 unmodified linesmodified:CHANGES.rst+3 added
86 unmodified lines87 ``(Deprecated) help`` to ``help (DEPRECATED)``.88 - Parameters cannot be required nor prompted or an error is raised.89 - A warning will be printed when something deprecated is used.90 91+- Add a ``catch_exceptions`` parameter to :class:`CliRunner`. If92+ ``catch_exceptions`` is not passed to :meth:`CliRunner.invoke`,93+ the value from :class:`CliRunner`. :issue:`2817` :pr:`2818`94 95 Version 8.1.896 -------------97 1117 unmodified lines