The CLI runner provides functionality to invoke a Click command line script for unittesting purposes in a isolated environment. This only works in single-threaded systems without any concurrency as it changes the global interpreter state. :param charset: the character set for the i
| 315 | |
| 316 | |
| 317 | class CliRunner: |
| 318 | """The CLI runner provides functionality to invoke a Click command line |
| 319 | script for unittesting purposes in a isolated environment. This only |
| 320 | works in single-threaded systems without any concurrency as it changes the |
| 321 | global interpreter state. |
| 322 | |
| 323 | :param charset: the character set for the input and output data. |
| 324 | :param env: a dictionary with environment variables for overriding. |
| 325 | :param echo_stdin: if this is set to `True`, then reading from `<stdin>` writes |
| 326 | to `<stdout>`. This is useful for showing examples in |
| 327 | some circumstances. Note that regular prompts |
| 328 | will automatically echo the input. |
| 329 | :param catch_exceptions: Whether to catch any exceptions other than |
| 330 | ``SystemExit`` when running :meth:`~CliRunner.invoke`. |
| 331 | :param capture: Selects the output capture strategy. ``sys`` (default) |
| 332 | captures Python-level writes only and leaves |
| 333 | :meth:`sys.stdout.fileno` raising :exc:`io.UnsupportedOperation`, so |
| 334 | user code that calls :func:`os.dup2` on ``sys.stdout.fileno()`` cannot |
| 335 | clobber the host runner's stdout. ``fd`` redirects file descriptors |
| 336 | ``1`` and ``2`` via :func:`os.dup2` to a temporary file, also catching |
| 337 | output from stale stream references, C extensions, and subprocesses. |
| 338 | ``fd`` is not supported on Windows. |
| 339 | |
| 340 | .. versionchanged:: 8.4.0 |
| 341 | Added the ``capture`` parameter. The default ``sys`` mode no longer |
| 342 | exposes the original fd through :meth:`fileno`, reverting the change |
| 343 | introduced in ``8.3.3`` that broke Pytest's ``fd``-level capture |
| 344 | teardown. Use ``capture="fd"`` to restore that behavior with proper |
| 345 | isolation. :issue:`3384` |
| 346 | |
| 347 | .. versionchanged:: 8.2 |
| 348 | Added the ``catch_exceptions`` parameter. |
| 349 | |
| 350 | .. versionchanged:: 8.2 |
| 351 | ``mix_stderr`` parameter has been removed. |
| 352 | """ |
| 353 | |
| 354 | charset: str |
| 355 | env: cabc.Mapping[str, str | None] |
| 356 | echo_stdin: bool |
| 357 | catch_exceptions: bool |
| 358 | capture: CaptureMode |
| 359 | |
| 360 | def __init__( |
| 361 | self, |
| 362 | charset: str = "utf-8", |
| 363 | env: cabc.Mapping[str, str | None] | None = None, |
| 364 | echo_stdin: bool = False, |
| 365 | catch_exceptions: bool = True, |
| 366 | capture: CaptureMode = "sys", |
| 367 | ) -> None: |
| 368 | if capture not in {"sys", "fd"}: |
| 369 | raise ValueError( |
| 370 | f"capture={capture!r} is not valid. Choose from 'sys' or 'fd'." |
| 371 | ) |
| 372 | if capture == "fd" and sys.platform == "win32": |
| 373 | raise ValueError( |
| 374 | f"capture={capture!r} is not supported on Windows. Use 'sys'." |
no outgoing calls