Skip to content
Myr

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

3 files+38 added−2 removed

modified:src/click/testing.py+13 added-2 removed
225 unmodified lines
226 :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`.232233 .. versionchanged:: 8.2234 Added the ``catch_exceptions`` parameter.235236 .. versionchanged:: 8.2237 ``mix_stderr`` parameter has been removed.238 """239240 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_exceptions251252 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 not
161 unmodified lines
416 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 are
10 unmodified lines
435 :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 lines
464 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_exceptions470471 with self.isolation(input=input, env=env, color=color) as outstreams:472 return_value = None473 exception: BaseException | None = None474 exit_code = 0
81 unmodified lines
modified:tests/test_testing.py+22 added
182 unmodified lines
183 result = runner.invoke(cli)184 assert result.exit_code == 1185186187def test_catch_exceptions_cli_runner():188 """Test that invoke `catch_exceptions` takes the value from CliRunner if not set189 explicitly."""190191 class CustomError(Exception):192 pass193194 @click.command()195 def cli():196 raise CustomError(1)197198 runner = CliRunner(catch_exceptions=False)199200 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) == 3204205 with pytest.raises(CustomError):206 runner.invoke(cli)207208209def test_with_color():210 @click.command()211 def cli():212 click.secho("hello world", fg="blue")
240 unmodified lines
modified:CHANGES.rst+3 added
86 unmodified lines
87 ``(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.9091- 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`9495Version 8.1.896-------------97
1117 unmodified lines