|
| 1 | +"""The TypedPython gate bundle published beside the KSP processor (#20, toolchain#90). |
| 2 | +
|
| 3 | +toolchain's typedpythonCheck resolves `python-multiplatform-ksp:<version>:typedpython-gate@zip`, |
| 4 | +unzips it and installs the gate from it with uv `--require-hashes`, so the zip must hold the wheel |
| 5 | +under its real file name and a manifest with every hash the install needs. |
| 6 | +""" |
| 7 | +import hashlib |
| 8 | +import json |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import tomllib |
| 12 | +import zipfile |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +MODULE = Path(__file__).resolve().parents[3] # python-multiplatform-ksp |
| 18 | +SCRIPT = MODULE / "gate_bundle.py" |
| 19 | +LOCK = MODULE / "typedpython-gate.lock.json" |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope="module") |
| 23 | +def bundle(tmp_path_factory): |
| 24 | + out = tmp_path_factory.mktemp("gate") / "typedpython-gate.zip" |
| 25 | + run = subprocess.run([sys.executable, str(SCRIPT), "--out", str(out)], capture_output=True, text=True) |
| 26 | + assert run.returncode == 0, run.stdout + run.stderr |
| 27 | + return out |
| 28 | + |
| 29 | + |
| 30 | +def _pyproject(): |
| 31 | + return tomllib.loads((MODULE / "pyproject.toml").read_text()) |
| 32 | + |
| 33 | + |
| 34 | +def test_zip_holds_the_wheel_under_its_real_name_and_a_manifest(bundle): |
| 35 | + names = zipfile.ZipFile(bundle).namelist() |
| 36 | + version = _pyproject()["project"]["version"] |
| 37 | + assert sorted(names) == sorted([f"typedpython-{version}-py3-none-any.whl", "typedpython-gate.json"]) |
| 38 | + |
| 39 | + |
| 40 | +def test_manifest_matches_the_wheel_and_the_pins(bundle): |
| 41 | + z = zipfile.ZipFile(bundle) |
| 42 | + m = json.loads(z.read("typedpython-gate.json")) |
| 43 | + project = _pyproject()["project"] |
| 44 | + assert m["format"] == 1 |
| 45 | + assert m["typedpython"]["version"] == project["version"] |
| 46 | + wheel = m["typedpython"]["wheel"] |
| 47 | + assert m["typedpython"]["sha256"] == hashlib.sha256(z.read(wheel)).hexdigest() |
| 48 | + # the pyrefly pin is the one the wheel itself depends on, with a hash for every published wheel |
| 49 | + assert f"pyrefly=={m['pyrefly']['version']}" in project["dependencies"] |
| 50 | + assert m["pyrefly"]["sha256"] == json.loads(LOCK.read_text())["pyrefly"]["sha256"] |
| 51 | + assert any("macosx_11_0_arm64" in f for f in m["pyrefly"]["sha256"]) |
| 52 | + assert all(f.endswith(".whl") for f in m["pyrefly"]["sha256"]) |
| 53 | + |
| 54 | + |
| 55 | +def test_lock_pins_the_same_pyrefly_as_the_package(): |
| 56 | + lock = json.loads(LOCK.read_text())["pyrefly"] |
| 57 | + assert f"pyrefly=={lock['version']}" in _pyproject()["project"]["dependencies"] |
| 58 | + assert all(f.startswith(f"pyrefly-{lock['version']}-") for f in lock["sha256"]) |
| 59 | + |
| 60 | + |
| 61 | +def test_the_wheel_installs_with_hashes_and_checks_code(bundle, tmp_path): |
| 62 | + # the install toolchain does, minus the network: typedpython from the unzipped dir with its hash |
| 63 | + # (--no-deps here: pyrefly comes from PyPI there, from this test's own interpreter here) |
| 64 | + d = tmp_path / "unzipped" |
| 65 | + zipfile.ZipFile(bundle).extractall(d) |
| 66 | + m = json.loads((d / "typedpython-gate.json").read_text()) |
| 67 | + req = tmp_path / "req.txt" |
| 68 | + req.write_text(f"typedpython=={m['typedpython']['version']} --hash=sha256:{m['typedpython']['sha256']}\n") |
| 69 | + target = tmp_path / "site" |
| 70 | + run = subprocess.run(["uv", "pip", "install", "--python", sys.executable, "--target", str(target), |
| 71 | + "--no-index", "--find-links", str(d), "--no-deps", "--require-hashes", |
| 72 | + "-r", str(req)], capture_output=True, text=True) |
| 73 | + assert run.returncode == 0, run.stdout + run.stderr |
| 74 | + assert (target / "typedpython" / "__main__.py").is_file() |
| 75 | + clean = tmp_path / "clean.py" |
| 76 | + clean.write_text("def f(x: int) -> int:\n return x + 1\n") |
| 77 | + env = {"PYTHONPATH": str(target), "PATH": __import__("os").environ["PATH"], |
| 78 | + "TMPDIR": str(tmp_path)} |
| 79 | + check = subprocess.run([sys.executable, "-m", "typedpython", "check", str(clean)], |
| 80 | + capture_output=True, text=True, env=env) |
| 81 | + assert check.returncode == 0, check.stdout + check.stderr |
| 82 | + |
| 83 | + |
| 84 | +def test_the_wheel_carries_the_c_runtime_and_the_builtin_stub(bundle): |
| 85 | + # compiling needs tp_runtime.h; checking needs the `compiled` builtin stub |
| 86 | + import io |
| 87 | + z = zipfile.ZipFile(bundle) |
| 88 | + wheel = json.loads(z.read("typedpython-gate.json"))["typedpython"]["wheel"] |
| 89 | + names = zipfile.ZipFile(io.BytesIO(z.read(wheel))).namelist() |
| 90 | + for needed in ("typedpython/runtime/tp_runtime.h", "typedpython/builtin_stubs/__builtins__.pyi", |
| 91 | + "typedpython_builtins.pth", "typedpython/__main__.py"): |
| 92 | + assert needed in names, needed |
0 commit comments