Path: blob/main/crates/wasi-config/tests/main.rs
1692 views
use anyhow::{Result, anyhow};1use test_programs_artifacts::{CONFIG_GET_COMPONENT, foreach_config};2use wasmtime::{3Store,4component::{Component, Linker, ResourceTable},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| {27config.async_support(true);28});29let mut store = Store::new(&engine, ctx);30let component = Component::from_file(&engine, path)?;3132let mut linker = Linker::new(&engine);33add_to_linker_async(&mut linker)?;34wasmtime_wasi_config::add_to_linker(&mut linker, |h: &mut Ctx| {35WasiConfig::from(&h.wasi_config_vars)36})?;3738let command = Command::instantiate_async(&mut store, &component, &linker).await?;39command40.wasi_cli_run()41.call_run(&mut store)42.await?43.map_err(|()| anyhow!("command returned with failing exit status"))44}4546macro_rules! assert_test_exists {47($name:ident) => {48#[expect(unused_imports, reason = "only here to ensure name exists")]49use self::$name as _;50};51}5253foreach_config!(assert_test_exists);5455#[tokio::test(flavor = "multi_thread")]56async fn config_get() -> Result<()> {57run_wasi(58CONFIG_GET_COMPONENT,59Ctx {60table: ResourceTable::new(),61wasi_ctx: WasiCtxBuilder::new().build(),62wasi_config_vars: WasiConfigVariables::from_iter(vec![("hello", "world")]),63},64)65.await66}676869