Skip to main content

ic_testkit/artifacts/
wasm.rs

1use std::{
2    fs,
3    path::{Path, PathBuf},
4};
5
6/// Resolve one crate's Wasm artifact under a caller-selected Cargo target directory.
7#[must_use]
8pub fn wasm_path(target_dir: &Path, crate_name: &str, profile_target_dir: &str) -> PathBuf {
9    target_dir
10        .join("wasm32-unknown-unknown")
11        .join(profile_target_dir)
12        .join(format!("{crate_name}.wasm"))
13}
14
15/// Check whether every requested Wasm artifact is a regular file.
16#[must_use]
17pub fn wasm_artifacts_ready(
18    target_dir: &Path,
19    canisters: &[&str],
20    profile_target_dir: &str,
21) -> bool {
22    canisters
23        .iter()
24        .all(|name| wasm_path(target_dir, name, profile_target_dir).is_file())
25}
26
27/// Read a compiled Wasm artifact for one crate.
28///
29/// # Panics
30///
31/// Panics with the crate name when the artifact cannot be read.
32#[must_use]
33pub fn read_wasm(target_dir: &Path, crate_name: &str, profile_target_dir: &str) -> Vec<u8> {
34    let path = wasm_path(target_dir, crate_name, profile_target_dir);
35    fs::read(&path).unwrap_or_else(|err| panic!("failed to read {crate_name} wasm: {err}"))
36}