|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from unittest.mock import Mock, patch |
| 4 | + |
| 5 | +from PySide6.QtWidgets import QApplication, QMessageBox |
| 6 | + |
| 7 | +from core.tabs import TabWidget |
| 8 | +from ui.main_window import MainWindow |
| 9 | + |
| 10 | + |
| 11 | +def _ensure_app(): |
| 12 | + return QApplication.instance() or QApplication([]) |
| 13 | + |
| 14 | + |
| 15 | +def test_close_tab_keeps_modified_tab_open_when_save_fails(): |
| 16 | + _ensure_app() |
| 17 | + widget = TabWidget() |
| 18 | + tab = widget.new_tab() |
| 19 | + tab.is_modified = True |
| 20 | + |
| 21 | + with ( |
| 22 | + patch.object(QMessageBox, "question", return_value=QMessageBox.StandardButton.Save), |
| 23 | + patch.object(tab, "save", return_value=False), |
| 24 | + ): |
| 25 | + widget.close_tab(0) |
| 26 | + |
| 27 | + assert widget.count() == 1 |
| 28 | + assert widget.tabs[0] is tab |
| 29 | + assert widget.current_tab() is tab |
| 30 | + |
| 31 | + widget.close() |
| 32 | + |
| 33 | + |
| 34 | +def test_run_current_does_not_execute_when_save_fails(tmp_path): |
| 35 | + _ensure_app() |
| 36 | + |
| 37 | + with patch("features.terminal.TerminalWidget._start_shell", lambda self: None): |
| 38 | + window = MainWindow() |
| 39 | + |
| 40 | + tab = window.tab_widget.current_tab() |
| 41 | + assert tab is not None |
| 42 | + |
| 43 | + tab.file_path = tmp_path / "script.py" |
| 44 | + tab.provider = type( |
| 45 | + "DummyProvider", |
| 46 | + (), |
| 47 | + {"get_run_command": staticmethod(lambda file_path: ["python", file_path])}, |
| 48 | + )() |
| 49 | + window.output.run_command = Mock() |
| 50 | + |
| 51 | + with patch.object(tab, "save", return_value=False): |
| 52 | + window.run_current() |
| 53 | + |
| 54 | + window.output.run_command.assert_not_called() |
| 55 | + |
| 56 | + window.close() |
| 57 | + |
| 58 | + |
| 59 | +def test_initial_save_updates_tab_title_and_enables_run(tmp_path): |
| 60 | + _ensure_app() |
| 61 | + |
| 62 | + with patch("features.terminal.TerminalWidget._start_shell", lambda self: None): |
| 63 | + window = MainWindow() |
| 64 | + |
| 65 | + tab = window.tab_widget.current_tab() |
| 66 | + assert tab is not None |
| 67 | + |
| 68 | + target = tmp_path / "script.py" |
| 69 | + tab.editor.setPlainText("print('hi')") |
| 70 | + |
| 71 | + with patch( |
| 72 | + "ui.main_window.QFileDialog.getSaveFileName", |
| 73 | + return_value=(str(target), "Python (*.py)"), |
| 74 | + ): |
| 75 | + window.save_file() |
| 76 | + |
| 77 | + current_index = window.tab_widget.currentIndex() |
| 78 | + assert tab.file_path == target |
| 79 | + assert window.tab_widget.tabText(current_index) == "script.py" |
| 80 | + assert window.output.run_btn.isEnabled() |
| 81 | + |
| 82 | + window.close() |
| 83 | + |
| 84 | + |
| 85 | +def test_initial_save_failure_restores_untitled_state(): |
| 86 | + _ensure_app() |
| 87 | + |
| 88 | + with patch("features.terminal.TerminalWidget._start_shell", lambda self: None): |
| 89 | + window = MainWindow() |
| 90 | + |
| 91 | + tab = window.tab_widget.current_tab() |
| 92 | + assert tab is not None |
| 93 | + original_index = window.tab_widget.currentIndex() |
| 94 | + original_label = window.lang_label.text() |
| 95 | + |
| 96 | + with ( |
| 97 | + patch( |
| 98 | + "ui.main_window.QFileDialog.getSaveFileName", |
| 99 | + return_value=("C:/tmp/never-written.py", "Python (*.py)"), |
| 100 | + ), |
| 101 | + patch("pathlib.Path.write_text", side_effect=OSError("disk full")), |
| 102 | + patch("PySide6.QtWidgets.QMessageBox.critical", return_value=None), |
| 103 | + ): |
| 104 | + window.save_file() |
| 105 | + |
| 106 | + assert tab.file_path is None |
| 107 | + assert window.tab_widget.tabText(original_index) == "Unbenannt" |
| 108 | + assert window.lang_label.text() == original_label |
| 109 | + assert not window.output.run_btn.isEnabled() |
| 110 | + |
| 111 | + window.close() |
0 commit comments