Tests that call patch.dict(os.environ, clear=True) dropped GITEA_MCP_SESSION_STATE_DIR and re-adopted the operator host cache under ~/.cache/gitea-tools/session-state. A residual terminal request_changes mutation then tripped the #332 hard-stop before test_unknown_profile_blocks could assert the empty-profile gate. - Pin mcp_session_state.default_state_dir / DEFAULT_STATE_DIR to a per-test temp dir in conftest (still honors an explicit env override) - Clear the decision lock at the start of each TestMergePR test - Add regression coverage for env-clear isolation Closes #590
108 lines
4.0 KiB
Python
108 lines
4.0 KiB
Python
"""Shared pytest fixtures for the Gitea-Tools test suite."""
|
|
import os
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent.parent))
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _reset_mutation_authority(monkeypatch):
|
|
"""Isolate the in-process mutation authority between tests (#199).
|
|
|
|
The mutation-authority gate stays LIVE in every test — this fixture only
|
|
clears the per-process record and the session profile lock so one test's
|
|
seeded authority (or an intentionally mismatched one) cannot leak into
|
|
the next test. It must never replace verify_mutation_authority with a
|
|
no-op: individual tests that need a specific authority state set it up
|
|
explicitly.
|
|
|
|
Session-state isolation (#559 / #590 / #594): many tests use
|
|
``patch.dict(os.environ, ..., clear=True)``, which drops
|
|
``GITEA_MCP_SESSION_STATE_DIR``. Relying only on the env var therefore
|
|
re-exposes the operator host cache
|
|
(``~/.cache/gitea-tools/session-state``) and its review-mutation ledger.
|
|
Pin ``default_state_dir`` / ``DEFAULT_STATE_DIR`` to a per-test temp dir
|
|
so durable load/save never touches host state even after env clears.
|
|
"""
|
|
monkeypatch.delenv("GITEA_SESSION_PROFILE_LOCK", raising=False)
|
|
# Isolate durable session-state files so tests never share host cache (#559).
|
|
import tempfile
|
|
|
|
_state_tmp = tempfile.TemporaryDirectory(prefix="gitea-session-state-")
|
|
state_dir = _state_tmp.name
|
|
monkeypatch.setenv("GITEA_MCP_SESSION_STATE_DIR", state_dir)
|
|
try:
|
|
import mcp_session_state
|
|
except Exception:
|
|
mcp_session_state = None
|
|
if mcp_session_state is not None:
|
|
# Survive patch.dict(..., clear=True) that drops the env var (#590).
|
|
# Still honor an explicit GITEA_MCP_SESSION_STATE_DIR when tests set
|
|
# their own temp dir (see tests/test_mcp_session_state.py).
|
|
monkeypatch.setattr(
|
|
mcp_session_state, "DEFAULT_STATE_DIR", state_dir, raising=False
|
|
)
|
|
|
|
def _isolated_default_state_dir(
|
|
_fallback: str = state_dir,
|
|
_env_key: str = mcp_session_state.STATE_DIR_ENV,
|
|
) -> str:
|
|
raw = (os.environ.get(_env_key) or "").strip()
|
|
return raw or _fallback
|
|
|
|
monkeypatch.setattr(
|
|
mcp_session_state,
|
|
"default_state_dir",
|
|
_isolated_default_state_dir,
|
|
)
|
|
try:
|
|
import mcp_server
|
|
except Exception:
|
|
_state_tmp.cleanup()
|
|
yield
|
|
return
|
|
monkeypatch.setattr(mcp_server, "_MUTATION_AUTHORITY", None)
|
|
monkeypatch.setattr(mcp_server, "_IDENTITY_CACHE", {})
|
|
monkeypatch.setattr(mcp_server, "_REVIEW_DECISION_LOCK", None)
|
|
monkeypatch.setattr(mcp_server, "_preflight_whoami_called", False)
|
|
monkeypatch.setattr(mcp_server, "_preflight_capability_called", False)
|
|
monkeypatch.setattr(mcp_server, "_preflight_resolved_role", None)
|
|
monkeypatch.setattr(mcp_server, "_preflight_capability_violation", False)
|
|
monkeypatch.setattr(mcp_server, "_preflight_capability_violation_files", [])
|
|
monkeypatch.setattr(mcp_server, "_preflight_capability_baseline_porcelain", None)
|
|
monkeypatch.setattr(mcp_server, "_preflight_resolved_task", None)
|
|
monkeypatch.setattr(mcp_server, "_preflight_reviewer_violation_files", [])
|
|
monkeypatch.setattr(
|
|
mcp_server,
|
|
"_check_mcp_runtimes_diagnostics",
|
|
lambda *args, **kwargs: [],
|
|
)
|
|
try:
|
|
import review_workflow_load
|
|
review_workflow_load._REVIEW_WORKFLOW_LOAD = None
|
|
except Exception:
|
|
pass
|
|
try:
|
|
import capability_stop_terminal
|
|
capability_stop_terminal.clear()
|
|
except Exception:
|
|
pass
|
|
yield
|
|
try:
|
|
import capability_stop_terminal
|
|
capability_stop_terminal.clear()
|
|
except Exception:
|
|
pass
|
|
try:
|
|
import review_workflow_load
|
|
review_workflow_load._REVIEW_WORKFLOW_LOAD = None
|
|
except Exception:
|
|
pass
|
|
try:
|
|
mcp_server._REVIEW_DECISION_LOCK = None
|
|
except Exception:
|
|
pass
|
|
_state_tmp.cleanup()
|