""" Shared fixtures for installer unit tests. Resets stale tkinter MagicMocks before each test so that tests in test_screen.py don't hit exhausted side-effect iterators left over from test_navigation.py (which replaces sys.modules["tkinter"] at module level). """ import sys from pathlib import Path import pytest from unittest.mock import MagicMock # Ensure the repository root is first on sys.path so we import the local # `installer` package (and not a site-packages name collision). _REPO_ROOT = Path(__file__).resolve().parents[2] if str(_REPO_ROOT) not in sys.path: sys.path.insert(0, str(_REPO_ROOT)) import knoe.screen as _screen_module @pytest.fixture(autouse=True) def reset_tkinter_mock(): """ Before each test, replace any stale tkinter MagicMock with a fresh instance both in sys.modules and in knoe.screen module namespace, so TerminalConsole widget calls don't hit exhausted side-effect iterators. """ original_screen_tk = getattr(_screen_module, "tk", None) if isinstance(original_screen_tk, MagicMock): fresh = MagicMock() _screen_module.tk = fresh for mod_name in ( "tkinter", "tkinter.ttk", "tkinter.scrolledtext", "tkinter.messagebox", ): if mod_name in sys.modules and isinstance(sys.modules[mod_name], MagicMock): sys.modules[mod_name] = fresh yield # Restore original reference so other tests in the suite are unaffected if isinstance(original_screen_tk, MagicMock): _screen_module.tk = original_screen_tk