Skip to content

Commit

Permalink
fix unwrap error when given a no export functions wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jun 4, 2021
1 parent 2e89003 commit 3ad617f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/examples/no_start.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(module
(type (;0;) (func (param f64) (result i32)))
(func (;0;) (type 0) (param f64) (result i32)
unreachable))

18 changes: 18 additions & 0 deletions tests/integration/cli/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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(())
}

0 comments on commit 3ad617f

Please sign in to comment.