Skip to content

Commit 6c46bc2

Browse files
committed
Fix order-dependent test isolation in test_legal_links
The streamlit mock was installed into sys.modules, but src.common.common was only popped AFTER importing get_legal_links. When test_gui.py runs first (CI order: `pytest test_gui.py tests/`), it imports the real, streamlit-bound common module into sys.modules; the subsequent mock-based import here was then a cache hit returning the real-streamlit-bound function, so the tests' session_state overrides had no effect and get_legal_links returned the OpenMS defaults. The two override tests failed in CI (they passed locally only when collected first). Pop src.common.common BEFORE the import to force a fresh import under the mock, then restore the original module afterward so the AppTest-based test modules still get the genuine package. Full suite: 76 passed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WCamMNCunp9T2aScEKKUvx
1 parent f5d2953 commit 6c46bc2

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

tests/test_legal_links.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ def __setattr__(self, name, value):
6767
_saved_modules = {name: sys.modules.get(name) for name in _MOCKED_MODULES}
6868
sys.modules.update(_MOCKED_MODULES)
6969

70+
# Force a FRESH import of src.common.common under the streamlit mock, even if an
71+
# earlier test module (e.g. test_gui.py) already imported the real-streamlit-bound
72+
# version. Save whatever was cached first so we can restore it afterwards.
73+
_saved_common = sys.modules.pop("src.common.common", None)
74+
7075
from src.common.common import get_legal_links, DEFAULT_LEGAL_LINKS # noqa: E402
7176

7277
# Restore the real modules (or remove ones that weren't present) so that other
@@ -76,10 +81,15 @@ def __setattr__(self, name, value):
7681
sys.modules.pop(_name, None)
7782
else:
7883
sys.modules[_name] = _orig
79-
# Drop the cached common module that was imported under the mocks so AppTest
80-
# re-imports it fresh with the real streamlit. get_legal_links keeps working: it
81-
# holds a reference to the same `mock_streamlit` object we mutate in the tests.
82-
sys.modules.pop("src.common.common", None)
84+
# Restore the original cached common module (the real-streamlit-bound one, if
85+
# any) so AppTest-based test modules keep getting the genuine package.
86+
# get_legal_links keeps working: it holds a reference to the freshly-imported
87+
# mock-bound module's globals (and the same `mock_streamlit` object the tests
88+
# mutate).
89+
if _saved_common is None:
90+
sys.modules.pop("src.common.common", None)
91+
else:
92+
sys.modules["src.common.common"] = _saved_common
8393

8494

8595
def setup_function(_):

0 commit comments

Comments
 (0)