test_dev_requirements_pins.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """requirements-dev.txt must mirror the pytest and pyyaml pins CI installs."""
  2. from __future__ import annotations
  3. import re
  4. from pathlib import Path
  5. ROOT = Path(__file__).resolve().parent.parent
  6. CI = ROOT / ".github" / "workflows" / "ci.yml"
  7. DEV = ROOT / "requirements-dev.txt"
  8. TOOLS = ("pytest", "pyyaml")
  9. def pins(text: str) -> dict[str, str]:
  10. """Map each of TOOLS to the version pinned with == in the text."""
  11. found = {}
  12. for name in TOOLS:
  13. match = re.search(rf"(?im)(?<![\w-]){name}==([0-9][\w.]*)", text)
  14. if match:
  15. found[name] = match.group(1)
  16. return found
  17. def ci_install_command() -> str:
  18. lines = [
  19. line
  20. for line in CI.read_text(encoding="utf-8").splitlines()
  21. if line.strip().startswith("install-command:")
  22. ]
  23. assert len(lines) == 1, "expected exactly one install-command in ci.yml"
  24. return lines[0]
  25. def test_ci_pins_every_tool_and_dev_requirements_match():
  26. ci = pins(ci_install_command())
  27. dev = pins(DEV.read_text(encoding="utf-8"))
  28. assert set(ci) == set(TOOLS), f"CI must pin {TOOLS} with ==, found {ci}"
  29. assert dev == ci, f"requirements-dev.txt {dev} differs from ci.yml {ci}"
  30. def test_guard_bites_on_drift_and_on_an_unpinned_tool():
  31. ci = pins("install-command: pip install pytest==9.2.0 pyyaml==6.0.2")
  32. assert pins("pytest==9.2.1\npyyaml==6.0.2\n") != ci
  33. assert pins("install-command: pip install pytest pyyaml==6.0.2") != ci
  34. assert pins("pytest==9.2.0\npyyaml==6.0.2\npytest-xdist==3.8.0\n") == ci