Path: blob/main/crates/component-macro/tests/expanded.rs
1692 views
use std::path::Path;12use anyhow::{Context, Result, bail};34macro_rules! genexpand {5($id:ident $name:tt $path:tt) => {6process_expanded($path, "", wasmtime::component::bindgen!({7path: $path,8stringify: true,9}))?;1011process_expanded($path, "_async", wasmtime::component::bindgen!({12path: $path,13stringify: true,14imports: { default: async },15exports: { default: async },16}))?;1718process_expanded($path, "_concurrent", wasmtime::component::bindgen!({19path: $path,20imports: { default: async | store },21exports: { default: async | store },22stringify: true,23}))?;2425process_expanded($path, "_tracing_async", wasmtime::component::bindgen!({26path: $path,27imports: { default: async | tracing },28exports: { default: async | tracing },29stringify: true,30}))?;31};32}3334fn process_expanded(path: &str, suffix: &str, src: &str) -> Result<()> {35let formatted_src = {36let syn_file = syn::parse_file(src).unwrap();37prettyplease::unparse(&syn_file)38};39let expanded_path = {40let mut stem = Path::new(path).file_stem().unwrap().to_os_string();41stem.push(suffix);42Path::new("tests/expanded").join(stem).with_extension("rs")43};44if std::env::var("BINDGEN_TEST_BLESS").is_ok_and(|val| !val.is_empty()) {45std::fs::write(expanded_path, formatted_src)?;46} else {47match std::fs::read_to_string(&expanded_path) {48Ok(expected) if formatted_src == expected => (),49Ok(expected) => {50bail!(51"checked-in expanded bindings from {expanded_path:?} \52do not match those generated from {path:?}53\n\54{diff}\n\55\n\56This test assertion can be automatically updated by setting the\n\57BINDGEN_TEST_BLESS=1 environment variable when running this test.",58diff = similar::TextDiff::from_lines(&expected, &formatted_src)59.unified_diff()60.header("expected", "actual")61)62}63Err(err) => return Err(err).with_context(|| {64format!(65"failed to read {expanded_path:?}; re-run with BINDGEN_TEST_BLESS=1 to create"66)67}),68}69}70Ok(())71}7273#[test]74fn expand_wits() -> Result<()> {75component_macro_test_helpers::foreach!(genexpand);76Ok(())77}787980