|
1 | 1 | import ast |
2 | 2 | import importlib.util |
3 | 3 | import os |
| 4 | +import shutil |
| 5 | +import subprocess |
4 | 6 | import tempfile |
5 | 7 | import unittest |
6 | 8 | from pathlib import Path |
@@ -105,6 +107,57 @@ def test_non_windows_external_commands_use_current_interpreter(self): |
105 | 107 | self.assertEqual(["/opt/current-python/bin/python", "tool.py"], cmd) |
106 | 108 |
|
107 | 109 |
|
| 110 | +@unittest.skipUnless(shutil.which("git") is not None, "git required") |
| 111 | +class GitIntegrationRegressionTests(unittest.TestCase): |
| 112 | + def _create_repo_with_tracked_file(self, initial_text: str): |
| 113 | + tmp_dir = Path(tempfile.mkdtemp(prefix="pythonbox-git-")) |
| 114 | + subprocess.run(["git", "init"], cwd=tmp_dir, check=True, capture_output=True) |
| 115 | + |
| 116 | + file_path = tmp_dir / "demo.py" |
| 117 | + file_path.write_text(initial_text, encoding="utf-8") |
| 118 | + subprocess.run(["git", "-C", str(tmp_dir), "add", "demo.py"], check=True, capture_output=True) |
| 119 | + |
| 120 | + self.addCleanup(shutil.rmtree, tmp_dir, True) |
| 121 | + return tmp_dir, file_path |
| 122 | + |
| 123 | + def test_git_status_formats_combined_codes_readably(self): |
| 124 | + module = load_pythonbox_module() |
| 125 | + |
| 126 | + _, file_path = self._create_repo_with_tracked_file("a\nb\nc\n") |
| 127 | + file_path.write_text("a\nX\nc\n", encoding="utf-8") |
| 128 | + |
| 129 | + git = module.GitIntegration() |
| 130 | + status = git.get_file_status(str(file_path)) |
| 131 | + |
| 132 | + self.assertEqual("+ Added / ● Modified", status) |
| 133 | + |
| 134 | + def test_git_modified_lines_classify_replacements_as_modified(self): |
| 135 | + module = load_pythonbox_module() |
| 136 | + |
| 137 | + _, file_path = self._create_repo_with_tracked_file("a\nb\nc\n") |
| 138 | + file_path.write_text("a\nX\nc\n", encoding="utf-8") |
| 139 | + |
| 140 | + git = module.GitIntegration() |
| 141 | + added, modified, deleted = git.get_modified_lines(str(file_path)) |
| 142 | + |
| 143 | + self.assertEqual([], sorted(added)) |
| 144 | + self.assertEqual([2], sorted(modified)) |
| 145 | + self.assertEqual([1], sorted(deleted)) |
| 146 | + |
| 147 | + def test_git_modified_lines_classify_insertions_as_added(self): |
| 148 | + module = load_pythonbox_module() |
| 149 | + |
| 150 | + _, file_path = self._create_repo_with_tracked_file("a\nc\n") |
| 151 | + file_path.write_text("a\nb\nc\n", encoding="utf-8") |
| 152 | + |
| 153 | + git = module.GitIntegration() |
| 154 | + added, modified, deleted = git.get_modified_lines(str(file_path)) |
| 155 | + |
| 156 | + self.assertEqual([2], sorted(added)) |
| 157 | + self.assertEqual([], sorted(modified)) |
| 158 | + self.assertEqual([], sorted(deleted)) |
| 159 | + |
| 160 | + |
108 | 161 | class SettingsRegressionTests(unittest.TestCase): |
109 | 162 | def _temp_settings(self, module, folder: str): |
110 | 163 | settings_path = Path(folder) / "settings.ini" |
@@ -158,6 +211,30 @@ def test_main_window_reacts_to_minimap_setting_changes(self): |
158 | 211 | window.deleteLater() |
159 | 212 | app.processEvents() |
160 | 213 |
|
| 214 | + def test_save_file_as_cancel_keeps_original_path(self): |
| 215 | + module = load_pythonbox_module() |
| 216 | + app = module.QApplication.instance() or module.QApplication([]) |
| 217 | + |
| 218 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 219 | + original_path = Path(temp_dir) / "demo.py" |
| 220 | + original_path.write_text("print('hi')\n", encoding="utf-8") |
| 221 | + |
| 222 | + window = module.PythonArchitect() |
| 223 | + try: |
| 224 | + tab = window.tab_editor.current_tab() |
| 225 | + tab.file_path = str(original_path) |
| 226 | + |
| 227 | + with mock.patch.object( |
| 228 | + module.QFileDialog, "getSaveFileName", return_value=("", "") |
| 229 | + ): |
| 230 | + window.save_file_as() |
| 231 | + |
| 232 | + self.assertEqual(str(original_path), tab.file_path) |
| 233 | + finally: |
| 234 | + window.close() |
| 235 | + window.deleteLater() |
| 236 | + app.processEvents() |
| 237 | + |
161 | 238 |
|
162 | 239 | if __name__ == "__main__": |
163 | 240 | unittest.main() |
0 commit comments