Files
AegisGitea-MCP/tests/conftest.py
T

50 lines
1.6 KiB
Python

"""Pytest configuration and fixtures.
Every test runs with a scrubbed environment, an empty working directory (so a
developer's own ``.env`` is never read) and state paths inside ``tmp_path``.
Nothing may write to ``/var/log``, ``/var/lib`` or the real home directory.
"""
from __future__ import annotations
from collections.abc import Generator
from pathlib import Path
import pytest
from aegis_gitea_mcp.audit import reset_audit_logger
from aegis_gitea_mcp.config import Settings, reset_settings, set_env_file
from aegis_gitea_mcp.request_context import clear_gitea_auth_context
def _reset_singletons() -> None:
"""Reset every module-level singleton."""
set_env_file(None)
reset_settings()
reset_audit_logger()
clear_gitea_auth_context()
@pytest.fixture(autouse=True)
def reset_globals(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
"""Isolate each test: clean env, tmp cwd, tmp state paths, fresh singletons."""
for field_name in Settings.model_fields:
monkeypatch.delenv(field_name.upper(), raising=False)
monkeypatch.delenv("AEGIS_ENV_FILE", raising=False)
monkeypatch.chdir(tmp_path)
data_dir = tmp_path / "data"
monkeypatch.setenv("DATA_DIR", str(data_dir))
monkeypatch.setenv("AUDIT_LOG_PATH", str(data_dir / "audit.log"))
_reset_singletons()
yield
_reset_singletons()
@pytest.fixture
def mock_env(monkeypatch: pytest.MonkeyPatch) -> None:
"""Minimal valid environment: a Gitea URL and a token."""
monkeypatch.setenv("GITEA_URL", "https://gitea.example.com")
monkeypatch.setenv("GITEA_TOKEN", "test-token-12345")