Skip to content

Commit 33442e3

Browse files
authored
Migrate the rust_xcrate test to wit-bindgen test (#1236)
* Migrate the `rust_xcrate` test to `wit-bindgen test` This was a tricky test to migrate. Doing this required implementing a number of new features: * Rust now has a `[lang]`-specific configuration `externs = [...]` which is a list of files to compile as external crates. This tests the cross-crate behavior of the test. * Tests now support more than just runner/test composed components, now they also support multiple components being composed. This is required because the test was specifically testing behavior where a component simultaneously imports and exports. This last point was particularly tricky. Namely this required abstracting the definition of a test to a runner plus a list of tests, not just one test. These are still modeled as a list of `world` items in the original WIT document. Additionally I was having trouble getting `wasm-compose` working so I ended up switching to `wac`. This means that tests can now support custom `wac` composition scripts to configure how exactly the composition is created, and that's used in this new test to insert a component as a sandwich between two others. * Review comments
1 parent 5e7d46c commit 33442e3

File tree

21 files changed

+778
-355
lines changed

21 files changed

+778
-355
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ wit-bindgen-csharp = { workspace = true, features = ['clap'], optional = true }
6565
wit-bindgen-test = { workspace = true }
6666
wit-component = { workspace = true }
6767
wasm-encoder = { workspace = true }
68+
env_logger = "0.11.7"
6869

6970
[features]
7071
default = [

crates/test-rust-wasm/Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ publish = false
66

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

1110
[lib]
1211
test = false
@@ -71,7 +70,3 @@ test = false
7170
[[bin]]
7271
name = "resource_borrow_simple"
7372
test = false
74-
75-
[[bin]]
76-
name = "rust_xcrate"
77-
test = false

crates/test-rust-wasm/rust-xcrate-test/Cargo.toml

Lines changed: 0 additions & 7 deletions
This file was deleted.

crates/test-rust-wasm/src/bin/rust_xcrate.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

crates/test/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ regex = "1.11.1"
2424
serde = { version = "1.0.218", features = ["derive"] }
2525
toml = "0.8.20"
2626
wasi-preview1-component-adapter-provider = "30.0.2"
27-
wasm-compose = { workspace = true }
27+
wac-parser = "0.6.1"
28+
wac-types = "0.6.1"
29+
wac-graph = "0.6.1"
30+
indexmap = { workspace = true }
2831
wasm-encoder = { workspace = true }
2932
wasmparser = { workspace = true, features = ["features"] }
3033
wat = { workspace = true }

crates/test/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,67 @@ have `runner-std.rs` and `runner-nostd.rs` to test with and without the
208208
bindings generator flags it's expected that the original `runner` or `test`
209209
worlds are still adhered to.
210210

211+
#### Test Configuration: World Names
212+
213+
By default `runner` and `test` worlds are expected, but this can be configured
214+
with:
215+
216+
```wit
217+
//@ runner = "other-runner"
218+
//@ dependencies = ["other-test"]
219+
220+
package foo:bar;
221+
222+
world other-runner {
223+
// ...
224+
}
225+
226+
world other-test {
227+
// ...
228+
}
229+
```
230+
231+
This will then expect `other-runner.rs` for example as a test file, so test
232+
files are still named after their worlds.
233+
234+
#### Test Configuration: Fancy Compositions
235+
236+
The `wac` tooling is available for composing components together. This can be
237+
configured with `dependencies` and `wac` keys:
238+
239+
```wit
240+
//@ dependencies = ["intermediate", "leaf"]
241+
//@ wac = "./compose.wac"
242+
243+
package foo:bar;
244+
245+
world runner {
246+
// ...
247+
}
248+
249+
world intermediate {
250+
// ...
251+
}
252+
253+
world leaf {
254+
// ...
255+
}
256+
```
257+
258+
This would then require a `compose.wac` file in the test directory. Components
259+
named `test:{world}` are made available to the script to perform compositions
260+
with:
261+
262+
```wac
263+
package example:composition;
264+
265+
let leaf = new test:leaf { ... };
266+
let intermediate = new test:intermediate { ...leaf, ... };
267+
let runner = new test:runner { ...intermediate, ... };
268+
269+
export runner...;
270+
```
271+
211272
## Language Support
212273

213274
Currently the `wit-bindgen test` CLI comes with built-in language support for a
@@ -236,3 +297,26 @@ This would recognize the `rs` file extension and use the
236297
`./wit-bindgen-rust-runner` script or binary to execute tests. The exact
237298
interface to the tests is documented as part of `wit-bindgen test --help` for
238299
the `--custom` argument.
300+
301+
#### Configuration: Rust
302+
303+
Rust configuration supports a few keys at the top of files in addition to the
304+
default `args` option for bindings generator options
305+
306+
```rust
307+
//@ [lang]
308+
//@ rustflags = '-O'
309+
//@ externs = ['./other.rs']
310+
```
311+
312+
Here the crate will be compiled with `-O` and `./other.rs` will be compiled as a
313+
separate crate and passed as `--extern`
314+
315+
#### Configuration: C
316+
317+
C/C++ configuration supports configuring compilation flags at this time:
318+
319+
```rust
320+
//@ [lang]
321+
//@ cflags = '-O'
322+
```

crates/test/src/config.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub struct RuntimeTestConfig<T = HashMap<String, toml::Value>> {
7575
pub lang: Option<T>,
7676
}
7777

78-
#[derive(Deserialize)]
78+
#[derive(Deserialize, Clone, Debug)]
7979
#[serde(untagged)]
8080
pub enum StringList {
8181
String(String),
@@ -114,6 +114,32 @@ pub struct WitConfig {
114114
/// arguments. For example with Rust it avoids passing `--generate-all` by
115115
/// default to bindings generation.
116116
pub default_bindgen_args: Option<bool>,
117+
118+
/// Name of the world for the "runner" component, and note that this affects
119+
/// filenames as well.
120+
pub runner: Option<String>,
121+
122+
/// List of worlds for "test" components. This affects filenames and these
123+
/// are all available to import to the "runner".
124+
pub dependencies: Option<StringList>,
125+
126+
/// Path to a `*.wac` file to specify how composition is done.
127+
pub wac: Option<String>,
128+
}
129+
130+
impl WitConfig {
131+
/// Returns the name of the "runner" world
132+
pub fn runner_world(&self) -> &str {
133+
self.runner.as_deref().unwrap_or("runner")
134+
}
135+
136+
/// Returns the list of dependency worlds that this configuration uses.
137+
pub fn dependency_worlds(&self) -> Vec<String> {
138+
match self.dependencies.clone() {
139+
Some(list) => list.into(),
140+
None => vec!["test".to_string()],
141+
}
142+
}
117143
}
118144

119145
/// Parses the configuration `T` from `contents` in comments at the start of the

0 commit comments

Comments
 (0)