Skip to content

Commit

Permalink
Merge pull request #17191 from impact27/test_debug_unsaved
Browse files Browse the repository at this point in the history
PR: Fix debugging unsaved files
  • Loading branch information
ccordoba12 committed Jan 25, 2022
2 parents ebc7e48 + dafc2ab commit 242a2fa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
4 changes: 2 additions & 2 deletions external-deps/spyder-kernels/.gitrepo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[subrepo]
remote = https://github.com/spyder-ide/spyder-kernels.git
branch = 2.x
commit = cd095059d7957b5929854e279ca75cd3c658d620
parent = e29db93e2a67ca98cd9e7da4b356bb685bf53a29
commit = 52f9b9c09a4c07f1b9ab00ada87a0d741341db64
parent = 97e14d2e8876460f7fdf1306fbc852274edb2ea4
method = merge
cmdver = 0.4.3
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
# Licensed under the terms of the MIT License
# (see spyder_kernels/__init__.py for details)

import sys
import linecache
import os.path
import sys

from IPython.core.getipython import get_ipython

Expand Down Expand Up @@ -103,5 +104,5 @@ def __exit__(self, exc_type, exc_val, exc_tb):
sys.modules['__main__'] = self._previous_main
elif '__main__' in sys.modules and self._reset_main:
del sys.modules['__main__']
if self.filename in linecache.cache:
if self.filename in linecache.cache and os.path.exists(self.filename):
linecache.cache.pop(self.filename)
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,10 @@ def runfile(filename=None, args=None, wdir=None, namespace=None,
_print("Could not get code from editor.\n")
return

# Normalise the filename
filename = os.path.abspath(filename)
filename = os.path.normcase(filename)

with NamespaceManager(filename, namespace, current_namespace,
file_code=file_code) as (ns_globals, ns_locals):
sys.argv = [filename]
Expand Down Expand Up @@ -676,6 +680,11 @@ def runcell(cellname, filename=None, post_mortem=False):
file_code = get_file_code(filename, save_all=False)
except Exception:
file_code = None

# Normalise the filename
filename = os.path.abspath(filename)
filename = os.path.normcase(filename)

with NamespaceManager(filename, current_namespace=True,
file_code=file_code) as (ns_globals, ns_locals):
exec_code(cell_code, filename, ns_globals, ns_locals,
Expand Down
37 changes: 37 additions & 0 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4409,6 +4409,7 @@ def crash_func():
@flaky(max_runs=3)
@pytest.mark.skipif(os.name == 'nt', reason="Tour messes up focus on Windows")
@pytest.mark.parametrize("focus_to_editor", [True, False])
@pytest.mark.skipif(os.name == 'nt', reason="Fails on Windows")
def test_focus_to_editor(main_window, qtbot, tmpdir, focus_to_editor):
"""Test that the focus_to_editor option works as expected."""
# Write code with cells to a file
Expand Down Expand Up @@ -4593,5 +4594,41 @@ def test_history_from_ipyconsole(main_window, qtbot):
assert text.splitlines()[-1] == code


@pytest.mark.slow
def test_debug_unsaved_function(main_window, qtbot):
"""
Test that a breakpoint in an unsaved file is reached.
"""
# Main variables
shell = main_window.ipyconsole.get_current_shellwidget()
control = shell._control
run_action = main_window.run_toolbar_actions[0]
run_button = main_window.run_toolbar.widgetForAction(run_action)

# Clear all breakpoints
main_window.editor.clear_all_breakpoints()

# create new file
main_window.editor.new()
code_editor = main_window.editor.get_focus_widget()
code_editor.set_text('def foo():\n print(1)')

# Set breakpoint
code_editor.debugger.toogle_breakpoint(line_number=2)

# run file
with qtbot.waitSignal(shell.executed):
qtbot.mouseClick(run_button, Qt.LeftButton)

# debug foo
with qtbot.waitSignal(shell.executed):
shell.execute('%debug foo()')

with qtbot.waitSignal(shell.executed):
shell.execute('continue')

assert "1---> 2 print(1)" in control.toPlainText()


if __name__ == "__main__":
pytest.main()

0 comments on commit 242a2fa

Please sign in to comment.