Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi-config/tests/main.rs
3090 views
1
use test_programs_artifacts::{CONFIG_GET_COMPONENT, foreach_config};
2
use wasmtime::{
3
Result, Store,
4
component::{Component, Linker, ResourceTable},
5
format_err,
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
let mut store = Store::new(&engine, ctx);
29
let component = Component::from_file(&engine, path)?;
30
31
let mut linker = Linker::new(&engine);
32
add_to_linker_async(&mut linker)?;
33
wasmtime_wasi_config::add_to_linker(&mut linker, |h: &mut Ctx| {
34
WasiConfig::from(&h.wasi_config_vars)
35
})?;
36
37
let command = Command::instantiate_async(&mut store, &component, &linker).await?;
38
command
39
.wasi_cli_run()
40
.call_run(&mut store)
41
.await?
42
.map_err(|()| format_err!("command returned with failing exit status"))
43
}
44
45
macro_rules! assert_test_exists {
46
($name:ident) => {
47
#[expect(unused_imports, reason = "only here to ensure name exists")]
48
use self::$name as _;
49
};
50
}
51
52
foreach_config!(assert_test_exists);
53
54
#[tokio::test(flavor = "multi_thread")]
55
async fn config_get() -> Result<()> {
56
run_wasi(
57
CONFIG_GET_COMPONENT,
58
Ctx {
59
table: ResourceTable::new(),
60
wasi_ctx: WasiCtxBuilder::new().build(),
61
wasi_config_vars: WasiConfigVariables::from_iter(vec![("hello", "world")]),
62
},
63
)
64
.await
65
}
66
67