Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

10.9 KB
Raw Blame
"""Check reviewed pip refresh inputs and installation boundaries offline."""

from contextlib import contextmanager
import hashlib
import importlib.util
import json
from pathlib import Path
import sys
import tempfile
from types import SimpleNamespace
import unittest
from unittest.mock import patch


ROOT = Path(__file__).resolve().parents[3]
SPEC = importlib.util.spec_from_file_location(
    "pip_vendor_refresh", ROOT / "scripts/refresh_pip_urllib3.py"
)
refresh = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(refresh)


class RefreshInstallationChecks(unittest.TestCase):
    def setUp(self):
        temporary = tempfile.TemporaryDirectory()
        self.addCleanup(temporary.cleanup)
        self.root = Path(temporary.name)
        self.bundle = self.root / "ensurepip/_bundled" / refresh.PIP_WHEEL
        self.bundle.parent.mkdir(parents=True)
        self.original = b"synthetic reviewed seed wheel"
        self.rebuilt = b"synthetic refreshed wheel"
        self.bundle.write_bytes(self.original)
        self.wheel = self.root / "rebuilt" / refresh.PIP_WHEEL
        self.wheel.parent.mkdir()
        self.wheel.write_bytes(self.rebuilt)
        self.verifier = self.root / "verify.py"
        self.verifier.write_text("# Synthetic verifier\n", encoding="utf-8")
        self.pip = SimpleNamespace(__version__="26.2.2")
        self.ensurepip = SimpleNamespace(
            __file__=str(self.bundle.parent.parent / "__init__.py"),
            version=lambda: "26.2.2",
            _get_pip_whl_path_ctx=self.selected_bundle,
        )

    @contextmanager
    def selected_bundle(self):
        yield self.bundle

    @contextmanager
    def installation(self):
        # No package installation or external process executes in these checks.
        with (
            patch.dict(
                sys.modules, {"pip": self.pip, "ensurepip": self.ensurepip}
            ),
            patch.dict(
                refresh.INPUT_HASHES,
                {refresh.PIP_WHEEL: hashlib.sha256(self.original).hexdigest()},
            ),
            patch.object(refresh.subprocess, "run") as run,
        ):
            yield run

    def test_reviewed_pair_refreshes_seed_and_checks_fresh_interpreters(self):
        with self.installation() as run:
            refresh.install(self.wheel, self.verifier)
        self.assertEqual(self.bundle.read_bytes(), self.rebuilt)
        commands = [call.args[0] for call in run.call_args_list]
        self.assertEqual(len(commands), 5)
        self.assertEqual(
            commands[0][:5], [sys.executable, "-I", "-m", "pip", "install"]
        )
        for option in ("--no-index", "--no-deps", "--force-reinstall"):
            self.assertIn(option, commands[0])
        expected = [
            "-I",
            str(self.verifier),
            refresh.URLLIB3_VERSION,
            refresh.MSGPACK_VERSION,
        ]
        self.assertEqual(commands[1], [sys.executable, *expected])
        self.assertEqual(commands[-1][1:], expected)
        self.assertEqual(commands[-2][:4], [sys.executable, "-I", "-m", "venv"])
        self.assertNotEqual(commands[-1][0], sys.executable)
        self.assertTrue(
            all(call.kwargs["check"] for call in run.call_args_list)
        )

    def test_repeat_refresh_accepts_only_the_same_rebuilt_seed(self):
        with self.installation() as run:
            refresh.install(self.wheel, self.verifier)
            refresh.install(self.wheel, self.verifier)
        self.assertEqual(self.bundle.read_bytes(), self.rebuilt)
        self.assertEqual(run.call_count, 10)

    def test_installed_or_seed_version_drift_is_refused_before_install(self):
        for component in ("pip", "ensurepip"):
            with self.subTest(component=component):
                if component == "pip":
                    self.pip.__version__ = "0.0"
                else:
                    self.pip.__version__ = "26.2.2"
                    self.ensurepip.version = lambda: "0.0"
                with self.installation() as run:
                    with self.assertRaisesRegex(
                        ValueError, "pip/ensurepip changed"
                    ):
                        refresh.install(self.wheel, self.verifier)
                    run.assert_not_called()
                self.assertEqual(self.bundle.read_bytes(), self.original)

    def test_alternative_seed_layout_is_refused_before_install(self):
        @contextmanager
        def alternative():
            yield self.root / "another-wheel.whl"

        self.ensurepip._get_pip_whl_path_ctx = alternative
        with self.installation() as run:
            with self.assertRaisesRegex(
                ValueError, "reviewed bundled-wheel layout"
            ):
                refresh.install(self.wheel, self.verifier)
            run.assert_not_called()
        self.assertEqual(self.bundle.read_bytes(), self.original)

    def test_changed_seed_bytes_are_refused_before_install(self):
        self.bundle.write_bytes(b"different synthetic seed")
        with self.installation() as run:
            with self.assertRaisesRegex(ValueError, "not the reviewed wheel"):
                refresh.install(self.wheel, self.verifier)
            run.assert_not_called()
        self.assertEqual(self.bundle.read_bytes(), b"different synthetic seed")

    def test_changed_input_is_refused_before_reading_zip_members(self):
        with patch.object(refresh, "ZipFile") as archive:
            with self.assertRaisesRegex(ValueError, "unreviewed wheel"):
                refresh.verified_wheel(self.wheel)
            archive.assert_not_called()


