use std::fs;
use std::path::Path;
use std::process::Command;
use anyhow::{bail, Context, Error};
use super::Tests;
use super::{node::shared_setup, Cli};
pub fn execute(module: &str, tmpdir: &Path, cli: Cli, tests: Tests) -> Result<(), Error> {
let mut js_to_execute = format!(
r#"import * as wasm from "./{module}.js";
const nocapture = {nocapture};
{shared_setup}
window.__wbg_test_invoke = f => f();
{args}
const tests = [];
"#,
shared_setup = shared_setup(cli.bench),
nocapture = cli.nocapture || cli.bench,
args = cli.get_args(&tests),
);
for test in tests.tests {
js_to_execute.push_str(&format!("tests.push('{}')\n", test.export));
}
js_to_execute.push_str(
r#"const ok = await cx.run(tests.map(n => wasm.__wasm[n]));
if (!ok) Deno.exit(1);"#,
);
let js_path = tmpdir.join("run.js");
fs::write(&js_path, js_to_execute).context("failed to write JS file")?;
let status = Command::new("deno")
.arg("run")
.arg("--allow-read")
.arg(&js_path)
.status()?;
if !status.success() {
bail!("Deno failed with exit_code {}", status.code().unwrap_or(1))
}
Ok(())
}