Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi-config/tests/main.rs
1692 views
1
use anyhow::{Result, anyhow};
2
use test_programs_artifacts::{CONFIG_GET_COMPONENT, foreach_config};
3
use wasmtime::{
4
Store,
5
component::{Component, Linker, ResourceTable},
6
};
7
use wasmtime_wasi::p2::{add_to_linker_async, bindings::Command};
8
use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiCtxView, WasiView};
9
use wasmtime_wasi_config::{WasiConfig, WasiConfigVariables};
10
11
struct Ctx {
12
table: ResourceTable,
13
wasi_ctx: WasiCtx,
14
wasi_config_vars: WasiConfigVariables,
15
}
16
17
impl WasiView for Ctx {
18
fn ctx(&mut self) -> WasiCtxView<'_> {
19
WasiCtxView {
20
ctx: &mut self.wasi_ctx,
21
table: &mut self.table,
22
}
23
}
24
}
25
26
async fn run_wasi(path: &str, ctx: Ctx) -> Result<()> {
27
let engine = test_programs_artifacts::engine(|config| {
28
config.async_support(true);
29
});
30
let mut store = Store::new(&engine, ctx);
31
let component = Component::from_file(&engine, path)?;
32
33
let mut linker = Linker::new(&engine);
34
add_to_linker_async(&mut linker)?;
35
wasmtime_wasi_config::add_to_linker(&mut linker, |h: &mut Ctx| {
36
WasiConfig::from(&h.wasi_config_vars)
37
})?;
38
39
let command = Command::instantiate_async(&mut store, &component, &linker).await?;
40
command
41
.wasi_cli_run()
42
.call_run(&mut store)
43
.await?
44
.map_err(|()| anyhow!("command returned with failing exit status"))
45
}
46
47
macro_rules! assert_test_exists {
48
($name:ident) => {
49
#[expect(unused_imports, reason = "only here to ensure name exists")]
50
use self::$name as _;
51
};
52
}
53
54
foreach_config!(assert_test_exists);
55
56
#[tokio::test(flavor = "multi_thread")]
57
async fn config_get() -> Result<()> {
58
run_wasi(
59
CONFIG_GET_COMPONENT,
60
Ctx {
61
table: ResourceTable::new(),
62
wasi_ctx: WasiCtxBuilder::new().build(),
63
wasi_config_vars: WasiConfigVariables::from_iter(vec![("hello", "world")]),
64
},
65
)
66
.await
67
}
68
69