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