"""Source binding refuses executable project caches without discarding evidence.""" import importlib.util import os from pathlib import Path import py_compile import subprocess import pytest from scripts import release_source_identity as identity from scripts.release_bytecode_policy import reject_project_bytecode from scripts.release_checkout_identity import verify_checkout from tests import test_release_bytecode_policy as bytecode_tests from tests.test_release_bytecode_policy import install, site_directory, verify environment = bytecode_tests.environment @pytest.mark.parametrize("name", ["obliteratus/example.py", "app.py"]) @pytest.mark.parametrize("legacy", [False, True]) def test_matching_timestamp_wrong_code_still_refuses_and_retains_cache(environment, name, legacy): install(environment) site = site_directory(environment) source = site / name source.parent.mkdir(parents=True, exist_ok=True) source.write_text("value = 1\n") cache = source.with_suffix(".pyc") if legacy else Path(importlib.util.cache_from_source(str(source))) py_compile.compile(str(source), cfile=str(cache), doraise=True, invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP) cached = cache.read_bytes() timestamp = source.stat().st_mtime_ns source.write_text("value = 2\n") # Same length and mtime: a timestamp cache remains loadable. os.utime(source, ns=(timestamp, timestamp)) assert int.from_bytes(cached[8:12], "little") == int(source.stat().st_mtime) assert int.from_bytes(cached[12:16], "little") == source.stat().st_size result = verify(environment, check=False) assert result.returncode != 0 and "project bytecode" in result.stderr assert cache.read_bytes() == cached assert source.read_text() == "value = 2\n" def test_dependency_cache_remains_usable_under_unchanged_no_write_policy(environment): install(environment) site = site_directory(environment) dependency = site / "dependency_probe.py" dependency.write_text("value = 17\n") cache = Path(py_compile.compile(str(dependency), doraise=True)) before = cache.read_bytes() assert verify(environment).returncode == 0 result = subprocess.run([environment / "bin/python", "-I", "-c", "import dependency_probe, sys; assert sys.dont_write_bytecode; " "assert dependency_probe.value == 17"], capture_output=True, text=True, timeout=30) assert result.returncode == 0, result.stdout + result.stderr assert cache.read_bytes() == before def test_project_directory_alias_cannot_hide_executable_cache(tmp_path): package = tmp_path / "obliteratus" package.mkdir() outside = tmp_path / "outside" outside.mkdir() (outside / "hidden.pyc").write_bytes(b"unbound") (package / "analysis").symlink_to(outside, target_is_directory=True) with pytest.raises(ValueError, match="cache directory alias"): reject_project_bytecode(package) assert (outside / "hidden.pyc").read_bytes() == b"unbound" @pytest.mark.parametrize("entrypoint", ["source", "report"]) def test_checkout_project_cache_fails_before_source_or_report_acceptance(tmp_path, entrypoint): package = tmp_path / "obliteratus" package.mkdir() cache = package / "orphan.pyc" cache.write_bytes(b"unbound") with pytest.raises(ValueError, match="project bytecode"): if entrypoint == "source": identity.source_identity(tmp_path) else: identity.check_report_source(tmp_path, {}) assert cache.read_bytes() == b"unbound" @pytest.mark.parametrize("relative", ["__pycache__/app.cpython-312.pyc", "app.pyc"]) def test_real_git_guard_rejects_root_caches_even_when_ignored(tmp_path, monkeypatch, relative): root = Path(__file__).parents[1] revision = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=root, text=True).strip() objects = subprocess.check_output(["git", "rev-parse", "--git-path", "objects"], cwd=root, text=True).strip() monkeypatch.setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", str((root / objects).resolve())) checkout = tmp_path / "checkout" subprocess.run(["git", "init", "--quiet", str(checkout)], check=True) subprocess.run(["git", "checkout", "--quiet", "--detach", revision], cwd=checkout, check=True) verify_checkout(checkout, revision) cache = checkout / relative cache.parent.mkdir(parents=True, exist_ok=True) cache.write_bytes(b"unbound") with pytest.raises(ValueError, match="uncommitted test/source/config"): verify_checkout(checkout, revision) assert cache.read_bytes() == b"unbound"