From b73757a7e96b2082cdba28899f6bab0be9ad3ea6 Mon Sep 17 00:00:00 2001 From: Lukas Geiger Date: Sat, 26 Sep 2026 18:33:25 +0200 Subject: [PATCH] fix(tests): platform-safe os.startfile/subprocess.Popen mock Split out of PR #6 at team-lead's request (measured Windows regression there was traced to the conftest.py Qt-teardown attempt, reverted; this fix is unrelated and independently verified). test_open_extern_handles_os_error_gracefully patched os.startfile unconditionally. os.startfile does not exist on Linux/macOS at all, so unittest.mock.patch() on it raised AttributeError there before the test body even ran. The test now mocks whichever call _open_extern() actually uses for the current sys.platform (os.startfile on win32, subprocess.Popen everywhere else -- matching the source's own branching). --- tests/test_preview_panel_hardening.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/test_preview_panel_hardening.py b/tests/test_preview_panel_hardening.py index 198fa4d..f9eb4e6 100644 --- a/tests/test_preview_panel_hardening.py +++ b/tests/test_preview_panel_hardening.py @@ -147,12 +147,22 @@ def test_metadata_handles_negative_or_invalid_timestamp_gracefully(self, tmp_pat class TestExcelPreviewExternalLaunch: def test_open_extern_handles_os_error_gracefully(self, tmp_path): + """_open_extern() waehlt den Aufruf je nach sys.platform (os.startfile + unter Windows, subprocess.Popen("open"/"xdg-open") sonst) -- os.startfile + existiert unter Linux/macOS gar nicht, patch() darauf wuerde dort mit + AttributeError abbrechen. Der passende Aufrufpfad fuer die aktuelle + Plattform wird stattdessen gemockt.""" from gui.preview.preview_panel import ExcelPreview widget = ExcelPreview() widget._path = str(tmp_path / "fake.xlsx") - with patch("os.startfile", side_effect=OSError("No application associated")): + if sys.platform == "win32": + target, kwargs = "os.startfile", {"side_effect": OSError("No application associated")} + else: + target, kwargs = "subprocess.Popen", {"side_effect": OSError("No application associated")} + + with patch(target, **kwargs): widget._open_extern() assert not widget.status_label.isHidden()