ic-build 0.6.0

Cargo and Wasm builds, exact inputs and retained artifact caches
Documentation
#[cfg(unix)]
use ic_host_process::testing::write_executable_script;

use std::{fs, path::PathBuf};

pub fn unique_temp_directory(label: &str) -> PathBuf {
    ic_host_fs::testing::create_fixture_directory(
        &std::env::temp_dir(),
        &format!("ic-testkit-{label}"),
    )
    .expect("create test directory")
}

/// Real Cargo metadata and identities, with a deterministic minimal Wasm producer.
#[cfg(unix)]
pub(super) fn fake_wasm_build_spec(label: &str) -> (PathBuf, super::wasm_cache::WasmBuildSpec) {
    let root = unique_temp_directory(label);
    fs::create_dir_all(root.join("fixture/src")).expect("create fixture sources");
    fs::write(
        root.join("Cargo.toml"),
        "[workspace]\nmembers = [\"fixture\"]\nresolver = \"2\"\n",
    )
    .expect("write workspace manifest");
    fs::write(root.join("fixture/Cargo.toml"),
        "[package]\nname = \"fixture\"\nversion = \"0.0.0\"\nedition = \"2024\"\n[lib]\ncrate-type = [\"cdylib\"]\n")
        .expect("write fixture manifest");
    fs::write(
        root.join("fixture/src/lib.rs"),
        "pub fn value() -> u8 { 1 }\n",
    )
    .expect("write fixture source");
    let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
    let quoted_cargo = format!("'{}'", cargo.to_string_lossy().replace('\'', "'\"'\"'"));
    let wrapper = root.join("cargo.sh");
    write_executable_script(
        &wrapper,
        format!(
            r#"#!/bin/sh
set -eu
if [ "$1" = build ]; then
    target="$CARGO_TARGET_DIR"
    mkdir -p "$target/wasm32-unknown-unknown/debug"
    printf '\000asm\001\000\000\000' > "$target/wasm32-unknown-unknown/debug/fixture.wasm"
    exit 0
fi
exec {quoted_cargo} "$@"
"#
        ),
    )
    .expect("write executable test script");
    let spec =
        super::wasm_cache::WasmBuildSpec::new(&root, &root.join("exact"), &["fixture"], "debug")
            .with_isolated_builds()
            .with_cargo_program(wrapper);
    (root, spec)
}