106 lines
3.2 KiB
Python
106 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
from importlib import metadata
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import sys
|
|
import unittest
|
|
|
|
|
|
REPOSITORY_ROOT = Path(__file__).resolve().parents[1]
|
|
REQUIREMENTS_PATH = REPOSITORY_ROOT / "requirements.txt"
|
|
CONSTRAINTS_PATH = (
|
|
REPOSITORY_ROOT / "constraints" / "python312-direct.txt"
|
|
)
|
|
PYTHON_VERSION_PATH = REPOSITORY_ROOT / ".python-version"
|
|
VERIFY_ENVIRONMENT_VARIABLE = "SYSTEM_SIMULATION_VERIFY_LOCKED_ENV"
|
|
|
|
REFERENCE_DIRECT_VERSIONS = {
|
|
"fastapi": "0.141.1",
|
|
"lxml": "6.1.1",
|
|
"numpy": "2.5.2",
|
|
"pydantic": "2.13.4",
|
|
"scipy": "1.18.0",
|
|
"uvicorn": "0.52.3",
|
|
}
|
|
|
|
|
|
def _active_lines(path: Path) -> tuple[str, ...]:
|
|
return tuple(
|
|
line
|
|
for raw_line in path.read_text(encoding="utf-8").splitlines()
|
|
if (line := raw_line.strip()) and not line.startswith("#")
|
|
)
|
|
|
|
|
|
def _normalized_name(requirement: str) -> str:
|
|
match = re.match(r"[A-Za-z0-9_.-]+", requirement)
|
|
if match is None:
|
|
raise AssertionError(f"Invalid requirement line: {requirement!r}")
|
|
return match.group(0).lower().replace("_", "-")
|
|
|
|
|
|
def _constraint_versions() -> dict[str, str]:
|
|
result: dict[str, str] = {}
|
|
for line in _active_lines(CONSTRAINTS_PATH):
|
|
parts = line.split("==")
|
|
if len(parts) != 2 or not all(parts):
|
|
raise AssertionError(
|
|
f"Direct constraint must be an exact package pin: {line!r}"
|
|
)
|
|
name, version = parts
|
|
normalized = _normalized_name(name)
|
|
if normalized in result:
|
|
raise AssertionError(f"Duplicate direct constraint: {normalized}")
|
|
result[normalized] = version
|
|
return result
|
|
|
|
|
|
class DependencyConstraintContractTests(unittest.TestCase):
|
|
def test_python_reference_version_is_explicit(self) -> None:
|
|
self.assertEqual(
|
|
PYTHON_VERSION_PATH.read_text(encoding="utf-8").strip(),
|
|
"3.12.3",
|
|
)
|
|
|
|
def test_requirements_remain_ranges_and_declare_every_direct_package(
|
|
self,
|
|
) -> None:
|
|
requirements = _active_lines(REQUIREMENTS_PATH)
|
|
names = {_normalized_name(line) for line in requirements}
|
|
|
|
self.assertEqual(names, set(REFERENCE_DIRECT_VERSIONS))
|
|
self.assertTrue(all("==" not in line for line in requirements))
|
|
|
|
def test_reference_constraints_pin_only_direct_cross_platform_packages(
|
|
self,
|
|
) -> None:
|
|
self.assertEqual(_constraint_versions(), REFERENCE_DIRECT_VERSIONS)
|
|
self.assertTrue(
|
|
{
|
|
"httptools",
|
|
"pyyaml",
|
|
"uvloop",
|
|
"watchfiles",
|
|
"websockets",
|
|
}.isdisjoint(_constraint_versions())
|
|
)
|
|
|
|
@unittest.skipUnless(
|
|
os.getenv(VERIFY_ENVIRONMENT_VARIABLE, "").strip().lower()
|
|
in {"1", "true", "yes", "on"},
|
|
f"Set {VERIFY_ENVIRONMENT_VARIABLE}=1 to verify installed versions.",
|
|
)
|
|
def test_installed_environment_matches_reference_constraints(self) -> None:
|
|
self.assertEqual(sys.version_info[:3], (3, 12, 3))
|
|
installed = {
|
|
name: metadata.version(name)
|
|
for name in REFERENCE_DIRECT_VERSIONS
|
|
}
|
|
self.assertEqual(installed, REFERENCE_DIRECT_VERSIONS)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|