class RefreshConfigurationChecks(unittest.TestCase):
    def test_all_bootstrap_exports_use_the_reviewed_refresh_pip(self):
        for name in (
            "bootstrap-builder.txt",
            "bootstrap-ci.txt",
            "bootstrap-runtime.txt",
            "pip-vendor-refresh.txt",
        ):
            with self.subTest(manifest=name):
                lines = (
                    (ROOT / "requirements" / name)
                    .read_text(encoding="utf-8")
                    .splitlines()
                )
                entries = [
                    i
                    for i, line in enumerate(lines)
                    if line.startswith("pip==")
                ]
                self.assertEqual(len(entries), 1)
                index = entries[0]
                self.assertEqual(
                    lines[index].split()[0], f"pip=={refresh.PIP_VERSION}"
                )
                self.assertIn(
                    "--hash=sha256:" + refresh.INPUT_HASHES[refresh.PIP_WHEEL],
                    lines[index + 1],
                )


class RefreshInventoryChecks(unittest.TestCase):
    def inventory(self):
        references = {
            "urllib3": "pkg:pypi/[email protected]",
            "msgpack": "pkg:pypi/[email protected]",
        }
        components = [
            {
                "name": name,
                "version": reference.split("@")[-1],
                "purl": reference,
                "bom-ref": reference,
            }
            for name, reference in references.items()
        ]
        sbom = {
            "components": components,
            "dependencies": [
                {"ref": "pip", "dependsOn": list(references.values())},
                *({"ref": ref} for ref in references.values()),
            ],
        }
        return {
            refresh.VENDOR
            + "vendor.txt": b"    urllib3==2.7.0\nmsgpack==1.1.2\n",
            refresh.VENDOR + "bom.cdx.json": json.dumps(sbom).encode(),
        }

    def test_code_manifest_and_all_dependency_references_agree(self):
        files = self.inventory()
        for name, previous, version in (
            ("urllib3", "2.7.0", refresh.URLLIB3_VERSION),
            ("msgpack", "1.1.2", refresh.MSGPACK_VERSION),
        ):
            refresh.refresh_inventory(files, name, previous, version)
            manifest = files[refresh.VENDOR + "vendor.txt"].decode()
            self.assertIn(f"{name}=={version}\n", manifest)
            self.assertNotIn(f"{name}=={previous}\n", manifest)
            sbom = files[refresh.VENDOR + "bom.cdx.json"].decode()
            self.assertNotIn(f"pkg:pypi/{name}@{previous}", sbom)
            self.assertEqual(sbom.count(f"pkg:pypi/{name}@{version}"), 4)

    def test_changed_vendor_pin_is_refused(self):
        files = self.inventory()
        path = refresh.VENDOR + "vendor.txt"
        files[path] = files[path].replace(b"msgpack==1.1.2", b"msgpack==0.0")
        with self.assertRaisesRegex(ValueError, "vendor pin changed"):
            refresh.refresh_inventory(files, "msgpack", "1.1.2", "1.2.3")

    def test_changed_component_is_refused(self):
        for field in ("version", "purl", "bom-ref"):
            with self.subTest(field=field):
                files = self.inventory()
                path = refresh.VENDOR + "bom.cdx.json"
                sbom = json.loads(files[path])
                sbom["components"][1][field] = "unexpected"
                files[path] = json.dumps(sbom).encode()
                with self.assertRaisesRegex(ValueError, "SBOM"):
                    refresh.refresh_inventory(
                        files, "msgpack", "1.1.2", "1.2.3"
                    )

    def test_msgpack_keeps_only_upstream_python_files_and_license(self):
        upstream = {
            "msgpack/" + name: f"# {name}\n".encode()
            for name in (
                "__init__.py",
                "exceptions.py",
                "ext.py",
                "fallback.py",
            )
        }
        upstream["msgpack/_cmsgpack.so"] = b"not a vendored file"
        license_path = (
            f"msgpack-{refresh.MSGPACK_VERSION}.dist-info/licenses/COPYING"
        )
        upstream[license_path] = b"upstream license text"
        files = refresh.vendored_msgpack_sources(upstream)
        self.assertEqual(len(files), 5)
        self.assertNotIn(refresh.VENDOR + "msgpack/_cmsgpack.so", files)
        for path, content in upstream.items():
            if path.endswith(".py"):
                self.assertEqual(files[refresh.VENDOR + path], content)
        self.assertEqual(
            files[refresh.VENDOR + "msgpack/COPYING"],
            upstream[license_path],
        )

    def test_msgpack_source_layout_drift_is_refused(self):
        with self.assertRaisesRegex(ValueError, "Python layout changed"):
            refresh.vendored_msgpack_sources({"msgpack/new.py": b""})

    def test_missing_or_ambiguous_msgpack_input_is_refused(self):
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            for present in (False, True):
                with self.subTest(two_inputs=present):
                    if present:
                        for name in refresh.MSGPACK_INPUT_HASHES:
                            (root / name).touch()
                    with self.assertRaisesRegex(ValueError, "exactly one"):
                        refresh.build_wheel(root, root / "output")
                    self.assertFalse((root / "output").exists())


if __name__ == "__main__":
    unittest.main()