Path: blob/main/crates/wasi-keyvalue/tests/main.rs
1692 views
use anyhow::{Result, anyhow};1use test_programs_artifacts::{KEYVALUE_MAIN_COMPONENT, foreach_keyvalue};2use wasmtime::{3Store,4component::{Component, Linker, ResourceTable},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| {26config.async_support(true);27});28let mut store = Store::new(&engine, ctx);29let component = Component::from_file(&engine, path)?;3031let mut linker = Linker::new(&engine);32wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;33wasmtime_wasi_keyvalue::add_to_linker(&mut linker, |h: &mut Ctx| {34WasiKeyValue::new(&h.wasi_keyvalue_ctx, &mut h.table)35})?;3637let command = Command::instantiate_async(&mut store, &component, &linker).await?;38command39.wasi_cli_run()40.call_run(&mut store)41.await?42.map_err(|()| anyhow!("command returned with failing exit status"))43}4445macro_rules! assert_test_exists {46($name:ident) => {47#[expect(unused_imports, reason = "just here to assert it exists")]48use self::$name as _;49};50}5152foreach_keyvalue!(assert_test_exists);5354#[tokio::test(flavor = "multi_thread")]55async fn keyvalue_main() -> Result<()> {56run_wasi(57KEYVALUE_MAIN_COMPONENT,58Ctx {59table: ResourceTable::new(),60wasi_ctx: WasiCtx::builder().inherit_stderr().build(),61wasi_keyvalue_ctx: WasiKeyValueCtxBuilder::new()62.in_memory_data([("atomics_key", "5")])63.build(),64},65)66.await67}686970