"""Shared CLI-test fixtures. The most important defence here is ``cli_env`` — a fixture that scrubs ``HELIX_*`` env vars and points the CLI at a tmp config path. Without it, a developer with a real ``~/.config/helix/config.json`` would see their personal PAT influence test outcomes (or worse, get test code to write a token into their personal config). """ from __future__ import annotations import os from collections.abc import Iterator from pathlib import Path import pytest @pytest.fixture def cli_env(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]: """Isolate the CLI from the developer's real config and env. Yields the tmp config path so individual tests can assert on file state. Every ``HELIX_*`` env var is unset for the duration of the test (they're restored automatically by ``monkeypatch``). """ cfg = tmp_path / "helix-cli.json" monkeypatch.setenv("HELIX_CONFIG", str(cfg)) for var in ( "HELIX_PAT", "HELIX_TOKEN", "HELIX_BASE_URL", "HELIX_DEBUG", "XDG_CONFIG_HOME", ): monkeypatch.delenv(var, raising=False) yield cfg @pytest.fixture(autouse=True) def _no_proxy(monkeypatch: pytest.MonkeyPatch) -> None: """Strip proxy env vars so the CLI never accidentally routes a request through a developer's local debug proxy. ``trust_env=False`` on the httpx clients already disables this, but belt-and-braces. """ for var in ("HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy", "NO_PROXY", "no_proxy"): monkeypatch.delenv(var, raising=False) @pytest.fixture(autouse=True) def _restore_cwd() -> Iterator[None]: """Some tests rebind sys.stdin/stdout; make sure the cwd survives.""" cwd = os.getcwd() try: yield finally: try: os.chdir(cwd) except OSError: pass