Fix CI import path isolation
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
"""Pytest import guardrails for local project modules."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SCRIPTS = str(ROOT / "scripts")
|
||||
WORKBUDDY_SKILL_SCRIPTS = ".workbuddy/skills/jyotish-vedic-astrology/scripts"
|
||||
|
||||
|
||||
def ensure_project_scripts_first() -> None:
|
||||
if SCRIPTS in sys.path:
|
||||
sys.path.remove(SCRIPTS)
|
||||
sys.path.insert(0, SCRIPTS)
|
||||
|
||||
|
||||
def remove_workbuddy_shadow_modules() -> None:
|
||||
for name, module in list(sys.modules.items()):
|
||||
module_file = getattr(module, "__file__", "") or ""
|
||||
if WORKBUDDY_SKILL_SCRIPTS in module_file:
|
||||
del sys.modules[name]
|
||||
|
||||
|
||||
def pytest_runtest_setup() -> None:
|
||||
remove_workbuddy_shadow_modules()
|
||||
ensure_project_scripts_first()
|
||||
|
||||
|
||||
ensure_project_scripts_first()
|
||||
@@ -11,7 +11,7 @@ SCRIPTS = ROOT / "scripts"
|
||||
if str(SCRIPTS) not in sys.path:
|
||||
sys.path.insert(0, str(SCRIPTS))
|
||||
|
||||
from ashtakavarga import (
|
||||
from ashtakavarga import ( # noqa: E402
|
||||
ALL_SOURCES,
|
||||
BAV_TOTALS,
|
||||
EXPECTED_SAV_TOTAL,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -92,3 +93,18 @@ def test_yoga_logic_validation_import_does_not_shadow_project_modules() -> None:
|
||||
|
||||
assert not sys.path[0].endswith(".workbuddy/skills/jyotish-vedic-astrology/scripts")
|
||||
sys.path[:] = before
|
||||
|
||||
|
||||
def test_pytest_import_guard_restores_project_scripts_first() -> None:
|
||||
before = list(sys.path)
|
||||
try:
|
||||
sys.path.insert(0, str(ROOT / ".workbuddy" / "skills" / "jyotish-vedic-astrology" / "scripts"))
|
||||
|
||||
import tests.conftest as conftest
|
||||
|
||||
importlib.reload(conftest)
|
||||
assert sys.path[0] == str(ROOT / "scripts")
|
||||
assert str(ROOT / "scripts") == conftest.SCRIPTS
|
||||
assert conftest.WORKBUDDY_SKILL_SCRIPTS == ".workbuddy/skills/jyotish-vedic-astrology/scripts"
|
||||
finally:
|
||||
sys.path[:] = before
|
||||
|
||||
Reference in New Issue
Block a user