TIER-2 READ conductor/code_styleguides/error_handling.md end-to-end before Phase 5: refactor(gui_2): migrate L1393 _open_patch_in_external_editor to Result[T] (Phase 5)

Extract _open_patch_in_external_editor_result helper from the external editor
launch try/except in App._open_patch_in_external_editor. Legacy wrapper
drains errors to app._last_request_errors per FR-BC-4 event-handler pattern.

[pre-audit] L1393 INTERNAL_BROAD_CATCH
[post-audit] V count: 11 -> 10 (L1393 removed)
This commit is contained in:
ed
2026-06-19 23:45:29 -04:00
parent 374866619d
commit 77a48b18bf
2 changed files with 120 additions and 23 deletions
+52
View File
@@ -923,3 +923,55 @@ def test_phase_5_l1367_apply_pending_patch_result_failure():
err = result.errors[0]
assert err.source == "gui_2._apply_pending_patch_result"
assert "patch blew up" in err.message
def test_phase_5_l1393_open_patch_in_external_editor_result_success():
"""
L1393 _open_patch_in_external_editor_result returns Result.ok=True on success.
The helper wraps the external editor launch try/except in
App._open_patch_in_external_editor. On success (launcher.launch_diff
returns a process), returns Result(data=True).
"""
from src import gui_2
from unittest.mock import MagicMock, patch
app = MagicMock()
app._pending_patch_files = ["/proj/foo/src/foo.py"]
app._pending_patch_text = "--- a/foo.py\n+++ b/foo.py\n"
mock_editor = MagicMock(name="mock_editor")
mock_launcher = MagicMock(name="mock_launcher")
mock_launcher.config.get_default.return_value = mock_editor
mock_process = MagicMock(name="mock_process")
mock_launcher.launch_diff.return_value = mock_process
with patch("os.path.exists", return_value=True), \
patch("src.external_editor.get_default_launcher", return_value=mock_launcher), \
patch("src.external_editor.create_temp_modified_file", return_value="/tmp/patch_temp.py"):
result = gui_2._open_patch_in_external_editor_result(app)
assert result.ok, f"Expected ok=True on success, got errors: {result.errors}"
assert result.data is True
assert app._vscode_diff_process is mock_process
def test_phase_5_l1393_open_patch_in_external_editor_result_failure():
"""
L1393 _open_patch_in_external_editor_result returns Result.ok=False with ErrorInfo on failure.
When the external editor launch raises, the helper converts the exception
to ErrorInfo and returns Result(data=False, errors=[ErrorInfo]).
"""
from src import gui_2
from unittest.mock import MagicMock, patch
app = MagicMock()
app._pending_patch_files = ["/proj/foo/src/foo.py"]
app._pending_patch_text = "--- a/foo.py\n+++ b/foo.py\n"
mock_launcher = MagicMock()
mock_launcher.config.get_default.return_value = MagicMock()
with patch("os.path.exists", return_value=True), \
patch("src.external_editor.get_default_launcher", return_value=mock_launcher), \
patch("src.external_editor.create_temp_modified_file", side_effect=RuntimeError("temp file creation blew up")):
result = gui_2._open_patch_in_external_editor_result(app)
assert not result.ok, f"Expected ok=False on failure, got data: {result.data}"
assert result.errors, "Expected at least one error on failure"
err = result.errors[0]
assert err.source == "gui_2._open_patch_in_external_editor_result"
assert "temp file creation blew up" in err.message