| # Copyright 2026 The Fuchsia Authors | |
| # | |
| # Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| # <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| # license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| # This file may not be copied, modified, or distributed except according to | |
| # those terms. | |
| """Shared imports, `_make_crate_bytes`, and `RepoFixture` for the | |
| `alpha_release.py` tests.""" | |
| from __future__ import annotations | |
| import contextlib | |
| import gzip | |
| import hashlib | |
| import io | |
| import json | |
| import lzma | |
| from pathlib import Path | |
| import subprocess | |
| import sys | |
| import tarfile | |
| import tempfile | |
| import tomllib | |
| import tracemalloc | |
| from alpha_release import ( | |
| CRATES_API_URL, | |
| DEV_VERSION, | |
| LOCK_REL, | |
| MANIFEST_REL, | |
| NON_STABLE_CHECK_REL, | |
| PRE_STANDALONE_FLOOR_COMMIT, | |
| PRE_STANDALONE_MAX_VERSION, | |
| REPO, | |
| STATIC_CRATES_PREFIX, | |
| UNPINNED, | |
| build_parser, | |
| inspect_crate_archive, | |
| parse_anneal_prerelease_version, | |
| release_notes, | |
| resolve_and_validate, | |
| stamp_manifests, | |
| toolchain_tag_and_url, | |
| user_agent_for_repo, | |
| validate_repo_slug, | |
| verify_crate_file, | |
| verify_published_crate, | |
| ) | |
| SCRIPT = Path(__file__).resolve().parent.parent / "alpha_release.py" | |
| def _make_crate_bytes( | |
| *, | |
| version: str = "0.2.0-alpha.7", | |
| commit: str = "a" * 40, | |
| sha256: str = "1" * 64, | |
| dirty: bool = True, | |
| path_in_vcs: str = "trusted/cargo-anneal", | |
| extra_orig_package_lines: str = "", | |
| extra_gen_package_lines: str = 'readme = "README.md"\nautobins = true\nbuild = "build.rs"\n', | |
| extra_top_toml: str = "", | |
| orig_dep_spec: str = '"1.0"', | |
| gen_dep_spec: str = '{ version = "1.0" }', | |
| bin_name: str = "cargo-anneal", | |
| bin_path: str = "src/main.rs", | |
| lock_version: str | None = None, | |
| lib_rs: bytes = b"pub fn verifier() {}\n", | |
| lib_mode: int = 0o644, | |
| manifest_mode: int = 0o644, | |
| extra_members: tuple[tuple[str, bytes, int, bytes], ...] = (), | |
| omit_member: str | None = None, | |
| repo_slug: str = "google/anneal", | |
| ) -> bytes: | |
| _, url = toolchain_tag_and_url(commit, sha256, repo_slug=repo_slug) | |
| lock_ver = version if lock_version is None else lock_version | |
| orig_toml = ( | |
| '[package]\n' | |
| 'name = "cargo-anneal"\n' | |
| f'version = "{version}"\n' | |
| 'edition = "2024"\n' | |
| 'description = "Formally verify Rust programs in Lean 4."\n' | |
| 'license = "BSD-2-Clause OR Apache-2.0 OR MIT"\n' | |
| 'repository = "https://github.com/google/anneal"\n' | |
| 'readme = "../../README.md"\n' | |
| f'{extra_orig_package_lines}\n' | |
| '[[package.metadata.exocrate]]\n' | |
| 'os = "linux"\n' | |
| 'arch = "x86_64"\n' | |
| f'sha256 = "{sha256}"\n' | |
| f'url = "{url}"\n\n' | |
| '[[bin]]\n' | |
| f'name = "{bin_name}"\n\n' | |
| '[features]\n' | |
| 'default = ["setup"]\n' | |
| 'setup = ["dep:exocrate"]\n\n' | |
| '[dependencies]\n' | |
| f'serde_json = {orig_dep_spec}\n' | |
| 'exocrate = { version = "0.3.0", optional = true }\n\n' | |
| "[target.'cfg(unix)'.dependencies]\n" | |
| 'libc = "0.2"\n' | |
| ) | |
| gen_toml = ( | |
| '# Automatically generated by Cargo\n' | |
| '[package]\n' | |
| 'name = "cargo-anneal"\n' | |
| f'version = "{version}"\n' | |
| 'edition = "2024"\n' | |
| 'description = "Formally verify Rust programs in Lean 4."\n' | |
| 'license = "BSD-2-Clause OR Apache-2.0 OR MIT"\n' | |
| 'repository = "https://github.com/google/anneal"\n' | |
| f'{extra_gen_package_lines}\n' | |
| '[[package.metadata.exocrate]]\n' | |
| 'os = "linux"\n' | |
| 'arch = "x86_64"\n' | |
| f'sha256 = "{sha256}"\n' | |
| f'url = "{url}"\n\n' | |
| '[[bin]]\n' | |
| f'name = "{bin_name}"\n' | |
| f'path = "{bin_path}"\n\n' | |
| '[features]\n' | |
| 'default = ["setup"]\n' | |
| 'setup = ["dep:exocrate"]\n\n' | |
| '[dependencies]\n' | |
| f'serde_json = {gen_dep_spec}\n' | |
| 'exocrate = { version = "0.3.0", optional = true }\n\n' | |
| "[target.'cfg(unix)'.dependencies]\n" | |
| 'libc = { version = "0.2" }\n' | |
| f'{extra_top_toml}' | |
| ) | |
| cargo_lock = ( | |
| 'version = 4\n\n' | |
| '[[package]]\n' | |
| 'name = "anneal-audit"\n' | |
| 'version = "0.1.0"\n\n' | |
| '[[package]]\n' | |
| 'name = "cargo-anneal"\n' | |
| f'version = "{lock_ver}"\n' | |
| ) | |
| vcs_info = json.dumps( | |
| { | |
| "git": {"sha1": commit, "dirty": dirty}, | |
| "path_in_vcs": path_in_vcs, | |
| } | |
| ).encode("utf-8") | |
| prefix = f"cargo-anneal-{version}/" | |
| entries: list[tuple[str, bytes, int, bytes]] = [ | |
| (f"{prefix}.cargo_vcs_info.json", vcs_info, manifest_mode, tarfile.REGTYPE), | |
| (f"{prefix}Cargo.toml", gen_toml.encode("utf-8"), manifest_mode, tarfile.REGTYPE), | |
| (f"{prefix}Cargo.toml.orig", orig_toml.encode("utf-8"), manifest_mode, tarfile.REGTYPE), | |
| (f"{prefix}Cargo.lock", cargo_lock.encode("utf-8"), manifest_mode, tarfile.REGTYPE), | |
| (f"{prefix}README.md", b"# Anneal\n", 0o644, tarfile.REGTYPE), | |
| (f"{prefix}build.rs", b"fn main() {}\n", 0o644, tarfile.REGTYPE), | |
| ( | |
| f"{prefix}packaged/bundled_resources.rs", | |
| b'pub const TEST: &str = "ok";\n', | |
| 0o644, | |
| tarfile.REGTYPE, | |
| ), | |
| ( | |
| f"{prefix}packaged/bundled_theory.json", | |
| b'{"schema_version":1}\n', | |
| 0o644, | |
| tarfile.REGTYPE, | |
| ), | |
| (f"{prefix}src/lib.rs", lib_rs, lib_mode, tarfile.REGTYPE), | |
| (f"{prefix}src/main.rs", b"fn main() {}\n", 0o644, tarfile.REGTYPE), | |
| *extra_members, | |
| ] | |
| buf = io.BytesIO() | |
| with gzip.GzipFile(fileobj=buf, mode="wb", mtime=0) as gz: | |
| with tarfile.open(fileobj=gz, mode="w|") as tf: | |
| for name, payload, mode, typeflag in entries: | |
| if omit_member is not None and name == f"{prefix}{omit_member}": | |
| continue | |
| info = tarfile.TarInfo(name=name) | |
| info.type = typeflag | |
| info.mode = mode | |
| if typeflag == tarfile.SYMTYPE: | |
| info.size = 0 | |
| info.linkname = "Cargo.toml" | |
| tf.addfile(info) | |
| elif typeflag == tarfile.DIRTYPE: | |
| info.size = 0 | |
| tf.addfile(info) | |
| else: | |
| info.size = len(payload) | |
| tf.addfile(info, io.BytesIO(payload)) | |
| return buf.getvalue() | |
| class RepoFixture: | |
| """Isolated origin + clone Git fixture for release resolution and stamping.""" | |
| def __init__(self, root: Path) -> None: | |
| self.origin = root / "origin.git" | |
| self.repo = root / "work" | |
| subprocess.run( | |
| ["git", "init", "--quiet", "--bare", "--initial-branch=main", str(self.origin)], | |
| check=True, | |
| ) | |
| subprocess.run( | |
| ["git", "init", "--quiet", "--initial-branch=main", str(self.repo)], | |
| check=True, | |
| ) | |
| self.git("remote", "add", "origin", str(self.origin)) | |
| self._write_base_files() | |
| self.initial_commit = self.commit_all("initial steady-state main") | |
| self.push_main() | |
| def git(self, *args: str, repo: Path | None = None) -> str: | |
| target = self.repo if repo is None else repo | |
| return subprocess.check_output( | |
| [ | |
| "git", | |
| "-c", | |
| "user.name=Test", | |
| "-c", | |
| "user.email=test@example.invalid", | |
| "-c", | |
| "commit.gpgSign=false", | |
| "-c", | |
| "core.hooksPath=/dev/null", | |
| "-c", | |
| "core.fsmonitor=false", | |
| *args, | |
| ], | |
| cwd=target, | |
| text=True, | |
| ).strip() | |
| def _write_base_files(self) -> None: | |
| manifest = self.repo / MANIFEST_REL | |
| manifest.parent.mkdir(parents=True, exist_ok=True) | |
| manifest.write_text( | |
| '[package]\n' | |
| 'name = "cargo-anneal"\n' | |
| f'version = "{DEV_VERSION}"\n' | |
| 'edition = "2024"\n' | |
| 'description = "Verifier"\n' | |
| 'license = "MIT"\n' | |
| 'repository = "https://github.com/google/anneal"\n\n' | |
| '# Sentinel metadata.\n' | |
| '[[package.metadata.exocrate]]\n' | |
| 'os = "linux"\n' | |
| 'arch = "x86_64"\n' | |
| f'sha256 = "{UNPINNED}"\n' | |
| 'url = ""\n\n' | |
| '[[bin]]\n' | |
| 'name = "cargo-anneal"\n\n' | |
| '[dependencies]\n' | |
| 'serde_json = "1.0"\n', | |
| encoding="utf-8", | |
| ) | |
| lockfile = self.repo / LOCK_REL | |
| lockfile.parent.mkdir(parents=True, exist_ok=True) | |
| lockfile.write_text( | |
| 'version = 4\n\n' | |
| '[[package]]\n' | |
| 'name = "anneal-audit"\n' | |
| 'version = "0.1.0"\n' | |
| 'dependencies = [\n' | |
| ' "cargo-anneal",\n' | |
| ']\n\n' | |
| '[[package]]\n' | |
| 'name = "cargo-anneal"\n' | |
| f'version = "{DEV_VERSION}"\n', | |
| encoding="utf-8", | |
| ) | |
| # Install a failing decoy script inside `self.repo` to prove | |
| # `stamp_manifests` runs the harness repo's | |
| # `check_non_stable_version.sh` rather than executing code from the | |
| # target commit checkout. | |
| check_dst = self.repo / NON_STABLE_CHECK_REL | |
| check_dst.parent.mkdir(parents=True, exist_ok=True) | |
| check_dst.write_text("#!/usr/bin/env bash\nexit 99\n", encoding="utf-8") | |
| check_dst.chmod(0o755) | |
| def commit_all(self, message: str, *, touch_file: str | None = None) -> str: | |
| if touch_file is not None: | |
| target = self.repo / touch_file | |
| target.parent.mkdir(parents=True, exist_ok=True) | |
| target.write_text(f"{message}\n", encoding="utf-8") | |
| self.git("add", ".") | |
| self.git("commit", "--quiet", "-m", message) | |
| return self.git("rev-parse", "HEAD") | |
| def push_main(self) -> None: | |
| self.git("push", "--quiet", "origin", "HEAD:refs/heads/main") | |
| self.fetch() | |
| def tag_annotated(self, tag: str, sha: str, *, push: bool = True, local: bool = True) -> None: | |
| self.git("tag", "-a", "-m", f"Tag {tag}", tag, sha) | |
| if push: | |
| self.git("push", "--quiet", "origin", f"refs/tags/{tag}") | |
| if not local: | |
| # Remove the local tag so `self.repo` lacks it until | |
| # `_fetch_origin_refs` runs during polling. | |
| self.git("tag", "-d", tag) | |
| def fetch(self) -> None: | |
| self.git( | |
| "fetch", | |
| "--quiet", | |
| "--force", | |
| "origin", | |
| "+refs/heads/main:refs/remotes/origin/main", | |
| "+refs/tags/anneal-*:refs/tags/anneal-*", | |
| ) |