wasmer_cli/commands/
wast.rs

1//! Runs a .wast WebAssembly test suites
2use std::path::PathBuf;
3
4use anyhow::{Context, Result};
5use clap::Parser;
6use wasmer::{
7    Store,
8    sys::{Target, engine::NativeEngineExt},
9};
10use wasmer_wast::Wast as WastSpectest;
11
12use crate::{backend::RuntimeOptions, common::HashAlgorithm};
13
14#[derive(Debug, Parser)]
15/// The options for the `wasmer wast` subcommand
16pub struct Wast {
17    /// Wast file to run
18    #[clap(name = "FILE")]
19    path: PathBuf,
20
21    #[clap(flatten)]
22    rt: RuntimeOptions,
23
24    #[clap(short, long)]
25    /// A flag to indicate wast stop at the first error or continue.
26    fail_fast: bool,
27
28    /// Hashing algorithm to be used for module hash
29    #[clap(long, value_enum)]
30    hash_algorithm: Option<HashAlgorithm>,
31}
32
33impl Wast {
34    /// Runs logic for the `validate` subcommand
35    pub fn execute(&self) -> Result<()> {
36        self.inner_execute()
37            .context(format!("failed to test the wast `{}`", self.path.display()))
38    }
39    fn inner_execute(&self) -> Result<()> {
40        let mut engine = self.rt.get_engine(&Target::default())?;
41
42        let hash_algorithm = self.hash_algorithm.unwrap_or_default().into();
43        engine.set_hash_algorithm(Some(hash_algorithm));
44
45        let store: Store = Store::new(engine);
46        let mut wast = WastSpectest::new_with_spectest(store);
47        wast.fail_fast = self.fail_fast;
48        wast.run_file(&self.path).with_context(|| "tests failed")?;
49        eprintln!("Wast tests succeeded for `{}`.", self.path.display());
50        Ok(())
51    }
52}