testenv.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """The isolated environment a checkout's test suite runs under.
  2. Activation builds it beside the checkout's install state, and CI builds it the
  3. same way. It carries the locked project dependencies plus the ``dev`` and
  4. ``test`` groups. It is a side environment
  5. (``ensure_project_environment``), never the selected application generation:
  6. test-only dependencies must not enter PM facts or anything that ships.
  7. """
  8. from __future__ import annotations
  9. from collections.abc import Sequence
  10. from pathlib import Path
  11. from pm.environments import install_state_dir
  12. NAME = "test-environment"
  13. # The default developer environment includes the app's normal features.
  14. # CI lanes pass their wider provider matrix explicitly.
  15. DEFAULT_TEST_EXTRAS = ("all",)
  16. GROUPS = ("dev", "test")
  17. def testenv_root(project_root: Path) -> Path:
  18. return install_state_dir(project_root) / NAME
  19. def ensure_testenv(project_root: Path, extras: Sequence[str] | None = None) -> Path:
  20. """Build the test environment unless its locked inputs are unchanged; return its python."""
  21. from pm import ensure_project_environment
  22. chosen = sorted(set(DEFAULT_TEST_EXTRAS if extras is None else extras))
  23. return ensure_project_environment(NAME, project_root, extras=chosen, groups=GROUPS,
  24. root=testenv_root(project_root), explicit=True)
  25. def testenv_python(project_root: Path) -> Path | None:
  26. """The selected test interpreter, read without acquiring tools or writing state."""
  27. from pm.operations import environment_python
  28. return environment_python(NAME, root=testenv_root(project_root))
  29. def parse_extras(value: str) -> list[str] | None:
  30. """An empty value selects default extras; commas and spaces separate overrides."""
  31. return value.replace(",", " ").split() or None