Isolate pytest env per file so fail-closed and setup stay script-accurate.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
9d79e3e92f
commit
3c0213ae74
5 files changed
+57
-29
No files matched your search
+26
-26
@@ -2,7 +2,8 @@
|
||||
|
||||
Each file stays one case (`main()`) until it is split into native pytest
|
||||
functions. Integration files reset `kansho_test` only; `kansho_dev` is never
|
||||
the test target.
|
||||
the test target. Provider keys and fake flags are restored per file so the
|
||||
shared pytest process matches the old one-script-per-process isolation.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -17,6 +18,7 @@ if str(ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from tests.harness import ( # noqa: E402
|
||||
apply_module_env,
|
||||
is_postgres_test_database,
|
||||
prepare_test_env,
|
||||
reset_postgres_schema,
|
||||
@@ -56,34 +58,32 @@ def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item
|
||||
class KanshoScriptModule(pytest.Module):
|
||||
def collect(self):
|
||||
module = self.obj
|
||||
|
||||
def test_main() -> None:
|
||||
main = getattr(module, "main", None)
|
||||
if main is None or not callable(main):
|
||||
pytest.fail(f"{self.path.name} has no callable main()")
|
||||
main()
|
||||
|
||||
yield pytest.Function.from_parent(self, name="test_main", callobj=test_main)
|
||||
main = getattr(module, "main", None)
|
||||
if main is None or not callable(main):
|
||||
pytest.fail(f"{self.path.name} has no callable main()")
|
||||
yield pytest.Function.from_parent(self, name="test_main", callobj=main)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def _module_backend(request: pytest.FixtureRequest):
|
||||
stem = Path(str(request.path)).stem
|
||||
if stem in UNIT_MODULES:
|
||||
saved = dict(os.environ)
|
||||
try:
|
||||
source = Path(str(request.path))
|
||||
stem = source.stem
|
||||
saved = dict(os.environ)
|
||||
try:
|
||||
apply_module_env(source)
|
||||
if stem in UNIT_MODULES:
|
||||
yield
|
||||
finally:
|
||||
os.environ.clear()
|
||||
os.environ.update(saved)
|
||||
return
|
||||
if not is_postgres_test_database():
|
||||
pytest.skip(
|
||||
"integration requires KANSHO_DB_BACKEND=postgres and DB_NAME ending _test "
|
||||
"(Gitea: kansho_test on the Dev Postgres, never kansho_dev)"
|
||||
)
|
||||
reset_postgres_schema()
|
||||
from db import init_db
|
||||
return
|
||||
if not is_postgres_test_database():
|
||||
pytest.skip(
|
||||
"integration requires KANSHO_DB_BACKEND=postgres and DB_NAME ending _test "
|
||||
"(Gitea: kansho_test on the Dev Postgres, never kansho_dev)"
|
||||
)
|
||||
reset_postgres_schema()
|
||||
from db import init_db
|
||||
|
||||
init_db()
|
||||
yield
|
||||
init_db()
|
||||
yield
|
||||
finally:
|
||||
os.environ.clear()
|
||||
os.environ.update(saved)
|
||||
Reference in new issue
Block a user