|
1 | 1 | import re |
2 | 2 | import sys |
| 3 | +import types |
3 | 4 | from collections.abc import Generator |
4 | 5 | from pathlib import Path |
5 | 6 | from subprocess import run |
@@ -248,13 +249,36 @@ def test_inspect_error(mocker: MockerFixture): |
248 | 249 | assert str(v) == "<unknown>:0 (error parsing code, call stack too shallow)\n 'x' (str) len=1" |
249 | 250 |
|
250 | 251 |
|
251 | | -def test_breakpoint(mocker: MockerFixture): |
252 | | - # not much else we can do here |
| 252 | +def test_breakpoint_pdb(mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch): |
| 253 | + # a `None` entry in sys.modules makes the import raise ImportError whether or not IPython is installed |
| 254 | + monkeypatch.setitem(sys.modules, 'IPython.terminal.debugger', None) |
253 | 255 | mocked_set_trace = mocker.patch('pdb.Pdb.set_trace') |
254 | 256 | debug.breakpoint() |
255 | 257 | assert mocked_set_trace.called |
256 | 258 |
|
257 | 259 |
|
| 260 | +def test_breakpoint_ipython(mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch): |
| 261 | + ipython_set_trace = mocker.MagicMock() |
| 262 | + constructed_with: list[list[str]] = [] |
| 263 | + |
| 264 | + class FakeTerminalPdb: |
| 265 | + def __init__(self, skip: list[str]) -> None: |
| 266 | + constructed_with.append(skip) |
| 267 | + |
| 268 | + set_trace = ipython_set_trace |
| 269 | + |
| 270 | + fake_module = types.ModuleType('IPython.terminal.debugger') |
| 271 | + fake_module.TerminalPdb = FakeTerminalPdb # pyright: ignore[reportAttributeAccessIssue] |
| 272 | + monkeypatch.setitem(sys.modules, 'IPython.terminal.debugger', fake_module) |
| 273 | + mocked_pdb_set_trace = mocker.patch('pdb.Pdb.set_trace') |
| 274 | + |
| 275 | + debug.breakpoint() |
| 276 | + |
| 277 | + assert ipython_set_trace.called |
| 278 | + assert constructed_with == [['devtools.*']] |
| 279 | + assert not mocked_pdb_set_trace.called |
| 280 | + |
| 281 | + |
258 | 282 | @pytest.mark.xfail( |
259 | 283 | sys.platform == 'win32' and sys.version_info >= (3, 9), |
260 | 284 | reason='see https://github.com/alexmojaki/executing/issues/27', |
|
0 commit comments