Skip to content

Commit 3395dd6

Browse files
authored
Conditionally use wac or wasm-compose for wit-bindgen test (#1285)
While `wac` is being merged upstream into `wasm-tools` fall back to using `wasm-compose` when `wac` is not otherwise necessary. This ensures that there's a composition tool used which is at the same version as all the other wasm-tools tools which `wac` doesn't guarantee just yet.
1 parent a2327aa commit 3395dd6

File tree

4 files changed

+169
-27
lines changed

4 files changed

+169
-27
lines changed

Cargo.lock

Lines changed: 116 additions & 9 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
@@ -37,6 +37,7 @@ wasm-encoder = "0.229.0"
3737
wasm-metadata = { version = "0.229.0", default-features = false }
3838
wit-parser = "0.229.0"
3939
wit-component = "0.229.0"
40+
wasm-compose = "0.229.0"
4041

4142
wit-bindgen-core = { path = 'crates/core', version = '0.41.0' }
4243
wit-bindgen-c = { path = 'crates/c', version = '0.41.0' }

crates/test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ wasi-preview1-component-adapter-provider = "30.0.2"
2727
wac-parser = "0.6.1"
2828
wac-types = "0.6.1"
2929
wac-graph = "0.6.1"
30+
wasm-compose = { workspace = true }
3031
indexmap = { workspace = true }
3132
wasm-encoder = { workspace = true }
3233
wasmparser = { workspace = true, features = ["features"] }

crates/test/src/lib.rs

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,55 @@ impl Runner<'_> {
832832
runner_wasm: &Path,
833833
test_components: &[(&Component, &Path)],
834834
) -> Result<()> {
835+
// If possible use `wasm-compose` to compose the test together. This is
836+
// only possible when customization isn't used though. This is also only
837+
// done for async tests at this time to ensure that there's a version of
838+
// composition that's done which is at the same version as wasmparser
839+
// and friends.
840+
let composed = if case.config.wac.is_none() && test_components.len() == 1 {
841+
self.compose_wasm_with_wasm_compose(runner_wasm, test_components)?
842+
} else {
843+
self.compose_wasm_with_wac(case, runner, runner_wasm, test_components)?
844+
};
845+
846+
let dst = runner_wasm.parent().unwrap();
847+
let mut filename = format!(
848+
"composed-{}",
849+
runner.path.file_name().unwrap().to_str().unwrap(),
850+
);
851+
for (test, _) in test_components {
852+
filename.push_str("-");
853+
filename.push_str(test.path.file_name().unwrap().to_str().unwrap());
854+
}
855+
filename.push_str(".wasm");
856+
let composed_wasm = dst.join(filename);
857+
write_if_different(&composed_wasm, &composed)?;
858+
859+
self.run_command(self.test_runner.command().arg(&composed_wasm))?;
860+
Ok(())
861+
}
862+
863+
fn compose_wasm_with_wasm_compose(
864+
&self,
865+
runner_wasm: &Path,
866+
test_components: &[(&Component, &Path)],
867+
) -> Result<Vec<u8>> {
868+
assert!(test_components.len() == 1);
869+
let test_wasm = test_components[0].1;
870+
let mut config = wasm_compose::config::Config::default();
871+
config.definitions = vec![test_wasm.to_path_buf()];
872+
wasm_compose::composer::ComponentComposer::new(runner_wasm, &config)
873+
.compose()
874+
.with_context(|| format!("failed to compose {runner_wasm:?} with {test_wasm:?}"))
875+
}
876+
877+
fn compose_wasm_with_wac(
878+
&self,
879+
case: &Test,
880+
runner: &Component,
881+
runner_wasm: &Path,
882+
test_components: &[(&Component, &Path)],
883+
) -> Result<Vec<u8>> {
835884
let document = match &case.config.wac {
836885
Some(path) => {
837886
let wac_config = case.path.join(path);
@@ -891,31 +940,15 @@ impl Runner<'_> {
891940
// TODO: should figure out how to render these errors better.
892941
let document =
893942
wac_parser::Document::parse(&document).context("failed to parse wac script")?;
894-
let composed = document
943+
document
895944
.resolve(packages)
896945
.context("failed to run `wac` resolve")?
897946
.encode(wac_graph::EncodeOptions {
898947
define_components: true,
899948
validate: false,
900949
processor: None,
901950
})
902-
.context("failed to encode `wac` result")?;
903-
904-
let dst = runner_wasm.parent().unwrap();
905-
let mut filename = format!(
906-
"composed-{}",
907-
runner.path.file_name().unwrap().to_str().unwrap(),
908-
);
909-
for (test, _) in test_components {
910-
filename.push_str("-");
911-
filename.push_str(test.path.file_name().unwrap().to_str().unwrap());
912-
}
913-
filename.push_str(".wasm");
914-
let composed_wasm = dst.join(filename);
915-
write_if_different(&composed_wasm, &composed)?;
916-
917-
self.run_command(self.test_runner.command().arg(&composed_wasm))?;
918-
Ok(())
951+
.context("failed to encode `wac` result")
919952
}
920953

921954
/// Helper to execute an external process and generate a helpful error

0 commit comments

Comments
 (0)