Spaces:
Sleeping
Sleeping
File size: 1,526 Bytes
9e98d70 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | """Conftest for opencode-openenv tests.
The env directory is named ``openenv`` which collides with the installed
``openenv-core`` namespace when pytest adds the parent directory
(``…/opencode``) to ``sys.path``. Strip the shadowing entries and evict
any already-loaded stale modules so ``import openenv.core`` resolves to
the installed package.
"""
from __future__ import annotations
import os
import sys
_OPENENV_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_OPENCODE_PARENT = os.path.dirname(_OPENENV_DIR)
for _entry in (_OPENCODE_PARENT, _OPENENV_DIR):
while _entry in sys.path:
sys.path.remove(_entry)
_stale_candidates = [
k
for k in list(sys.modules)
if (k == "openenv" or k.startswith("openenv."))
and not k.startswith("openenv.tests") # keep the conftest's own module
]
for _stale in _stale_candidates:
module = sys.modules[_stale]
module_file = getattr(module, "__file__", None) or ""
# Only evict the modules that resolved to our shadow env directory.
if module_file.startswith(_OPENENV_DIR + os.sep) or (
_OPENENV_DIR in module_file and "/.venv/" not in module_file
):
sys.modules.pop(_stale, None)
# Sanity check — fail loudly at collection time if the import still breaks.
try:
import openenv.core # noqa: F401
except ImportError as exc:
raise ImportError(
"openenv.core still not importable after sys.path scrub; check that "
"openenv-core is installed in the env's .venv."
) from exc
|