mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
133 lines
5.9 KiB
Python
133 lines
5.9 KiB
Python
"""Commit-bound files cannot be replaced by dirty, staged, or ignored test inputs."""
|
|
|
|
import hashlib
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
from scripts import release_checkout_identity as identity
|
|
from scripts import run_release_partition as runner
|
|
|
|
|
|
SHA = "a" * 40
|
|
|
|
|
|
@pytest.fixture
|
|
def checkout(tmp_path, monkeypatch):
|
|
files = {"tests/test_case.py": b"def test_case(): assert True\n",
|
|
"tests/conftest.py": b"# fixture\n", "pyproject.toml": b"[project]\n",
|
|
"ci/policy.json": b"{}\n", "tests/fixture.bin": b"\x01\x02"}
|
|
entries = []
|
|
# Ask Git itself for blob IDs; the checker must match its object semantics.
|
|
for name, contents in files.items():
|
|
path = tmp_path / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(contents)
|
|
oid = subprocess.check_output(["git", "hash-object", "--no-filters", str(path)]).strip()
|
|
entries.append(b"100644 blob " + oid + b"\t" + os.fsencode(name) + b"\0")
|
|
tree = b"".join(entries)
|
|
extra = []
|
|
|
|
def git(command, **kwargs):
|
|
assert kwargs["cwd"] == tmp_path
|
|
if command[1] == "ls-tree":
|
|
assert command[-1] == SHA
|
|
return tree
|
|
assert command[1:5] == ["ls-files", "--cached", "--others", "-z"]
|
|
assert "--exclude-standard" not in command
|
|
return b"\0".join(os.fsencode(name) for name in [*files, *extra]) + b"\0"
|
|
|
|
monkeypatch.setattr(identity.subprocess, "check_output", git)
|
|
return tmp_path, extra, hashlib.sha256(tree).hexdigest()
|
|
|
|
|
|
def test_exact_tree_binds_git_blob_content(checkout):
|
|
root, _, expected = checkout
|
|
assert identity.verify_checkout(root, SHA) == expected
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["tests/test_case.py", "tests/conftest.py", "pyproject.toml", "ci/policy.json", "tests/fixture.bin"])
|
|
def test_dirty_test_fixture_and_config_bytes_fail_even_if_git_head_is_unchanged(checkout, name):
|
|
root, _, _ = checkout
|
|
path = root / name
|
|
path.write_bytes(path.read_bytes() + b"# substituted\n")
|
|
with pytest.raises(ValueError, match="differs from requested commit"):
|
|
identity.verify_checkout(root, SHA)
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["tests/test_added.py", "tests/ignored/test_substitute.py", "tests/data.json", "conftest.py", "pytest.ini"])
|
|
def test_new_staged_untracked_or_ignored_execution_inputs_fail(checkout, name):
|
|
root, extra, _ = checkout
|
|
path = root / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text("# injected input\n")
|
|
extra.append(name)
|
|
with pytest.raises(ValueError, match="uncommitted test/source/config"):
|
|
identity.verify_checkout(root, SHA)
|
|
|
|
|
|
@pytest.mark.parametrize("change", ["missing", "symlink", "mode", "parent"])
|
|
def test_missing_or_substituted_files_cannot_pass(checkout, tmp_path, change):
|
|
root, _, _ = checkout
|
|
path = root / "tests/test_case.py"
|
|
if change == "missing":
|
|
path.unlink()
|
|
elif change == "mode":
|
|
path.chmod(0o755)
|
|
elif change == "parent":
|
|
(root / "tests").rename(root / "outside")
|
|
(root / "tests").symlink_to(root / "outside", target_is_directory=True)
|
|
else:
|
|
other = root / "outside.py"
|
|
other.write_bytes(path.read_bytes())
|
|
path.unlink()
|
|
path.symlink_to(other)
|
|
with pytest.raises((ValueError, FileNotFoundError)):
|
|
identity.verify_checkout(root, SHA)
|
|
|
|
|
|
def test_metadata_requires_checkout_verification_before_source_inventory(monkeypatch, tmp_path):
|
|
monkeypatch.setattr(runner.subprocess, "check_output", lambda *a, **kw: SHA + "\n")
|
|
monkeypatch.setattr(runner, "release_extras", lambda *a: ["dev"])
|
|
|
|
def dirty(root, revision):
|
|
assert (root, revision) == (tmp_path, SHA)
|
|
raise ValueError("dirty test body")
|
|
|
|
monkeypatch.setattr(runner, "verify_checkout", dirty)
|
|
with pytest.raises(ValueError, match="dirty test body"):
|
|
runner.target_metadata(tmp_path, SHA, SHA, "head")
|
|
|
|
|
|
def test_real_git_tree_rejects_dirty_assume_unchanged_and_ignored_or_staged_tests(tmp_path, monkeypatch):
|
|
source = Path(__file__).parents[1]
|
|
revision = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=source, text=True).strip()
|
|
objects = subprocess.check_output(["git", "rev-parse", "--git-path", "objects"], cwd=source, text=True).strip()
|
|
# Read existing objects through an alternate; all index and checkout writes
|
|
# stay inside this disposable repository. No commit or shared-Git mutation.
|
|
monkeypatch.setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", str((source / objects).resolve()))
|
|
subprocess.run(["git", "init", "--quiet", str(tmp_path)], check=True, capture_output=True)
|
|
subprocess.run(["git", "read-tree", revision], cwd=tmp_path, check=True)
|
|
subprocess.run(["git", "checkout-index", "--all"], cwd=tmp_path, check=True)
|
|
clean = identity.verify_checkout(tmp_path, revision)
|
|
path = tmp_path / "tests/conftest.py"
|
|
original = path.read_bytes()
|
|
subprocess.run(["git", "update-index", "--assume-unchanged", "tests/conftest.py"], cwd=tmp_path, check=True)
|
|
path.write_bytes(original + b"\n# substituted fixture\n")
|
|
with pytest.raises(ValueError, match="differs from requested commit"):
|
|
identity.verify_checkout(tmp_path, revision)
|
|
path.write_bytes(original)
|
|
extra = tmp_path / "tests/test_substitution.py"
|
|
(tmp_path / ".git/info/exclude").write_text("tests/test_substitution.py\n")
|
|
extra.write_text("def test_substitute(): pass\n")
|
|
with pytest.raises(ValueError, match="uncommitted test/source/config"):
|
|
identity.verify_checkout(tmp_path, revision)
|
|
subprocess.run(["git", "add", "--force", "tests/test_substitution.py"], cwd=tmp_path, check=True)
|
|
with pytest.raises(ValueError, match="uncommitted test/source/config"):
|
|
identity.verify_checkout(tmp_path, revision)
|
|
subprocess.run(["git", "rm", "--cached", "--quiet", "tests/test_substitution.py"], cwd=tmp_path, check=True)
|
|
extra.unlink()
|
|
assert identity.verify_checkout(tmp_path, revision) == clean
|