mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Prepare only locked dependencies in owned release environments before installing the exact project. Preserve verified wheel-supplied caches outside import paths, require zero caches before compilation, and bind generated cache bytes to source and retained diagnostics. Keep project caches forbidden, preserve the no-write startup policy, sanitize inherited compilation settings, and retain bootstrap evidence separately without changing test selection or release gates. Refs #306
249 lines
11 KiB
Python
249 lines
11 KiB
Python
"""Retained real cache bytes exercise dependency bootstrap refusal boundaries."""
|
|
|
|
import importlib.metadata
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import py_compile
|
|
import shutil
|
|
import subprocess
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from scripts import release_dependency_bytecode as bootstrap
|
|
|
|
|
|
def cache(source):
|
|
return Path(py_compile.compile(str(source), doraise=True,
|
|
invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP))
|
|
|
|
|
|
@pytest.fixture
|
|
def setup(tmp_path, monkeypatch):
|
|
root, environment = tmp_path / "source", tmp_path / "environment"
|
|
site = environment / "site-packages"
|
|
site.mkdir(parents=True)
|
|
(root / "obliteratus").mkdir(parents=True)
|
|
(root / "obliteratus/__init__.py").write_text("value = 17\n")
|
|
(root / "app.py").write_text("value = 23\n")
|
|
(root / "uv.lock").write_text("version = 1\n")
|
|
(root / "pyproject.toml").write_text(
|
|
'[project]\nname="obliteratus"\n[project.optional-dependencies]\ndev=[]\nrecovery=[]\nmcp=[]\n')
|
|
uv = tmp_path / "uv"
|
|
uv.write_text('#!/bin/sh\necho "uv 0.12.4 (test fixture)"\n')
|
|
uv.chmod(0o755)
|
|
dependency = site / "dependency.py"
|
|
calls = []
|
|
state = SimpleNamespace(inject=None, root=root, environment=environment, site=site, uv=uv,
|
|
dependency=dependency, calls=calls, receipt=tmp_path / "diagnostics/receipt.json")
|
|
monkeypatch.setenv("UV_COMPILE_BYTECODE", "true")
|
|
# Venv ownership and real uv installation are tested by the separate real
|
|
# venv suite/bootstrap matrix. Here only those boundaries are substituted.
|
|
monkeypatch.setattr(bootstrap, "owned_site", lambda value: site if value == environment else pytest.fail("wrong venv"))
|
|
monkeypatch.setattr(bootstrap, "verify_checkout", lambda *args: "c" * 64)
|
|
original_run = bootstrap.subprocess.run
|
|
monkeypatch.setattr(bootstrap.subprocess, "check_output", lambda command, **kwargs:
|
|
"a" * 40 + "\n" if command[0] == "git" else
|
|
original_run(command, capture_output=True, text=True, check=True).stdout)
|
|
distribution = importlib.metadata.distribution
|
|
monkeypatch.setattr(importlib.metadata, "distribution", lambda name:
|
|
SimpleNamespace(locate_file=lambda name: site / name)
|
|
if name == "obliteratus" else distribution(name))
|
|
|
|
def sync(command, *, cwd, env, check):
|
|
assert cwd == root and env["UV_PROJECT_ENVIRONMENT"] == str(environment) and check
|
|
assert "UV_COMPILE_BYTECODE" not in env
|
|
calls.append(command)
|
|
if not dependency.exists():
|
|
dependency.write_text("value = 1\n")
|
|
if state.inject:
|
|
state.inject(command)
|
|
if "--compile-bytecode" in command:
|
|
cache(dependency)
|
|
|
|
monkeypatch.setattr(bootstrap.subprocess, "run", sync)
|
|
return state
|
|
|
|
|
|
def compile_dependencies(setup):
|
|
bootstrap.compile_dependencies(setup.root, setup.environment, str(setup.uv), setup.receipt)
|
|
|
|
|
|
def install_project(setup):
|
|
shutil.copytree(setup.root / "obliteratus", setup.site / "obliteratus")
|
|
shutil.copyfile(setup.root / "app.py", setup.site / "app.py")
|
|
|
|
|
|
def verify(setup):
|
|
bootstrap.verify_dependencies(setup.root, setup.environment, str(setup.uv), setup.receipt)
|
|
|
|
|
|
def test_final_extras_install_without_compilation_then_new_caches_bind_and_survive_project_sync(setup, capsys):
|
|
compile_dependencies(setup)
|
|
logged = [json.loads(line) for line in capsys.readouterr().out.splitlines()]
|
|
assert logged[0]["environment_removed_for_explicit_flags"] == ["UV_COMPILE_BYTECODE"]
|
|
first, second = setup.calls
|
|
assert "--no-compile-bytecode" in first and "--compile-bytecode" not in first
|
|
assert "--compile-bytecode" in second and "--no-compile-bytecode" not in second
|
|
for command in setup.calls:
|
|
assert "--no-install-project" in command and "--no-editable" in command
|
|
assert [command[i + 1] for i, flag in enumerate(command) if flag == "--extra"] == ["dev", "recovery", "mcp"]
|
|
recorded = json.loads(setup.receipt.read_text())
|
|
assert recorded["sources"] == {"dependency.py": bootstrap.digest(setup.dependency)}
|
|
entry = next(iter(recorded["caches"].values()))
|
|
assert entry["mode"] == "timestamp" and entry["source"] == "dependency.py"
|
|
assert recorded["helpers"]["release_dependency_bytecode.py"] == bootstrap.digest(Path(bootstrap.__file__))
|
|
before = setup.receipt.read_bytes()
|
|
install_project(setup)
|
|
verify(setup)
|
|
assert setup.receipt.read_bytes() == before
|
|
|
|
|
|
def test_final_dependency_install_cannot_supply_stale_timestamp_cache(setup):
|
|
preserved = {}
|
|
|
|
def inject(command):
|
|
assert "--no-compile-bytecode" in command
|
|
path = cache(setup.dependency)
|
|
timestamp = setup.dependency.stat().st_mtime_ns
|
|
setup.dependency.write_text("value = 2\n")
|
|
os.utime(setup.dependency, ns=(timestamp, timestamp))
|
|
preserved.update(path=path, data=path.read_bytes())
|
|
|
|
setup.inject = inject
|
|
with pytest.raises(ValueError, match="cache-free"):
|
|
compile_dependencies(setup)
|
|
assert len(setup.calls) == 1 and "--compile-bytecode" not in setup.calls[0]
|
|
assert preserved["path"].read_bytes() == preserved["data"]
|
|
assert not setup.receipt.exists()
|
|
|
|
|
|
@pytest.mark.parametrize("present", ["obliteratus", "obliteratus-0.1.dist-info", "app.py", "app.pyc"])
|
|
def test_project_presence_refuses_before_any_sync(setup, present):
|
|
(setup.site / present).write_bytes(b"existing owner")
|
|
with pytest.raises(ValueError, match="project"):
|
|
compile_dependencies(setup)
|
|
assert setup.calls == [] and (setup.site / present).read_bytes() == b"existing owner"
|
|
|
|
|
|
def test_preexisting_receipt_is_never_rebound(setup):
|
|
setup.receipt.parent.mkdir()
|
|
setup.receipt.write_text("earlier evidence")
|
|
with pytest.raises(ValueError, match="never rebind"):
|
|
compile_dependencies(setup)
|
|
assert not setup.calls and setup.receipt.read_text() == "earlier evidence"
|
|
|
|
|
|
def test_source_changes_during_compilation_cannot_be_blessed(setup):
|
|
setup.inject = lambda command: setup.dependency.write_text("value = 2\n") if "--compile-bytecode" in command else None
|
|
with pytest.raises(ValueError, match="source bytes changed during compilation"):
|
|
compile_dependencies(setup)
|
|
assert len(setup.calls) == 2 and not setup.receipt.exists()
|
|
before = json.loads(setup.receipt.with_name("receipt-precompile.json").read_text())
|
|
assert before["stage"] == "ready-to-compile" and before["cache_free"] is True
|
|
assert before["sources"]["dependency.py"] != bootstrap.digest(setup.dependency)
|
|
|
|
|
|
def test_failed_compiler_preserves_completed_precompilation_inventory(setup):
|
|
def fail(command):
|
|
if "--compile-bytecode" in command:
|
|
raise subprocess.CalledProcessError(42, command)
|
|
setup.inject = fail
|
|
with pytest.raises(subprocess.CalledProcessError):
|
|
compile_dependencies(setup)
|
|
path = setup.receipt.with_name("receipt-precompile.json")
|
|
before = json.loads(path.read_text())
|
|
assert before["cache_free"] is True and before["sources"] == {"dependency.py": bootstrap.digest(setup.dependency)}
|
|
assert before["target_sha"] == "a" * 40 and before["uv"]["sha256"] == bootstrap.digest(setup.uv)
|
|
assert not setup.receipt.exists()
|
|
retained = path.read_bytes()
|
|
with pytest.raises(ValueError, match="never rebind"):
|
|
compile_dependencies(setup)
|
|
assert path.read_bytes() == retained and len(setup.calls) == 2
|
|
|
|
|
|
def test_dotted_dependency_filename_uses_forward_cache_mapping(setup):
|
|
source = setup.site / "v0.9.0.a.py"
|
|
source.write_text("value = 17\n")
|
|
generated = cache(source)
|
|
entry = bootstrap.inventory(setup.site)["caches"][generated.relative_to(setup.site).as_posix()]
|
|
assert entry["source"] == source.name and entry["sha256"] == bootstrap.digest(generated)
|
|
|
|
|
|
@pytest.mark.parametrize("change", ["source", "cache", "new-cache", "lock", "helper-binding", "project-cache", "app-source"])
|
|
def test_post_install_verification_refuses_drift_without_deleting_evidence(setup, change):
|
|
compile_dependencies(setup)
|
|
install_project(setup)
|
|
bytecode = Path(importlib.util.cache_from_source(str(setup.dependency)))
|
|
if change == "source":
|
|
timestamp = setup.dependency.stat().st_mtime_ns
|
|
setup.dependency.write_text("value = 2\n")
|
|
os.utime(setup.dependency, ns=(timestamp, timestamp))
|
|
elif change == "cache":
|
|
bytecode.write_bytes(bytecode.read_bytes() + b"unbound payload")
|
|
elif change == "new-cache":
|
|
(setup.site / "orphan.pyc").write_bytes(b"orphan")
|
|
elif change == "lock":
|
|
(setup.root / "uv.lock").write_text("version = 2\n")
|
|
elif change == "helper-binding":
|
|
receipt = json.loads(setup.receipt.read_text())
|
|
receipt["helpers"]["release_dependency_bytecode.py"] = "0" * 64
|
|
setup.receipt.write_text(json.dumps(receipt))
|
|
elif change == "project-cache":
|
|
cache(setup.site / "obliteratus/__init__.py")
|
|
else:
|
|
(setup.site / "app.py").write_text("value = 24\n")
|
|
retained = bytecode.read_bytes()
|
|
with pytest.raises(ValueError):
|
|
verify(setup)
|
|
assert bytecode.read_bytes() == retained and setup.receipt.exists()
|
|
|
|
|
|
@pytest.mark.parametrize("change", ["bad-magic", "short", "unchecked", "invalid-flags", "wrong-time", "symlink", "directory-alias"])
|
|
def test_invalid_cache_scope_and_headers_are_not_accepted(setup, change):
|
|
setup.dependency.write_text("value = 1\n")
|
|
path = cache(setup.dependency)
|
|
data = bytearray(path.read_bytes())
|
|
if change == "bad-magic":
|
|
data[:4] = b"BAD!"
|
|
elif change == "short":
|
|
data = data[:10]
|
|
elif change in {"unchecked", "invalid-flags"}:
|
|
data[4:8] = (1 if change == "unchecked" else 2).to_bytes(4, "little")
|
|
elif change == "wrong-time":
|
|
data[8:12] = b"\0" * 4
|
|
elif change == "symlink":
|
|
actual = path.with_name("actual-cache")
|
|
path.rename(actual)
|
|
path.symlink_to(actual)
|
|
else:
|
|
(setup.site / "alias").symlink_to(path.parent, target_is_directory=True)
|
|
if change not in {"symlink", "directory-alias"}:
|
|
path.write_bytes(data)
|
|
with pytest.raises(ValueError):
|
|
bootstrap.inventory(setup.site)
|
|
assert path.exists()
|
|
|
|
|
|
def test_checked_hash_cache_is_bound_to_actual_source_bytes(setup):
|
|
setup.dependency.write_text("value = 1\n")
|
|
path = Path(py_compile.compile(str(setup.dependency), doraise=True,
|
|
invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH))
|
|
assert next(iter(bootstrap.inventory(setup.site)["caches"].values()))["mode"] == "checked-hash"
|
|
setup.dependency.write_text("value = 2\n")
|
|
with pytest.raises(ValueError, match="header differs"):
|
|
bootstrap.inventory(setup.site)
|
|
assert path.is_file()
|
|
|
|
|
|
def test_wrong_uv_and_redirected_cache_scope_refuse_before_mutation(setup, monkeypatch):
|
|
setup.uv.write_text('#!/bin/sh\necho "uv 0.9.26"\n')
|
|
with pytest.raises(ValueError, match="requires uv 0.12.4"):
|
|
compile_dependencies(setup)
|
|
monkeypatch.setenv("PYTHONPYCACHEPREFIX", str(setup.root / "outside"))
|
|
with pytest.raises(ValueError, match="owned site-packages"):
|
|
compile_dependencies(setup)
|
|
assert not setup.calls
|