Skip to content

Commit

Permalink
Merge pull request #22820 from ccordoba12/issue-22630
Browse files Browse the repository at this point in the history
PR: Register run metadata on renames for supported file extensions (Editor)
  • Loading branch information
ccordoba12 authored Nov 3, 2024
2 parents 0e71cda + a4e1f6a commit 3bee638
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
32 changes: 15 additions & 17 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6630,19 +6630,16 @@ def test_runfile_namespace(main_window, qtbot, tmpdir):
assert "test_globals True" in control.toPlainText()


@pytest.mark.skipif(
os.name == 'nt',
reason="No quotes on Windows file paths"
)
def test_quotes_rename_ipy(main_window, qtbot, tmpdir):
@pytest.mark.skipif(os.name == "nt", reason="No quotes on Windows file paths")
def test_quotes_rename_ipy(main_window, qtbot, tmp_path):
"""
Test that we can run files with quotes in name, renamed files,
and ipy files.
"""
# create a file with a funky name
path = "a'b\"c\\.py"
file = tmpdir.join(path)
file.write("print(23 + 780)")
file = tmp_path / path
file.write_text("print(23 + 780)")
path = to_text_string(file)
main_window.editor.load(path)

Expand Down Expand Up @@ -6702,18 +6699,19 @@ def test_quotes_rename_ipy(main_window, qtbot, tmpdir):
# Save file in a new folder
code_editor.set_text("print(19 + 780)")

with tempfile.TemporaryDirectory() as td:
new_dir = tmp_path / f"foo_{random.randint(1, 1000)}"
new_dir.mkdir()

editorstack = main_window.editor.get_current_editorstack()
editorstack.select_savename = lambda fn: os.path.join(td, "fn.ipy")
main_window.editor.save()
with qtbot.waitSignal(shell.executed):
qtbot.mouseClick(main_window.run_cell_button, Qt.LeftButton)
editorstack = main_window.editor.get_current_editorstack()
editorstack.select_savename = lambda fn: str(new_dir / "fn.ipy")
editorstack.save_as()
with qtbot.waitSignal(shell.executed):
qtbot.mouseClick(main_window.run_cell_button, Qt.LeftButton)

assert "799" in control.toPlainText()
assert "error" not in control.toPlainText()
assert "fn.ipy" in control.toPlainText()
main_window.editor.close_file()
assert "799" in control.toPlainText()
assert "error" not in control.toPlainText()
assert "fn.ipy" in control.toPlainText()
main_window.editor.close_file()


@flaky(max_runs=5)
Expand Down
31 changes: 31 additions & 0 deletions spyder/plugins/editor/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from spyder.plugins.editor.utils.autosave import AutosaveForPlugin
from spyder.plugins.editor.widgets.editorstack import editorstack as editor_module
from spyder.plugins.editor.widgets.codeeditor import CodeEditor
from spyder.plugins.run.api import RunContext
from spyder.utils.sourcecode import get_eol_chars, get_eol_chars_from_os_name


Expand Down Expand Up @@ -510,5 +511,35 @@ def test_remove_editorstacks_and_windows(editor_plugin, qtbot):
assert len(editor_plugin.get_widget().editorstacks) == 1


def test_register_run_metadata(editor_plugin):
"""
Check that run metadata is registered for Python files and deregistered for
non-Python ones on renames.
This is a regression test for spyder-ide/spyder#22630.
"""
# Add run config for Python files
widget = editor_plugin.get_widget()
widget.supported_run_configurations = {
"py": {RunContext.File, RunContext.Selection, RunContext.Cell}
}

# Create empty file
editor_plugin.new()

# Check the file was registered to be run
editorstack = editor_plugin.get_current_editorstack()
filename = editorstack.get_filenames()[0]
assert filename in widget.file_per_id.values()

# Rename file to a type that can't be run
editor_plugin.renamed(filename, 'foo.md')

# Check the file is no longer available to be run
filename = editorstack.get_filenames()[0]
assert filename not in widget.file_per_id.values()
assert widget.file_per_id == {}


if __name__ == "__main__":
pytest.main(['-x', osp.basename(__file__), '-vv', '-rw'])
17 changes: 13 additions & 4 deletions spyder/plugins/editor/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,12 +1093,21 @@ def change_register_file_run_metadata(self, old_filename, new_filename):

self.deregister_file_run_metadata(old_filename)

# This avoids to register the run metadata of new_filename twice, which
# can happen for some rename operations.
if not self.id_per_file.get(new_filename):
# Get file extension (without the dot)
filename_ext = osp.splitext(new_filename)[1][1:]

# Check if we can actually register the new file
if (
# This avoids to register new_filename twice, which can happen for
# some rename operations.
not self.id_per_file.get(new_filename)
# Check if the new file extension is supported.
# Fixes spyder-ide/spyder#22630
and filename_ext in self.supported_run_configurations
):
self.register_file_run_metadata(new_filename)

if is_selected:
if is_selected and self.id_per_file.get(new_filename):
self._plugin._switch_focused_run_configuration(
self.id_per_file[new_filename]
)
Expand Down

0 comments on commit 3bee638

Please sign in to comment.