Allow debugger interactions in tests (#3235)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 1339fd3, 4f9086b · Browse files · 4 files·+100 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+100 added−0 removed
- tests:+50 added−0 removed
- other:+50 added−0 removed
Provenance
- Commit
ac2dd7a- Tree
7eb8d5c- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
1339fd3- Parent 2
4f9086b
4 files changed+100 added−0 removed
modified:src/click/testing.py+45 added
2 unmodified lines3 import collections.abc as cabc4 import contextlib5 import io6 import os7+import pdb8 import shlex9 import sys10 import tempfile11 import typing as t378 unmodified lines390 old_hidden_prompt_func = termui.hidden_prompt_func391 old__getchar_func = termui._getchar392 old_should_strip_ansi = utils.should_strip_ansi # type: ignore393 old__compat_should_strip_ansi = _compat.should_strip_ansi394+ old_pdb_init = pdb.Pdb.__init__395 termui.visible_prompt_func = visible_input396 termui.hidden_prompt_func = hidden_input397 termui._getchar = _getchar398 utils.should_strip_ansi = should_strip_ansi # type: ignore399 _compat.should_strip_ansi = should_strip_ansi400 401+ def _patched_pdb_init(402+ self: pdb.Pdb,403+ completekey: str = "tab",404+ stdin: t.IO[str] | None = None,405+ stdout: t.IO[str] | None = None,406+ **kwargs: t.Any,407+ ) -> None:408+ """Default ``pdb.Pdb`` to real terminal streams during409+ ``CliRunner`` isolation.410+411+ Without this patch, ``pdb.Pdb.__init__`` inherits from412+ ``cmd.Cmd`` which falls back to ``sys.stdin``/``sys.stdout``413+ when no explicit streams are provided. During isolation414+ those are ``BytesIO``-backed wrappers, so the debugger415+ reads from an empty buffer and writes to captured output,416+ making interactive debugging impossible.417+418+ By defaulting to ``sys.__stdin__``/``sys.__stdout__`` (the419+ original terminal streams Python preserves regardless of420+ redirection), debuggers can interact with the user while421+ ``click.echo`` output is still captured normally.422+423+ This covers ``pdb.set_trace()``, ``breakpoint()``,424+ ``pdb.post_mortem()``, and debuggers that subclass425+ ``pdb.Pdb`` (ipdb, pdbpp). Explicit ``stdin``/``stdout``426+ arguments are honored and not overridden. Debuggers that427+ do not subclass ``pdb.Pdb`` (pudb, debugpy) are not428+ covered.429+430+ See: https://github.com/pallets/click/issues/654 and431+ https://github.com/pallets/click/issues/824432+ """433+ if stdin is None:434+ stdin = sys.__stdin__435+ if stdout is None:436+ stdout = sys.__stdout__437+ old_pdb_init(438+ self, completekey=completekey, stdin=stdin, stdout=stdout, **kwargs439+ )440+441+ pdb.Pdb.__init__ = _patched_pdb_init # type: ignore[assignment]442+443 old_env = {}444 try:445 for key, value in env.items():446 old_env[key] = os.environ.get(key)22 unmodified lines469 termui._getchar = old__getchar_func470 utils.should_strip_ansi = old_should_strip_ansi # type: ignore471 _compat.should_strip_ansi = old__compat_should_strip_ansi472 formatting.FORCED_WIDTH = old_forced_width473+ pdb.Pdb.__init__ = old_pdb_init # type: ignore[method-assign]474 475 def invoke(476 self,477 cli: Command,142 unmodified linesmodified:tests/test_testing.py+50 added
1 import os2+import pdb3 import sys4 from io import BytesIO5 6 import pytest462 unmodified lines469 print("gyarados gyarados gyarados", file=sys.stderr, end="", flush=False)470 471 result = runner.invoke(cli)472 assert result.stderr == "gyarados gyarados gyarados"473+474+475+def test_pdb_uses_real_streams():476+ """``pdb.Pdb()`` inside ``CliRunner`` defaults to real terminal streams477+ so that interactive debuggers work instead of reading from the478+ captured ``BytesIO`` stdin.479+ """480+481+ @click.command()482+ def cli():483+ debugger = pdb.Pdb()484+ assert debugger.stdin is sys.__stdin__485+ assert debugger.stdout is sys.__stdout__486+ click.echo("after debugger")487+488+ runner = CliRunner()489+ result = runner.invoke(cli, catch_exceptions=False)490+ assert result.output == "after debugger\n"491+492+493+def test_pdb_explicit_streams_honored():494+ """Explicit ``stdin``/``stdout`` arguments to ``pdb.Pdb()`` are not495+ overridden by the ``CliRunner`` patch.496+ """497+498+ @click.command()499+ def cli():500+ custom_in = sys.stdin501+ custom_out = sys.stdout502+ debugger = pdb.Pdb(stdin=custom_in, stdout=custom_out)503+ assert debugger.stdin is custom_in504+ assert debugger.stdout is custom_out505+506+ runner = CliRunner()507+ runner.invoke(cli, catch_exceptions=False)508+509+510+def test_pdb_init_restored_after_invoke():511+ """``pdb.Pdb.__init__`` is restored to its original after invoke."""512+ original = pdb.Pdb.__init__513+514+ @click.command()515+ def cli():516+ pass517+518+ runner = CliRunner()519+ runner.invoke(cli)520+521+ assert pdb.Pdb.__init__ is originalmodified:.pre-commit-config.yaml+1 added
11 unmodified lines121314151617181911 unmodified lines rev: 3e8a8703264a2f4a69428a0aa4dcb512790b2c8c # frozen: v6.0.0 hooks: - id: check-merge-conflict - id: debug-statements exclude: ^(src/click/testing\.py|tests/test_testing\.py)$ - id: fix-byte-order-marker - id: trailing-whitespace - id: end-of-file-fixermodified:CHANGES.rst+4 added
11 unmodified lines12 an object that doesn't support equality comparison with strings, such as13 ``semver.Version``. :issue:`3298` :pr:`3299`14 - Fix pager test pollution under parallel execution by using pytest's15 ``tmp_path`` fixture instead of a shared temporary file path. :pr:`3238`16+- Patch ``pdb.Pdb`` in ``CliRunner`` isolation so ``pdb.set_trace()``,17+ ``breakpoint()``, and debuggers subclassing ``pdb.Pdb`` (ipdb, pdbpp) can18+ interact with the real terminal instead of the captured I/O streams.19+ :issue:`654` :issue:`824` :issue:`843` :pr:`951` :pr:`3235`20 21 Version 8.3.222 -------------23 1323 unmodified lines