Skip to content

Commit b916474

Browse files
committed
test-programs
1 parent 804922e commit b916474

File tree

10 files changed

+277
-961
lines changed

10 files changed

+277
-961
lines changed

Cargo.lock

Lines changed: 138 additions & 954 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
[workspace]
2-
members = ["wasmtime", "cli"]
2+
members = ["cli", "wasmtime", "test-programs", "test-programs/artifacts"]
33

44
[workspace.package]
55
version = "0.1.0"
66
edition = "2024"
77

88
[workspace.dependencies]
9+
clap = { version = "4", features = ["derive"] }
910
component-init = { path = "." }
1011
component-init-wasmtime = { path = "./wasmtime" }
1112
anyhow = "1.0.89"
1213
async-trait = "0.1.83"
1314
futures = "0.3.31"
15+
tokio = { version = "1", features = ["full"] }
16+
test-programs = { path = "./test-programs" }
17+
test-programs-artifacts = { path = "./test-programs/artifacts" }
1418
wasm-encoder = { version = "0.235.0", features = ["wasmparser"] }
1519
wasmparser = "0.235.0"
1620
wasmtime = "34.0.1"
17-
wasmtime-wasi = "34.0.1"
18-
wasmtime-wasi-http = "34.0.1"
21+
# wasmtime-wasi = "34.0.1"
22+
# wasmtime-wasi-http = "34.0.1"
23+
wit-bindgen = "0.43.0"
1924

2025
[package]
2126
name = "component-init"

cli/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ path = "src/main.rs"
99

1010
[dependencies]
1111
anyhow.workspace = true
12-
clap = { version = "4", features = ["derive"] }
12+
clap.workspace = true
1313
component-init-wasmtime.workspace = true
14-
tokio = { version = "1", features = ["full"] }
14+
tokio.workspace = true

test-programs/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "test-programs"
3+
version.workspace = true
4+
edition.workspace = true
5+
publish = false
6+
7+
[dependencies]
8+
wit-bindgen.workspace = true

test-programs/artifacts/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "test-programs-artifacts"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
publish = false
6+
7+
[dependencies]
8+
9+
[build-dependencies]
10+
cargo_metadata = "0.18"
11+
heck = "0.5"

test-programs/artifacts/build.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use heck::ToShoutySnakeCase;
2+
use std::env::var_os;
3+
use std::path::PathBuf;
4+
use std::process::Command;
5+
6+
fn main() {
7+
let out_dir = PathBuf::from(var_os("OUT_DIR").expect("OUT_DIR env var exists"));
8+
9+
let meta = cargo_metadata::MetadataCommand::new()
10+
.exec()
11+
.expect("cargo metadata");
12+
let test_programs_meta = meta
13+
.packages
14+
.iter()
15+
.find(|p| p.name == "test-programs")
16+
.expect("test-programs is in cargo metadata");
17+
let test_programs_root = test_programs_meta.manifest_path.parent().unwrap();
18+
println!(
19+
"cargo:rerun-if-changed={}",
20+
test_programs_root.as_os_str().to_str().unwrap()
21+
);
22+
23+
let status = Command::new("cargo")
24+
.arg("build")
25+
.arg("--target=wasm32-wasip2")
26+
.arg("--package=test-programs")
27+
.env("CARGO_TARGET_DIR", &out_dir)
28+
.env("CARGO_PROFILE_DEV_DEBUG", "2")
29+
.env("RUSTFLAGS", rustflags())
30+
.env_remove("CARGO_ENCODED_RUSTFLAGS")
31+
.status()
32+
.expect("cargo build test programs");
33+
assert!(status.success());
34+
35+
let mut generated_code = "// THIS FILE IS GENERATED CODE\n".to_string();
36+
37+
for binary in test_programs_meta
38+
.targets
39+
.iter()
40+
.filter(|t| t.kind == ["bin"])
41+
{
42+
let component_path = out_dir
43+
.join("wasm32-wasip2")
44+
.join("debug")
45+
.join(format!("{}.wasm", binary.name));
46+
47+
let const_name = binary.name.to_shouty_snake_case();
48+
generated_code += &format!(
49+
"pub const {const_name}: &str = {:?};\n",
50+
component_path.as_os_str().to_str().expect("path is str")
51+
);
52+
}
53+
54+
std::fs::write(out_dir.join("gen.rs"), generated_code).unwrap();
55+
}
56+
57+
fn rustflags() -> &'static str {
58+
match option_env!("RUSTFLAGS") {
59+
Some(s) if s.contains("-D warnings") => "-D warnings",
60+
_ => "",
61+
}
62+
}

test-programs/artifacts/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include!(concat!(env!("OUT_DIR"), "/gen.rs"));

test-programs/src/bin/test.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::cell::Cell;
2+
3+
wit_bindgen::generate!({
4+
inline: r"
5+
package this:wit;
6+
world w {
7+
export component-init: func();
8+
}"
9+
});
10+
11+
const IS_INITIALIZED: Cell<bool> = Cell::new(false);
12+
13+
struct S;
14+
impl Guest for S {
15+
fn component_init() {
16+
let before = IS_INITIALIZED.replace(true);
17+
assert!(!before, "component should only be initialized once");
18+
}
19+
}
20+
21+
export!(S);
22+
23+
fn main() {
24+
let initialized = IS_INITIALIZED.get();
25+
assert!(initialized, "component was not initialized")
26+
}

wasmtime/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ anyhow.workspace = true
88
async-trait.workspace = true
99
component-init.workspace = true
1010
wasmtime.workspace = true
11-
wasmtime-wasi.workspace = true
12-
wasmtime-wasi-http.workspace = true
11+
# wasmtime-wasi.workspace = true
12+
# wasmtime-wasi-http.workspace = true
13+
14+
[dev-dependencies]
15+
tokio.workspace = true
16+
test-programs-artifacts.workspace = true

wasmtime/tests/test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use anyhow::Result;
2+
use test_programs_artifacts::TEST;
3+
4+
#[tokio::test]
5+
async fn initialize_test_component() -> Result<()> {
6+
let component = std::fs::read(TEST)?;
7+
8+
// TODO: execute `component`. it should trap.
9+
10+
let out = component_init_wasmtime::initialize(&component).await?;
11+
12+
// TODO: execute `out`. it should not trap.
13+
14+
Ok(())
15+
}

0 commit comments

Comments
 (0)