Skip to content
Myr

Merge pull request #1312 from tom-dalton-fanduel/allow-testing-command-result-capture

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

Capture command return value in Result / CliRunner

authored by David Lord · committed by GitHub · parents 563b1a7, a7436e8 · Browse files · 3 files+45 added−12 removed
This commit

State

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

Diff stats

3 files+45 added−12 removed

  • tests:+13 added−0 removed
  • other:+32 added−12 removed

3 files+45 added−12 removed

modified:src/click/testing.py+29 added-12 removed
60 unmodified lines
61class Result:62 """Holds the captured result of an invoked CLI script."""6364 def __init__(65 self, runner, stdout_bytes, stderr_bytes, exit_code, exception, exc_info=None65 self,66 runner,67 stdout_bytes,68 stderr_bytes,69 return_value,70 exit_code,71 exception,72 exc_info=None,73 ):74 #: The runner that created the result75 self.runner = runner76 #: The standard output as bytes.77 self.stdout_bytes = stdout_bytes78 #: The standard error as bytes, or None if not available79 self.stderr_bytes = stderr_bytes80 #: The value returned from the invoked command.81 #:82 #: .. versionadded:: 8.083 self.return_value = return_value84 #: The exit code as integer.85 self.exit_code = exit_code86 #: The exception that happened if one did.87 self.exception = exception
191 unmodified lines
279 the command.280281 This returns a :class:`Result` object.282272 .. versionadded:: 3.0273 The ``catch_exceptions`` parameter was added.274275 .. versionchanged:: 3.0276 The result object now has an `exc_info` attribute with the277 traceback if available.278279 .. versionadded:: 4.0280 The ``color`` parameter was added.281283 :param cli: the command to invoke284 :param args: the arguments to invoke. It may be given as an iterable285 or a string. When given as string it will be interpreted286 as a Unix shell command. More details at
4 unmodified lines
291 ``SystemExit``.292 :param extra: the keyword arguments to pass to :meth:`main`.293 :param color: whether the output should contain color codes. The294 application can still override this explicitly.295296 .. versionchanged:: 8.0297 The result object has the ``return_value`` attribute with298 the value returned from the invoked command.299300 .. versionchanged:: 4.0301 Added the ``color`` parameter.302303 .. versionchanged:: 3.0304 Added the ``catch_exceptions`` parameter.305306 .. versionchanged:: 3.0307 The result object has the ``exc_info`` attribute with the308 traceback if available.309 """310 exc_info = None311 with self.isolation(input=input, env=env, color=color) as outstreams:312 return_value = None313 exception = None314 exit_code = 0315316 if isinstance(args, str):
4 unmodified lines
321 except KeyError:322 prog_name = self.get_default_prog_name(cli)323324 try:309 cli.main(args=args or (), prog_name=prog_name, **extra)325 return_value = cli.main(args=args or (), prog_name=prog_name, **extra)326 except SystemExit as e:327 exc_info = sys.exc_info()328 exit_code = e.code329 if exit_code is None:
24 unmodified lines
354 return Result(355 runner=self,356 stdout_bytes=stdout,357 stderr_bytes=stderr,358 return_value=return_value,359 exit_code=exit_code,360 exception=exception,361 exc_info=exc_info,362 )
17 unmodified lines
modified:tests/test_testing.py+13 added
293 unmodified lines
294 runner = CliRunner()295 result = runner.invoke(cli, prog_name="foobar")296 assert not result.exception297 assert result.output == "ok\n"298299300def test_command_standalone_mode_returns_value():301 @click.command()302 def cli():303 click.echo("ok")304 return "Hello, World!"305306 runner = CliRunner()307 result = runner.invoke(cli, standalone_mode=False)308 assert result.output == "ok\n"309 assert result.return_value == "Hello, World!"310 assert result.exit_code == 0
modified:CHANGES.rst+3 added
62 unmodified lines
63- Use ``Context.with_resource()`` to manage resources that would64 normally be used in a ``with`` statement, allowing them to be used65 across subcommands and callbacks, then cleaned up when the context66 ends. :pr:`1191`67- The result object returned by the test runner's ``invoke()`` method68 has a ``return_value`` attribute with the value returned by the69 invoked command. :pr:`1312`707172Version 7.1.273-------------
635 unmodified lines