import os from pathlib import Path import pytest from afk_developer.config import DEFAULT_PROJECTS_DIR from afk_developer.ledger import LedgerStore # AFK_* overrides are read at Config.from_env() time, so a developer's shell can # silently change test outcomes. Observed live: an exported AFK_TARGET_PYTHON # failed three campaign-create tests that pass in a clean environment. _AMBIENT_PREFIXES = ("AFK_",) @pytest.fixture(autouse=True) def _isolate_afk_environment( monkeypatch: pytest.MonkeyPatch, tmp_path_factory: pytest.TempPathFactory ) -> None: for name in [key for key in os.environ if key.startswith(_AMBIENT_PREFIXES)]: monkeypatch.delenv(name, raising=False) # ~/.afk-developer/config.yml is the same hazard as an exported AFK_* var, # and was not covered: a real config.yml on the developer's machine failed # three campaign create-core tests that pass without one, because its # execution.target_python pointed outside the test's target directory. # Point the loader at a path that cannot exist so tests read no user config. absent = tmp_path_factory.mktemp("afk-no-user-config") / "config.yml" monkeypatch.setenv("AFK_CONFIG_PATH", str(absent)) @pytest.fixture(autouse=True) def _isolate_ledger_store( tmp_path_factory: pytest.TempPathFactory, monkeypatch: pytest.MonkeyPatch ) -> None: """Redirect the ledger of any `Config()` (default projects_dir) into a scratch dir. The ledger is shared across every plan named the same, so without this a scar written by one test poisons every later test that reuses "example". The scratch dir is deliberately NOT `tmp_path`: many tests use `tmp_path` as the target git repo, and a ledger written inside it would dirty the tree. """ state_dir = tmp_path_factory.mktemp("afk-ledger-state") original_init = LedgerStore.__init__ def isolated_init(self, projects_dir): if Path(projects_dir) == DEFAULT_PROJECTS_DIR: projects_dir = state_dir original_init(self, projects_dir) monkeypatch.setattr(LedgerStore, "__init__", isolated_init)