Skip to content

Commit f88d693

Browse files
committed
Add support for deriving packages from WIT files in the graph tests.
1 parent 6f3b62c commit f88d693

6 files changed

Lines changed: 63 additions & 95 deletions

File tree

Cargo.lock

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

crates/wac-graph/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ wasmprinter = { workspace = true }
2828
wat = { workspace = true }
2929
serde = { workspace = true }
3030
serde_json = { workspace = true }
31+
wit-component = { workspace = true, features = ["dummy-module"] }
32+
wit-parser = { workspace = true }
3133

3234
[features]
3335
serde = ["dep:serde", "wac-types/serde"]

crates/wac-graph/tests/encoding.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ use semver::Version;
44
use serde::Deserialize;
55
use std::{
66
collections::HashMap,
7+
ffi::OsStr,
78
fs::{self, File},
89
path::{Path, PathBuf},
910
};
1011
use wac_graph::{types::Package, CompositionGraph, EncodeOptions, NodeId, PackageId};
12+
use wit_component::{ComponentEncoder, StringEncoding};
13+
use wit_parser::Resolve;
1114

1215
/// Represents a node to add to a composition graph.
1316
#[derive(Deserialize)]
@@ -118,12 +121,52 @@ impl GraphFile {
118121

119122
for (index, package) in self.packages.iter().enumerate() {
120123
let path = root.join(&package.path);
121-
let bytes = wat::parse_file(&path).with_context(|| {
122-
format!(
123-
"failed to parse package `{path}` for test case `{test_case}`",
124-
path = path.display()
125-
)
126-
})?;
124+
let bytes = match path.extension().and_then(OsStr::to_str) {
125+
Some("wit") => {
126+
let mut resolve = Resolve::default();
127+
let id = resolve.push_file(&path).with_context(|| {
128+
format!(
129+
"failed to parse package file `{path}` for test case `{test_case}`",
130+
path = path.display()
131+
)
132+
})?;
133+
let world = resolve.select_world(id, None).with_context(|| {
134+
format!(
135+
"failed to select world from `{path}` for test case `{test_case}`",
136+
path = path.display()
137+
)
138+
})?;
139+
140+
let mut module = wit_component::dummy_module(&resolve, world);
141+
wit_component::embed_component_metadata(
142+
&mut module,
143+
&resolve,
144+
world,
145+
StringEncoding::default(),
146+
)
147+
.with_context(|| {
148+
format!(
149+
"failed to embed component metadata from package `{path}` for test case `{test_case}`",
150+
path = path.display()
151+
)
152+
})?;
153+
154+
let encoder = ComponentEncoder::default().validate(true).module(&module)?;
155+
encoder
156+
.encode()
157+
.with_context(|| format!("failed to encode a component from module derived from package `{path}` for test case `{test_case}`", path = path.display()))?
158+
}
159+
Some("wat") => wat::parse_file(&path).with_context(|| {
160+
format!(
161+
"failed to parse package `{path}` for test case `{test_case}`",
162+
path = path.display()
163+
)
164+
})?,
165+
_ => bail!(
166+
"unexpected file extension for package file `{path}`",
167+
path = package.path.display()
168+
),
169+
};
127170

128171
let package = Package::from_bytes(&package.name, package.version.as_ref(), bytes)
129172
.with_context(|| {
@@ -226,6 +269,9 @@ impl GraphFile {
226269
/// * [required] `graph.json` - a JSON representation of a composition graph
227270
/// (see above for serialization format).
228271
/// * [optional] `*.wat` - packages (i.e. components) referenced from `graph.json`.
272+
/// * [optional] `*.wit` - packages (i.e. components) referenced from `graph.json`;
273+
/// the file is expected to contain a single world representing the world of
274+
/// the component; a dummy module will be created to implement the component.
229275
///
230276
/// And the output files are one of the following:
231277
///

crates/wac-graph/tests/graphs/merged-usings/component.wat

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

crates/wac-graph/tests/graphs/merged-usings/encoded.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
(export "memory" (memory 0))
5252
(export "cabi_realloc" (func 3))
5353
(@producers
54-
(processed-by "wit-component" "0.201.0")
54+
(processed-by "wit-component" "0.202.0")
5555
)
5656
)
5757
(alias export 0 "x" (type (;3;)))
@@ -96,7 +96,7 @@
9696
)
9797
(export (;3;) "foo:bar/qux" (instance 2))
9898
(@producers
99-
(processed-by "wit-component" "0.201.0")
99+
(processed-by "wit-component" "0.202.0")
100100
)
101101
)
102102
(instance $component2 (;2;) (instantiate 0
@@ -140,7 +140,7 @@
140140
(export "memory" (memory 0))
141141
(export "cabi_realloc" (func 3))
142142
(@producers
143-
(processed-by "wit-component" "0.201.0")
143+
(processed-by "wit-component" "0.202.0")
144144
)
145145
)
146146
(alias export 0 "x" (type (;3;)))
@@ -185,7 +185,7 @@
185185
)
186186
(export (;3;) "foo:bar/qux" (instance 2))
187187
(@producers
188-
(processed-by "wit-component" "0.201.0")
188+
(processed-by "wit-component" "0.202.0")
189189
)
190190
)
191191
(instance $component1 (;4;) (instantiate 1

crates/wac-graph/tests/graphs/merged-usings/graph.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"packages": [
33
{
44
"name": "test:foo",
5-
"path": "component.wat"
5+
"path": "component.wit"
66
},
77
{
88
"name": "test:bar",
9-
"path": "component.wat"
9+
"path": "component.wit"
1010
}
1111
],
1212
"nodes": [

0 commit comments

Comments
 (0)