Path: blob/main/crates/wasi-keyvalue/tests/main.rs
3082 views
use test_programs_artifacts::{KEYVALUE_MAIN_COMPONENT, foreach_keyvalue};1use wasmtime::{2Result, Store,3component::{Component, Linker, ResourceTable},4format_err,5};6use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView, p2::bindings::Command};7use wasmtime_wasi_keyvalue::{WasiKeyValue, WasiKeyValueCtx, WasiKeyValueCtxBuilder};89struct Ctx {10table: ResourceTable,11wasi_ctx: WasiCtx,12wasi_keyvalue_ctx: WasiKeyValueCtx,13}1415impl WasiView for Ctx {16fn ctx(&mut self) -> WasiCtxView<'_> {17WasiCtxView {18ctx: &mut self.wasi_ctx,19table: &mut self.table,20}21}22}2324async fn run_wasi(path: &str, ctx: Ctx) -> Result<()> {25let engine = test_programs_artifacts::engine(|_config| {});26let mut store = Store::new(&engine, ctx);27let component = Component::from_file(&engine, path)?;2829let mut linker = Linker::new(&engine);30wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;31wasmtime_wasi_keyvalue::add_to_linker(&mut linker, |h: &mut Ctx| {32WasiKeyValue::new(&h.wasi_keyvalue_ctx, &mut h.table)33})?;3435let command = Command::instantiate_async(&mut store, &component, &linker).await?;36command37.wasi_cli_run()38.call_run(&mut store)39.await?40.map_err(|()| format_err!("command returned with failing exit status"))41}4243macro_rules! assert_test_exists {44($name:ident) => {45#[expect(unused_imports, reason = "just here to assert it exists")]46use self::$name as _;47};48}4950foreach_keyvalue!(assert_test_exists);5152#[tokio::test(flavor = "multi_thread")]53async fn keyvalue_main() -> Result<()> {54run_wasi(55KEYVALUE_MAIN_COMPONENT,56Ctx {57table: ResourceTable::new(),58wasi_ctx: WasiCtx::builder().inherit_stderr().build(),59wasi_keyvalue_ctx: WasiKeyValueCtxBuilder::new()60.in_memory_data([("atomics_key", "5")])61.build(),62},63)64.await65}666768