opencode-env-rollout / tests /conftest.py
AdithyaSK's picture
AdithyaSK HF Staff
Upload folder using huggingface_hub
9e98d70 verified
"""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