Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate the rust_xcrate test to wit-bindgen test #1236

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
329 changes: 238 additions & 91 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ wit-bindgen-csharp = { workspace = true, features = ['clap'], optional = true }
wit-bindgen-test = { workspace = true }
wit-component = { workspace = true }
wasm-encoder = { workspace = true }
env_logger = "0.11.7"

[features]
default = [
Expand Down
5 changes: 0 additions & 5 deletions crates/test-rust-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ publish = false

[dependencies]
wit-bindgen = { path = "../guest-rust" }
rust-xcrate-test = { path = './rust-xcrate-test' }

[lib]
test = false
Expand Down Expand Up @@ -71,7 +70,3 @@ test = false
[[bin]]
name = "resource_borrow_simple"
test = false

[[bin]]
name = "rust_xcrate"
test = false
7 changes: 0 additions & 7 deletions crates/test-rust-wasm/rust-xcrate-test/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions crates/test-rust-wasm/src/bin/rust_xcrate.rs

This file was deleted.

5 changes: 4 additions & 1 deletion crates/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ regex = "1.11.1"
serde = { version = "1.0.218", features = ["derive"] }
toml = "0.8.20"
wasi-preview1-component-adapter-provider = "30.0.2"
wasm-compose = { workspace = true }
wac-parser = "0.6.1"
wac-types = "0.6.1"
wac-graph = "0.6.1"
indexmap = { workspace = true }
wasm-encoder = { workspace = true }
wasmparser = { workspace = true, features = ["features"] }
wat = { workspace = true }
Expand Down
84 changes: 84 additions & 0 deletions crates/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,67 @@ have `runner-std.rs` and `runner-nostd.rs` to test with and without the
bindings generator flags it's expected that the original `runner` or `test`
worlds are still adhered to.

#### Test Configuration: World Names

By default `runner` and `test` worlds are expected, but this can be configured
with:

```wit
//@ runner = "other-runner"
//@ dependencies = ["other-test"]

package foo:bar;

world other-runner {
// ...
}

world other-test {
// ...
}
```

This will then expect `other-runner.rs` for example as a test file, so test
files are still named after their worlds.

#### Test Configuration: Fancy Compositions

The `wac` tooling is available for composing components together. This can be
configured with `dependencies` and `wac` keys:

```wit
//@ dependencies = ["intermediate", "leaf"]
//@ wac = "./compose.wac"

package foo:bar;

world runner {
// ...
}

world intermediate {
// ...
}

world leaf {
// ...
}
```

This would then require a `compose.wac` file in the test directory. Components
named `test:{world}` are made available to the script to perform compositions
with:

```wac
package example:composition;

let leaf = new test:leaf { ... };
let intermediate = new test:intermediate { ...leaf, ... };
let runner = new test:runner { ...intermediate, ... };

export runner...;
```

## Language Support

Currently the `wit-bindgen test` CLI comes with built-in language support for a
Expand Down Expand Up @@ -236,3 +297,26 @@ This would recognize the `rs` file extension and use the
`./wit-bindgen-rust-runner` script or binary to execute tests. The exact
interface to the tests is documented as part of `wit-bindgen test --help` for
the `--custom` argument.

#### Configuration: Rust

Rust configuration supports a few keys at the top of files in addition to the
default `args` option for bindings generator options

```rust
//@ [lang]
//@ rustflags = '-O'
//@ externs = ['./other.rs']
```

Here the crate will be compiled with `-O` and `./other.rs` will be compiled as a
separate crate and passed as `--extern`

#### Configuration: C

C/C++ configuration supports configuring compilation flags at this time:

```rust
//@ [lang]
//@ cflags = '-O'
```
28 changes: 27 additions & 1 deletion crates/test/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub struct RuntimeTestConfig<T = HashMap<String, toml::Value>> {
pub lang: Option<T>,
}

#[derive(Deserialize)]
#[derive(Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum StringList {
String(String),
Expand Down Expand Up @@ -114,6 +114,32 @@ pub struct WitConfig {
/// arguments. For example with Rust it avoids passing `--generate-all` by
/// default to bindings generation.
pub default_bindgen_args: Option<bool>,

/// Name of the world for the "runner" component, and note that this affects
/// filenames as well.
pub runner: Option<String>,

/// List of worlds for "test" components. This affects filenames and these
/// are all available to import to the "runner".
pub dependencies: Option<StringList>,

/// Path to a `*.wac` file to specify how composition is done.
pub wac: Option<String>,
}

impl WitConfig {
/// Returns the name of the "runner" world
pub fn runner_world(&self) -> &str {
self.runner.as_deref().unwrap_or("runner")
}

/// Returns the list of dependency worlds that this configuration uses.
pub fn dependency_worlds(&self) -> Vec<String> {
match self.dependencies.clone() {
Some(list) => list.into(),
None => vec!["test".to_string()],
}
}
}

/// Parses the configuration `T` from `contents` in comments at the start of the
Expand Down
Loading