use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::process::Command;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use camino_tempfile::Utf8TempDir;
#[derive(Debug)]
struct Plan {
values: HashMap<String, String>,
command_args: Vec<String>,
}
fn remove_release_overrides(command: &mut Command) {
for (name, _) in std::env::vars() {
if matches!(
name.as_str(),
"CARGO_BUILD_RUSTFLAGS"
| "CARGO_ENCODED_RUSTFLAGS"
| "CARGO_HOME"
| "RUSTC"
| "RUSTC_WRAPPER"
| "RUSTC_WORKSPACE_WRAPPER"
| "RUSTFLAGS"
) || name.starts_with("CARGO_PROFILE_RELEASE_")
|| (name.starts_with("CARGO_TARGET_") && name.ends_with("_RUSTFLAGS"))
{
command.env_remove(name);
}
}
}
fn remove_node_overrides(command: &mut Command) {
for name in [
"NODE_COMPILE_CACHE",
"NODE_DEBUG",
"NODE_DEBUG_NATIVE",
"NODE_ENV",
"NODE_INSPECT_RESUME_ON_START",
"NODE_OPTIONS",
"NODE_PATH",
"NODE_PENDING_DEPRECATION",
] {
command.env_remove(name);
}
}
fn plan(target: &str, profile: &str) -> Plan {
let repo_root = Path::new(env!("CARGO_MANIFEST_DIR"));
let fixture = Utf8TempDir::new().expect("temporary plan fixture should be created");
let fixture_tools = fixture.path().join("tools");
fs::create_dir_all(&fixture_tools).expect("temporary tools directory should be created");
let fixture_script = fixture_tools.join("dev-test.sh");
fs::copy(repo_root.join("tools/dev-test.sh"), &fixture_script)
.expect("dev-test script should be copied into the isolated fixture");
let mut command = Command::new("bash");
command
.arg(fixture_script)
.args([target, profile, "runtime", "profile_probe"])
.env("WASM_RQUICKJS_DEV_TEST_PLAN_ONLY", "1");
if profile == "release" {
remove_release_overrides(&mut command);
}
let output = command
.output()
.expect("dev-test profile planning should run");
assert!(
output.status.success(),
"profile planning failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
let mut values = HashMap::new();
let mut command_args = Vec::new();
for line in String::from_utf8(output.stdout)
.expect("profile plan should be UTF-8")
.lines()
{
let (key, value) = line
.split_once('=')
.expect("profile plan lines should be key=value");
if key == "command_arg" {
command_args.push(value.to_string());
} else {
values.insert(key.to_string(), value.to_string());
}
}
Plan {
values,
command_args,
}
}
fn value<'a>(plan: &'a Plan, key: &str) -> &'a str {
plan.values
.get(key)
.unwrap_or_else(|| panic!("missing {key} in {plan:?}"))
}
fn feature_list(plan: &Plan) -> Vec<&str> {
value(plan, "features")
.split(',')
.filter(|feature| !feature.is_empty())
.collect()
}
fn assert_common_fast_profile(plan: &Plan, expected_target: &str) {
assert_eq!(value(plan, "artifact_cache"), "1");
assert_eq!(value(plan, "locked_builds"), "1");
assert_eq!(value(plan, "wasmtime_cache"), "1");
assert_eq!(value(plan, "test_target"), expected_target);
assert!(plan.command_args.iter().any(|arg| arg == "--locked"));
assert!(plan.command_args.iter().any(|arg| arg == "--report-time"));
}
#[test]
fn dev_test_profile_matrix_preserves_standard_and_fast_semantics() {
for target in ["p2", "p3"] {
let standard = plan(target, "standard");
let expected_standard_features = if target == "p2" {
vec!["use-golem-wasmtime"]
} else {
Vec::new()
};
assert_eq!(feature_list(&standard), expected_standard_features);
assert_eq!(value(&standard, "artifact_cache"), "0");
assert_eq!(value(&standard, "component_profile"), "dev");
assert_eq!(value(&standard, "host_release"), "false");
assert_eq!(value(&standard, "locked_builds"), "0");
assert_eq!(value(&standard, "precompile_component"), "0");
assert_eq!(value(&standard, "prepared_component_cache"), "0");
assert_eq!(value(&standard, "unoptimized"), "0");
assert_eq!(value(&standard, "wasmtime_cache"), "0");
assert!(!standard.command_args.iter().any(|arg| arg == "--locked"));
assert!(
!standard
.command_args
.iter()
.any(|arg| arg == "--test-threads")
);
let release = plan(target, "release");
assert_eq!(feature_list(&release), expected_standard_features);
assert_eq!(value(&release, "artifact_cache"), "0");
assert_eq!(value(&release, "component_profile"), "release");
assert_eq!(value(&release, "host_release"), "true");
assert_eq!(value(&release, "locked_builds"), "1");
assert_eq!(value(&release, "unoptimized"), "0");
assert!(release.command_args.iter().any(|arg| arg == "--release"));
assert!(release.command_args.iter().any(|arg| arg == "--locked"));
assert!(
!release
.command_args
.iter()
.any(|arg| arg == "--test-threads")
);
let fast_start = plan(target, "fast-start");
let mut expected_fast_features = vec!["wasm-rquickjs/external-skeleton"];
if target == "p2" {
expected_fast_features.insert(0, "use-golem-wasmtime");
}
assert_eq!(feature_list(&fast_start), expected_fast_features);
assert_common_fast_profile(&fast_start, target);
assert_eq!(value(&fast_start, "unoptimized"), "1");
assert_eq!(value(&fast_start, "precompile_component"), "0");
assert_eq!(value(&fast_start, "prepared_component_cache"), "0");
assert!(
fast_start
.command_args
.windows(2)
.any(|args| args == ["--test-threads", "1"])
);
let fast_run = plan(target, "fast-run");
assert_eq!(feature_list(&fast_run), expected_fast_features);
assert_common_fast_profile(&fast_run, target);
assert_eq!(value(&fast_run, "unoptimized"), "0");
assert_eq!(value(&fast_run, "precompile_component"), "1");
assert_eq!(value(&fast_run, "prepared_component_cache"), "1");
assert!(
fast_run
.command_args
.windows(2)
.any(|args| args == ["--test-threads", "8"])
);
}
}
#[test]
fn release_profile_rejects_inherited_compiler_overrides() {
let repo_root = Path::new(env!("CARGO_MANIFEST_DIR"));
for (variable, value) in [
("CARGO_PROFILE_RELEASE_OPT_LEVEL", "0"),
("RUSTFLAGS", "-Copt-level=0"),
] {
let mut command = Command::new("bash");
command
.arg(repo_root.join("tools/dev-test.sh"))
.args(["p3", "release", "agentic_ts", ""])
.env("WASM_RQUICKJS_DEV_TEST_PLAN_ONLY", "1");
remove_release_overrides(&mut command);
let output = command
.env(variable, value)
.output()
.expect("release profile planning should run");
assert!(
!output.status.success(),
"{variable} was unexpectedly accepted"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains(variable) && stderr.contains("rejects inherited"),
"unexpected rejection for {variable}: {stderr}"
);
}
let cargo_home = Utf8TempDir::new().expect("temporary Cargo home should be created");
fs::write(
cargo_home.path().join("config.toml"),
"[profile.release]\nopt-level = 0\n",
)
.expect("temporary Cargo config should be written");
let mut command = Command::new("bash");
command
.arg(repo_root.join("tools/dev-test.sh"))
.args(["p3", "release", "agentic_ts", ""])
.env("WASM_RQUICKJS_DEV_TEST_PLAN_ONLY", "1");
remove_release_overrides(&mut command);
let output = command
.env("CARGO_HOME", cargo_home.path())
.output()
.expect("release profile planning should run");
assert!(
!output.status.success(),
"redirected CARGO_HOME was accepted"
);
assert!(
String::from_utf8_lossy(&output.stderr).contains("CARGO_HOME"),
"redirected Cargo config rejection did not identify CARGO_HOME"
);
let mut command = Command::new("bash");
command
.arg(repo_root.join("tools/dev-test.sh"))
.args(["p3", "release", "agentic_ts", ""])
.env("WASM_RQUICKJS_DEV_TEST_PLAN_ONLY", "1");
remove_release_overrides(&mut command);
let output = command
.env("RUSTC", "/tmp/not-the-pinned-rustc")
.output()
.expect("release profile planning should run");
assert!(!output.status.success(), "alternate RUSTC was accepted");
assert!(
String::from_utf8_lossy(&output.stderr).contains("RUSTC"),
"alternate compiler rejection did not identify RUSTC"
);
}
#[test]
fn agentic_ts_release_runner_rejects_node_environment_overrides() {
let repo_root = Path::new(env!("CARGO_MANIFEST_DIR"));
for (variable, value) in [
("NODE_OPTIONS", "--trace-warnings"),
("NODE_COMPILE_CACHE", "/tmp/node-compile-cache"),
("NODE_ENV", "production"),
] {
let mut command = Command::new("sh");
command
.arg(repo_root.join("tests/agentic_ts/run.sh"))
.arg("--release")
.current_dir(repo_root);
remove_node_overrides(&mut command);
let output = command
.env(variable, value)
.output()
.expect("release runner guard should execute");
assert!(
!output.status.success(),
"{variable} was unexpectedly accepted"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains(variable) && stderr.contains("rejects inherited Node"),
"unexpected rejection for {variable}: {stderr}"
);
}
}
#[test]
fn agentic_ts_runner_uses_one_date_for_the_report_pair() {
let repo_root = Path::new(env!("CARGO_MANIFEST_DIR"));
let runner = fs::read_to_string(repo_root.join("tests/agentic_ts/run.sh"))
.expect("agentic TypeScript runner should be readable");
assert_eq!(
runner.matches("measurement_date=$(date +%Y-%m-%d)").count(),
1,
"the report-pair date should be captured exactly once"
);
assert!(
runner.contains(
"report=\"$results_dir/${measurement_date}${report_label}-$target-$platform-$arch.json\""
),
"both report paths should use the captured measurement date"
);
}
#[test]
fn npm_metadata_release_runner_rejects_node_environment_overrides() {
let repo_root = Path::new(env!("CARGO_MANIFEST_DIR"));
for (variable, value) in [
("NODE_OPTIONS", "--trace-warnings"),
("NODE_COMPILE_CACHE", "/tmp/node-compile-cache"),
("NODE_ENV", "production"),
] {
let mut command = Command::new("sh");
command
.arg(repo_root.join("tests/npm_metadata/run.sh"))
.arg("--release")
.current_dir(repo_root);
remove_node_overrides(&mut command);
let output = command
.env(variable, value)
.output()
.expect("release runner guard should execute");
assert!(
!output.status.success(),
"{variable} was unexpectedly accepted"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains(variable) && stderr.contains("rejects inherited Node"),
"unexpected rejection for {variable}: {stderr}"
);
}
}
#[test]
fn npm_metadata_runner_uses_one_date_for_the_report_pair() {
let repo_root = Path::new(env!("CARGO_MANIFEST_DIR"));
let runner = fs::read_to_string(repo_root.join("tests/npm_metadata/run.sh"))
.expect("npm metadata runner should be readable");
assert_eq!(
runner.matches("measurement_date=$(date +%Y-%m-%d)").count(),
1,
"the report-pair date should be captured exactly once"
);
assert!(
runner.contains(
"report=\"$results_dir/${measurement_date}-release-$target-$platform-$arch.json\""
),
"both report paths should use the captured measurement date"
);
}
#[test]
fn wasmtime_fork_transform_supports_copied_manifests_and_new_patch_crates() {
let repo_root = Path::new(env!("CARGO_MANIFEST_DIR"));
let temp = Utf8TempDir::new().expect("temporary directory should be created");
let source = temp.path().join("source.toml");
let output = temp.path().join("output.toml");
let branch = "golem-wasmtime-v46.0.1-p3";
fs::write(
&source,
format!(
"[workspace]\n\
#[patch.crates-io]\n\
#wasmtime = {{ git = \"https://github.com/golemcloud/wasmtime.git\", branch = \"{branch}\" }}\n\
#wasmtime-component-util = {{ git = \"https://github.com/golemcloud/wasmtime.git\", branch = \"{branch}\" }}\n"
),
)
.expect("source manifest should be written");
let result = Command::new("bash")
.arg(repo_root.join(".github/scripts/enable-wasmtime-fork.sh"))
.arg(&source)
.arg(&output)
.output()
.expect("Wasmtime fork transform should run");
assert!(
result.status.success(),
"transform failed:\n{}",
String::from_utf8_lossy(&result.stderr)
);
let source_after = fs::read_to_string(&source).expect("source manifest should remain");
let output = fs::read_to_string(&output).expect("output manifest should exist");
assert!(source_after.contains("#[patch.crates-io]"));
assert!(output.contains("[patch.crates-io]"));
assert!(output.contains("wasmtime = { git = "));
assert!(output.contains("wasmtime-component-util = { git = "));
assert!(!output.contains("#wasmtime"));
}
#[cfg(unix)]
fn skeleton_clippy_fixture() -> (Utf8TempDir, std::path::PathBuf, std::path::PathBuf) {
let repo_root = Path::new(env!("CARGO_MANIFEST_DIR"));
let source_tests = fs::canonicalize(repo_root.join("tests"))
.expect("source tests directory should be available");
let source_root = source_tests
.parent()
.expect("source tests directory should have a repository parent");
let helper = source_root.join("tools/check-skeleton-clippy.sh");
let temp = Utf8TempDir::new().expect("temporary directory should be created");
let tools_dir = temp.path().join("tools");
let skeleton_dir = temp.path().join("crates/wasm-rquickjs/skeleton");
fs::create_dir_all(&tools_dir).expect("fixture tools directory should be created");
fs::create_dir_all(&skeleton_dir).expect("fixture skeleton directory should be created");
fs::copy(helper, tools_dir.join("check-skeleton-clippy.sh"))
.expect("Clippy helper should be copied");
fs::write(skeleton_dir.join("Cargo.toml_"), "[workspace]\n")
.expect("stored fixture manifest should be written");
fs::write(skeleton_dir.join("Cargo.lock"), "# fixture lockfile\n")
.expect("fixture lockfile should be written");
fs::create_dir_all(skeleton_dir.join("src"))
.expect("fixture source directory should be created");
fs::write(skeleton_dir.join("src/lib.rs"), "mod builtin;\n")
.expect("fixture lib.rs should be written");
fs::write(skeleton_dir.join("src/builtin_p3.rs"), "")
.expect("fixture P3 registry should be written");
let fake_cargo = temp.path().join("fake-cargo.sh");
fs::write(
&fake_cargo,
"#!/usr/bin/env bash\nset -euo pipefail\nmkdir -p \"$CARGO_TARGET_DIR\"\nprintf '%s\\n' \"$*\" >> \"$FAKE_CARGO_LOG\"\ncall=$(wc -l < \"$FAKE_CARGO_LOG\")\nif [[ -n \"${FAKE_CARGO_EXIT:-}\" && \"$call\" -eq \"${FAKE_CARGO_FAIL_ON_CALL:-1}\" ]]; then\n exit \"$FAKE_CARGO_EXIT\"\nfi\n",
)
.expect("fake Cargo should be written");
let mut permissions = fs::metadata(&fake_cargo)
.expect("fake Cargo metadata should exist")
.permissions();
permissions.set_mode(0o755);
fs::set_permissions(&fake_cargo, permissions).expect("fake Cargo should be executable");
(temp, skeleton_dir.into(), fake_cargo.into())
}
#[cfg(unix)]
#[test]
fn skeleton_clippy_helper_covers_the_supported_feature_matrix_and_cleans_up() {
let (temp, skeleton_dir, fake_cargo) = skeleton_clippy_fixture();
let log = temp.path().join("cargo.log");
let output = Command::new("bash")
.arg(temp.path().join("tools/check-skeleton-clippy.sh"))
.env("CARGO", &fake_cargo)
.env("FAKE_CARGO_LOG", &log)
.output()
.expect("Clippy helper should run");
assert!(
output.status.success(),
"Clippy helper failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(skeleton_dir.join("Cargo.toml_").is_file());
assert!(!skeleton_dir.join("Cargo.toml").exists());
assert!(!skeleton_dir.join("target").exists());
assert!(temp.path().join("target").is_dir());
let invocations = fs::read_to_string(log).expect("fake Cargo log should exist");
let invocations = invocations.lines().collect::<Vec<_>>();
assert_eq!(invocations.len(), 10);
assert!(invocations.iter().all(|args| {
args.contains("--locked")
&& args.contains("--target wasm32-wasip2")
&& args.contains("--all-targets")
&& args.contains("-Dwarnings")
}));
assert!(invocations[0].contains("clippy --manifest-path"));
assert!(invocations[1].contains("--features full,golem,typescript-transform-runtime"));
assert!(invocations[2].contains("--features full,golem,typescript-compiler-profiling"));
assert!(
invocations[3].contains("--features full-no-logging,golem,typescript-transform-runtime")
);
assert!(
invocations[4].contains("--features full-no-logging,golem,typescript-compiler-profiling")
);
assert!(invocations[5].contains("--features normal-p3"));
assert!(invocations[6].contains("--features full-p3,golem,typescript-transform-runtime"));
assert!(invocations[7].contains("--features full-p3,golem,typescript-compiler-profiling"));
assert!(
invocations[8].contains("--features full-no-logging-p3,golem,typescript-transform-runtime")
);
assert!(
invocations[9]
.contains("--features full-no-logging-p3,golem,typescript-compiler-profiling")
);
}
#[cfg(unix)]
#[test]
fn skeleton_clippy_helper_restores_the_manifest_after_a_lint_failure() {
let (temp, skeleton_dir, fake_cargo) = skeleton_clippy_fixture();
let log = temp.path().join("cargo.log");
let output = Command::new("bash")
.arg(temp.path().join("tools/check-skeleton-clippy.sh"))
.env("CARGO", &fake_cargo)
.env("FAKE_CARGO_EXIT", "23")
.env("FAKE_CARGO_LOG", &log)
.output()
.expect("Clippy helper should run");
assert_eq!(output.status.code(), Some(23));
assert!(skeleton_dir.join("Cargo.toml_").is_file());
assert!(!skeleton_dir.join("Cargo.toml").exists());
assert!(!skeleton_dir.join("target").exists());
assert!(temp.path().join("target").is_dir());
assert_eq!(
fs::read_to_string(log)
.expect("fake Cargo log should exist")
.lines()
.count(),
1
);
}