diff --git a/Cargo.lock b/Cargo.lock index c9145c8723..b0110b2109 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1928,6 +1928,7 @@ dependencies = [ "rayon", "structopt", "target-lexicon", + "tempfile", "test-programs", "wasi-common", "wasm-webidl-bindings", diff --git a/Cargo.toml b/Cargo.toml index c1fc9b6196..bdb5f3dd81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ more-asserts = "0.2.1" # wasm32-wasi integration tests. To enable, run # `cargo test --features test-programs`. test-programs = { path = "crates/test-programs" } +tempfile = "3.1.0" [build-dependencies] anyhow = "1.0.19" diff --git a/crates/interface-types/src/lib.rs b/crates/interface-types/src/lib.rs index 85278872a0..e730696c78 100644 --- a/crates/interface-types/src/lib.rs +++ b/crates/interface-types/src/lib.rs @@ -217,7 +217,7 @@ impl ExportBinding<'_> { ExportBindingKind::Raw(sig) => sig .params .iter() - .skip(1) // skip the VMContext argument + .skip(2) // skip the VMContext arguments .enumerate() .map(|(i, param)| default_incoming(i, param)) .collect(), @@ -252,7 +252,7 @@ impl ExportBinding<'_> { }) .collect() } - ExportBindingKind::Raw(sig) => sig.params.iter().skip(1).map(abi2ast).collect(), + ExportBindingKind::Raw(sig) => sig.params.iter().skip(2).map(abi2ast).collect(), } } diff --git a/tests/cli_tests.rs b/tests/cli_tests.rs new file mode 100644 index 0000000000..f2b194a647 --- /dev/null +++ b/tests/cli_tests.rs @@ -0,0 +1,66 @@ +use anyhow::{bail, Result}; +use std::env; +use std::io::Write; +use std::path::Path; +use std::process::{Command, Stdio}; +use tempfile::NamedTempFile; + +fn run_wasmtime(args: &[&str]) -> Result<()> { + let cargo = env::var("CARGO").unwrap_or("cargo".to_string()); + let pkg_dir = env!("CARGO_MANIFEST_DIR"); + let success = Command::new(cargo) + .current_dir(pkg_dir) + .stdout(Stdio::null()) + .args(&["run", "-q", "--"]) + .args(args) + .status()? + .success(); + if !success { + bail!("Failed to execute wasmtime with: {:?}", args); + } + Ok(()) +} + +fn build_wasm(wat_path: impl AsRef) -> Result { + let mut wasm_file = NamedTempFile::new()?; + let wasm = wat::parse_file(wat_path)?; + wasm_file.write(&wasm)?; + Ok(wasm_file) +} + +// Very basic use case: compile binary wasm file and run specific function with arguments. +#[test] +fn run_wasmtime_simple() -> Result<()> { + let wasm = build_wasm("tests/wasm/simple.wat")?; + run_wasmtime(&[ + "run", + wasm.path().to_str().unwrap(), + "--invoke", + "simple", + "4", + ]) +} + +// Wasmtime shakk when not enough arguments were provided. +#[test] +fn run_wasmtime_simple_fail_no_args() -> Result<()> { + let wasm = build_wasm("tests/wasm/simple.wat")?; + assert!( + run_wasmtime(&["run", wasm.path().to_str().unwrap(), "--invoke", "simple"]).is_err(), + "shall fail" + ); + Ok(()) +} + +// Running simple wat +#[test] +fn run_wasmtime_simple_wat() -> Result<()> { + let wasm = build_wasm("tests/wasm/simple.wat")?; + run_wasmtime(&[ + "run", + wasm.path().to_str().unwrap(), + "--invoke", + "simple", + "4", + ]) +} diff --git a/tests/wasm/simple.wat b/tests/wasm/simple.wat new file mode 100644 index 0000000000..7b618ee429 --- /dev/null +++ b/tests/wasm/simple.wat @@ -0,0 +1,5 @@ +(module + (func (export "simple") (param i32) (result i32) + local.get 0 + ) +) \ No newline at end of file