Skip to content

Commit

Permalink
Testing: Check that pdb magics start a recursive debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Oct 4, 2024
1 parent 4d727f0 commit 6e85699
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
38 changes: 35 additions & 3 deletions spyder/plugins/ipythonconsole/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Standard library imports
import os
import os.path as osp
from pathlib import Path
import sys
import threading
import traceback
Expand Down Expand Up @@ -203,26 +204,57 @@ def get_plugin(name):

debugger.get_plugin = get_plugin
debugger.on_ipython_console_available()

# Plugin setup
console.on_initialize()
console._register()
console.get_widget().matplotlib_status.register_ipythonconsole(console)

# Register handlers to run cells.
def get_file_code(fname, save_all=True):
"""
Get code from a file.
save_all is necessary to keep consistency with the handler registered
in the editor.
"""
path = Path(fname)
return path.read_text()

def get_cell(cell_id, fname):
"""
Get cell code from a file.
For now this only works with unnamed cells and cell separators of the
form `# %%`.
"""
path = Path(fname)
contents = path.read_text()
cells = contents.split("# %%")
return cells[int(cell_id)]

console.register_spyder_kernel_call_handler('get_file_code', get_file_code)
console.register_spyder_kernel_call_handler('run_cell', get_cell)

# Start client and show window
console.create_new_client(
special=special,
given_name=given_name,
path_to_custom_interpreter=path_to_custom_interpreter
)
window.setCentralWidget(console.get_widget())

# Set exclamation mark to True
configuration.set('debugger', 'pdb_use_exclamation_mark', True)

if os.name == 'nt':
qtbot.addWidget(window)

with qtbot.waitExposed(window):
window.resize(640, 480)
window.show()

# Set exclamation mark to True
configuration.set('debugger', 'pdb_use_exclamation_mark', True)

# Create new client for Matplotlb backend tests
if auto_backend or tk_backend:
qtbot.wait(SHELL_TIMEOUT)
console.create_new_client()
Expand Down
36 changes: 36 additions & 0 deletions spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,42 @@ def test_recursive_pdb(ipyconsole, qtbot):
assert control.toPlainText().split()[-2:] == ["In", "[3]:"]


def test_pdb_magics_are_recursive(ipyconsole, qtbot, tmp_path):
"""
Check that calls to Pdb magics start a recursive debugger when called in
a debugging session.
"""
shell = ipyconsole.get_current_shellwidget()
control = ipyconsole.get_widget().get_focus_widget()

# Code to run
code = "a = 10\n\n# %%\n\nb = 20"

# Write code to file on disk
file = tmp_path / 'test_pdb_magics.py'
file.write_text(code)

# Run file
with qtbot.waitSignal(shell.executed):
shell.execute(f"%debugfile {str(file)}")

# Run %debugfile in debugger
with qtbot.waitSignal(shell.executed):
shell.pdb_execute(f"%debugfile {str(file)}")

# Check that there are no errors and we started a recursive debugger
assert "error" not in control.toPlainText().lower()
assert "(IPdb [1]):" in control.toPlainText()

# Run %debugcell in debugger
with qtbot.waitSignal(shell.executed):
shell.pdb_execute(f"%debugcell -i 0 {str(file)}")

# Check that there are no errors and we started a recursive debugger
assert "error" not in control.toPlainText().lower()
assert "((IPdb [1])):" in control.toPlainText()


@flaky(max_runs=3)
@pytest.mark.skipif(os.name == 'nt', reason="Doesn't work on windows")
def test_stop_pdb(ipyconsole, qtbot):
Expand Down

0 comments on commit 6e85699

Please sign in to comment.