File size: 2,887 Bytes
1866d85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from __future__ import annotations

import hashlib
import tarfile
from pathlib import Path

import pytest

from autocad_bench.bundle import BUNDLE_VERSION
from autocad_bench.common.paths import BENCHMARK_ROOT
from autocad_bench.release import (
    CHECKSUM_FILENAME,
    DEFAULT_SOURCE_DATE_EPOCH,
    RELEASE_FILENAME,
    ReleaseError,
    _write_bundle_archive,
    build_release,
    verify_release,
)


def test_release_build_is_complete_and_self_verifying(tmp_path: Path) -> None:
    output = tmp_path / "release"
    built = build_release(
        output,
        source_root=BENCHMARK_ROOT,
        evaluator_versions=(),
    )

    assert built.source_date_epoch == DEFAULT_SOURCE_DATE_EPOCH
    assert built.package_name == "autocad-bench"
    assert built.package_version == "0.1.0"
    assert built.evaluator_versions == ()
    assert {artifact.kind for artifact in built.artifacts} == {
        "benchmark_bundle",
        "sdist",
        "wheel",
    }
    assert {path.name for path in output.iterdir()} == {
        RELEASE_FILENAME,
        CHECKSUM_FILENAME,
        *(artifact.filename for artifact in built.artifacts),
    }
    assert verify_release(output) == built

    wheel = next(
        output / artifact.filename
        for artifact in built.artifacts
        if artifact.kind == "wheel"
    )
    wheel.write_bytes(wheel.read_bytes() + b"tampered")
    with pytest.raises(ReleaseError, match="size changed"):
        verify_release(output)


def test_release_build_refuses_existing_output(tmp_path: Path) -> None:
    output = tmp_path / "existing"
    output.mkdir()

    with pytest.raises(ReleaseError, match="already exists"):
        build_release(
            output,
            source_root=BENCHMARK_ROOT,
            evaluator_versions=(),
        )


def test_bundle_archive_writer_is_byte_reproducible(tmp_path: Path) -> None:
    bundle = tmp_path / "bundle"
    (bundle / "nested").mkdir(parents=True)
    (bundle / "autocad-bench-bundle.json").write_text(
        '{"fixture":true}\n',
        encoding="utf-8",
    )
    (bundle / "nested" / "payload.bin").write_bytes(b"\x00\x01fixture")
    first = tmp_path / "first.tar.gz"
    second = tmp_path / "second.tar.gz"

    _write_bundle_archive(
        bundle,
        first,
        epoch=DEFAULT_SOURCE_DATE_EPOCH,
    )
    _write_bundle_archive(
        bundle,
        second,
        epoch=DEFAULT_SOURCE_DATE_EPOCH,
    )

    assert first.read_bytes() == second.read_bytes()
    assert hashlib.sha256(first.read_bytes()).hexdigest() == hashlib.sha256(
        second.read_bytes()
    ).hexdigest()
    with tarfile.open(first, mode="r:gz") as archive:
        assert archive.getnames() == [
            BUNDLE_VERSION,
            f"{BUNDLE_VERSION}/autocad-bench-bundle.json",
            f"{BUNDLE_VERSION}/nested",
            f"{BUNDLE_VERSION}/nested/payload.bin",
        ]