"""pytest bootstrap for the PDG regression suite. Stdlib only. Its sole job is to guarantee a *writable* base temp dir so the suite runs with the bare command ``python -m pytest tests -q`` on hosts where the default ``%LOCALAPPDATA%\\Temp`` path is not accessible (e.g. sandboxed CI). It prefers, in order: an explicit ``--basetemp`` (left untouched), then ``$PYTEST_DEBUG_TEMPROOT``, then a writable ``C:\\tmp`` / ``/tmp``, then the project's own ``.pytest_tmp`` directory. This changes only *where* temp dirs live; it never weakens any assertion. """ from __future__ import annotations import os import sys import tempfile from pathlib import Path def _pick_temproot() -> Path: candidates = [] env = os.environ.get("PYTEST_DEBUG_TEMPROOT") if env: candidates.append(Path(env)) # Writable scratch roots common on this host. if sys.platform.startswith("win"): candidates.append(Path("C:/tmp")) else: candidates.append(Path("/tmp")) # Last-resort: a temp root inside the project tree (always writable). candidates.append(Path(__file__).resolve().parent.parent / ".pytest_tmp") for c in candidates: try: c.mkdir(parents=True, exist_ok=True) probe = c / ".write_probe" probe.write_text("ok", encoding="utf-8") probe.unlink() return c except OSError: continue # Could not find anything writable; fall back to the system default. return Path(tempfile.gettempdir()) # Only override when the user has not already pinned a base temp dir. if "--basetemp" not in " ".join(sys.argv) and not os.environ.get("PYTEST_DEBUG_TEMPROOT"): root = _pick_temproot() os.environ["PYTEST_DEBUG_TEMPROOT"] = str(root)