|
1 | | -"""conftest.py -- ensure this directory is first on sys.path. |
| 1 | +"""conftest.py -- isolate envelope-gate imports from sibling primitives. |
2 | 2 |
|
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. |
5 | 6 | """ |
6 | 7 |
|
7 | 8 | import sys |
8 | 9 | from pathlib import Path |
9 | 10 |
|
10 | 11 | _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 |
12 | 40 | if _HERE in sys.path: |
13 | 41 | sys.path.remove(_HERE) |
14 | 42 | sys.path.insert(0, _HERE) |
0 commit comments