From 3ad617fd690a13b95795783c1ef3c4d43a4377fb Mon Sep 17 00:00:00 2001 From: chenyukang Date: Fri, 4 Jun 2021 15:40:37 +0800 Subject: [PATCH] fix unwrap error when given a no export functions wasm --- lib/cli/src/commands/run.rs | 14 +++++++++----- tests/examples/no_start.wat | 5 +++++ tests/integration/cli/tests/run.rs | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 tests/examples/no_start.wat diff --git a/lib/cli/src/commands/run.rs b/lib/cli/src/commands/run.rs index f4b912087a6..c515c10f1f5 100644 --- a/lib/cli/src/commands/run.rs +++ b/lib/cli/src/commands/run.rs @@ -341,13 +341,17 @@ impl Run { let suggested_command = format!( "wasmer {} -i {} {}", self.path.display(), - suggested_functions.get(0).unwrap(), + suggested_functions.get(0).unwrap_or(&String::new()), args.join(" ") ); - let suggestion = format!( - "Similar functions found: {}.\nTry with: {}", - names, suggested_command - ); + let suggestion = if suggested_functions.len() == 0 { + String::from("Can not find any export functions.") + } else { + format!( + "Similar functions found: {}.\nTry with: {}", + names, suggested_command + ) + }; match e { ExportError::Missing(_) => { anyhow!("No export `{}` found in the module.\n{}", name, suggestion) diff --git a/tests/examples/no_start.wat b/tests/examples/no_start.wat new file mode 100644 index 00000000000..b6e7ee108c2 --- /dev/null +++ b/tests/examples/no_start.wat @@ -0,0 +1,5 @@ +(module + (type (;0;) (func (param f64) (result i32))) + (func (;0;) (type 0) (param f64) (result i32) + unreachable)) + diff --git a/tests/integration/cli/tests/run.rs b/tests/integration/cli/tests/run.rs index 7a2ed750eed..07fddc86564 100644 --- a/tests/integration/cli/tests/run.rs +++ b/tests/integration/cli/tests/run.rs @@ -12,6 +12,10 @@ fn test_no_imports_wat_path() -> String { format!("{}/{}", ASSET_PATH, "fib.wat") } +fn test_no_start_wat_path() -> String { + format!("{}/{}", ASSET_PATH, "no_start.wat") +} + #[test] fn run_wasi_works() -> anyhow::Result<()> { let output = Command::new(WASMER_PATH) @@ -39,6 +43,7 @@ fn run_wasi_works() -> anyhow::Result<()> { } #[test] + fn run_no_imports_wasm_works() -> anyhow::Result<()> { let output = Command::new(WASMER_PATH) .arg("run") @@ -57,3 +62,16 @@ fn run_no_imports_wasm_works() -> anyhow::Result<()> { Ok(()) } + +#[test] +fn run_no_start_wasm_report_error() -> anyhow::Result<()> { + let output = Command::new(WASMER_PATH) + .arg("run") + .arg(test_no_start_wat_path()) + .output()?; + + assert_eq!(output.status.success(), false); + let result = std::str::from_utf8(&output.stderr).unwrap().to_string(); + assert_eq!(result.contains("Can not find any export functions."), true); + Ok(()) +}