refactor(tests): centralize fake endpoint resolver cleanup

Test-only refactor continuing #2523. Centralizes the final repeated fake src.endpoint_resolver cleanup pattern into a focused import-state helper.
This commit is contained in:
Alexandre Teixeira
2026-06-05 13:23:46 +01:00
committed by GitHub
parent 301d1109b5
commit 452a94fb1b
5 changed files with 234 additions and 16 deletions
+49
View File
@@ -12,6 +12,11 @@ Use ``clear_fake_database_modules`` to evict a *stubbed* ``core.database`` (and
its companion ``src.database``) that another test left in import state, without
touching a real ``core.database`` loaded from disk.
Use ``clear_fake_endpoint_resolver_modules`` to evict a *stubbed*
``src.endpoint_resolver`` (and the route modules that imported it) that another
test left in import state, without touching a real ``src.endpoint_resolver``
loaded from disk.
Background: importing ``routes.session_routes`` also sets ``session_routes`` on
the parent ``routes`` package object. A ``from routes import session_routes``
or ``import routes.session_routes as X`` statement resolves through that parent
@@ -93,6 +98,50 @@ def clear_fake_database_modules():
delattr(parent, "database")
def clear_fake_endpoint_resolver_modules(*extra_modules):
"""Evict a *stubbed* ``src.endpoint_resolver`` (and dependent route modules).
Test-only. Several route tests need the *real* ``src.endpoint_resolver`` URL
helpers, but another test may have installed a fake — a stub module with no
on-disk ``__file__`` — into ``sys.modules`` and onto the ``src`` package
during collection. The route modules (``routes.model_routes`` and any extras
passed in, e.g. ``routes.chat_routes``) get cached against that fake on first
import, so they must be evicted too.
Conservative, mirroring ``clear_fake_database_modules`` and the per-file
guards it replaces:
* It acts only when ``src.endpoint_resolver`` is a fake/stub, detected by a
falsy ``__file__`` (missing, ``None``, or empty string) — exactly the
truthiness check the old inline guards used. A real resolver loaded from
disk carries a truthy ``__file__`` and is left untouched, as is the case
where nothing is cached. When the resolver is real, the dependent route
modules are left untouched too.
* When it does act, it drops ``routes.model_routes`` plus every name in
``extra_modules``.
* It removes the ``src.endpoint_resolver`` parent-package attribute only when
that attribute is the same fake object being evicted.
Behavior delta vs. the old bare ``sys.modules.pop(...)`` guards: dependent
modules are dropped via :func:`clear_module`, which also clears the parent
``routes`` package attribute (e.g. ``routes.model_routes``), not just the
``sys.modules`` entry. This prevents a stale parent attribute from shadowing
the fresh import — the same parent-attr handling the rest of this helper
family already applies.
"""
parent = sys.modules.get("src")
attr = getattr(parent, "endpoint_resolver", None) if parent is not None else None
mod = sys.modules.get("src.endpoint_resolver") or attr
if mod is None or getattr(mod, "__file__", None):
return
sys.modules.pop("src.endpoint_resolver", None)
if parent is not None and attr is mod:
delattr(parent, "endpoint_resolver")
clear_module("routes.model_routes")
for name in extra_modules:
clear_module(name)
@contextmanager
def preserve_import_state(*module_names):
"""Save and restore sys.modules entries and parent-package attributes.