use std::path::PathBuf;
use anyhow::{Context, Result};
use clap::Parser;
use wasmer::{
sys::{engine::NativeEngineExt, Target},
Store,
};
use wasmer_wast::Wast as WastSpectest;
use crate::{backend::RuntimeOptions, common::HashAlgorithm};
#[derive(Debug, Parser)]
pub struct Wast {
#[clap(name = "FILE")]
path: PathBuf,
#[clap(flatten)]
rt: RuntimeOptions,
#[clap(short, long)]
fail_fast: bool,
#[clap(long, value_enum)]
hash_algorithm: Option<HashAlgorithm>,
}
impl Wast {
pub fn execute(&self) -> Result<()> {
self.inner_execute()
.context(format!("failed to test the wast `{}`", self.path.display()))
}
fn inner_execute(&self) -> Result<()> {
let mut engine = self.rt.get_engine(&Target::default())?;
let hash_algorithm = self.hash_algorithm.unwrap_or_default().into();
engine.set_hash_algorithm(Some(hash_algorithm));
let store: Store = Store::new(engine);
let mut wast = WastSpectest::new_with_spectest(store);
wast.fail_fast = self.fail_fast;
wast.run_file(&self.path).with_context(|| "tests failed")?;
eprintln!("Wast tests succeeded for `{}`.", self.path.display());
Ok(())
}
}