Skip to content

Commit df48dbf

Browse files
authored
fix: upgrade conftest.py to evict cached sibling modules
1 parent 12f66f8 commit df48dbf

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1-
"""conftest.py -- ensure this directory is first on sys.path.
1+
"""conftest.py -- isolate envelope-gate imports from sibling primitives.
22
3-
Prevents pytest from importing 'gate' from a sibling primitive
4-
(e.g. authority-gate/gate.py) instead of this one.
3+
Uses pytest_configure hook (runs before collection) to ensure
4+
this directory is first on sys.path and that any cached 'gate'
5+
module from a sibling primitive (authority-gate) is evicted.
56
"""
67

78
import sys
89
from pathlib import Path
910

1011
_HERE = str(Path(__file__).resolve().parent)
11-
if _HERE not in sys.path or sys.path[0] != _HERE:
12+
_PRIMITIVES = str(Path(__file__).resolve().parent.parent)
13+
14+
15+
def pytest_configure(config):
16+
"""Runs before test collection -- guaranteed early."""
17+
# Evict any cached modules that shadow our local ones.
18+
# If authority-gate/gate.py was imported first, 'gate' in
19+
# sys.modules points to the wrong file.
20+
for mod_name in ["gate", "envelope_parser", "rules"]:
21+
if mod_name in sys.modules:
22+
cached = sys.modules[mod_name]
23+
origin = getattr(cached, "__file__", "") or ""
24+
if _HERE not in origin:
25+
del sys.modules[mod_name]
26+
27+
# Remove sibling primitive dirs from sys.path
28+
to_remove = []
29+
for p in sys.path:
30+
if (
31+
p != _HERE
32+
and p.startswith(_PRIMITIVES)
33+
and Path(p, "gate.py").exists()
34+
):
35+
to_remove.append(p)
36+
for p in to_remove:
37+
sys.path.remove(p)
38+
39+
# Ensure our directory is first
1240
if _HERE in sys.path:
1341
sys.path.remove(_HERE)
1442
sys.path.insert(0, _HERE)

0 commit comments

Comments
 (0)