Fix 'not enough arguments' during wasmtime run (#858)

* Fix 'not enough arguments' during wasmtime run

* add simple cli smoke tests

* autogenerate wasm
This commit is contained in:
Yury Delendik
2020-01-30 14:29:50 +01:00
committed by GitHub
parent bc50815eac
commit a8cad05e80
5 changed files with 75 additions and 2 deletions

1
Cargo.lock generated
View File

@@ -1928,6 +1928,7 @@ dependencies = [
"rayon",
"structopt",
"target-lexicon",
"tempfile",
"test-programs",
"wasi-common",
"wasm-webidl-bindings",

View File

@@ -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"

View File

@@ -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(),
}
}

66
tests/cli_tests.rs Normal file
View File

@@ -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<Path>) -> Result<NamedTempFile> {
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",
])
}

5
tests/wasm/simple.wat Normal file
View File

@@ -0,0 +1,5 @@
(module
(func (export "simple") (param i32) (result i32)
local.get 0
)
)