Path: blob/main/crates/wasi-config/tests/main.rs
3090 views
use test_programs_artifacts::{CONFIG_GET_COMPONENT, foreach_config};1use wasmtime::{2Result, Store,3component::{Component, Linker, ResourceTable},4format_err,5};6use wasmtime_wasi::p2::{add_to_linker_async, bindings::Command};7use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiCtxView, WasiView};8use wasmtime_wasi_config::{WasiConfig, WasiConfigVariables};910struct Ctx {11table: ResourceTable,12wasi_ctx: WasiCtx,13wasi_config_vars: WasiConfigVariables,14}1516impl WasiView for Ctx {17fn ctx(&mut self) -> WasiCtxView<'_> {18WasiCtxView {19ctx: &mut self.wasi_ctx,20table: &mut self.table,21}22}23}2425async fn run_wasi(path: &str, ctx: Ctx) -> Result<()> {26let engine = test_programs_artifacts::engine(|_config| {});27let mut store = Store::new(&engine, ctx);28let component = Component::from_file(&engine, path)?;2930let mut linker = Linker::new(&engine);31add_to_linker_async(&mut linker)?;32wasmtime_wasi_config::add_to_linker(&mut linker, |h: &mut Ctx| {33WasiConfig::from(&h.wasi_config_vars)34})?;3536let command = Command::instantiate_async(&mut store, &component, &linker).await?;37command38.wasi_cli_run()39.call_run(&mut store)40.await?41.map_err(|()| format_err!("command returned with failing exit status"))42}4344macro_rules! assert_test_exists {45($name:ident) => {46#[expect(unused_imports, reason = "only here to ensure name exists")]47use self::$name as _;48};49}5051foreach_config!(assert_test_exists);5253#[tokio::test(flavor = "multi_thread")]54async fn config_get() -> Result<()> {55run_wasi(56CONFIG_GET_COMPONENT,57Ctx {58table: ResourceTable::new(),59wasi_ctx: WasiCtxBuilder::new().build(),60wasi_config_vars: WasiConfigVariables::from_iter(vec![("hello", "world")]),61},62)63.await64}656667