MCPcopy Create free account
hub / github.com/bytecodealliance/wit-bindgen / run_component_codegen_test

Function run_component_codegen_test

crates/test-helpers/src/lib.rs:92–141  ·  view source on GitHub ↗
(
    gen_name: &str,
    wit_path: &Path,
    generate: fn(&str, &[u8], &mut Files),
    verify: fn(&Path, &str),
)

Source from the content-addressed store, hash-verified

90}
91
92pub fn run_component_codegen_test(
93 gen_name: &str,
94 wit_path: &Path,
95 generate: fn(&str, &[u8], &mut Files),
96 verify: fn(&Path, &str),
97) {
98 let (resolve, world) = parse_wit(wit_path);
99 let world_name = &resolve.worlds[world].name;
100 let mut wasm = wit_component::dummy_module(&resolve, world, ManglingAndAbi::Standard32);
101 let encoded =
102 wit_component::metadata::encode(&resolve, world, StringEncoding::UTF8, None).unwrap();
103 let section = wasm_encoder::CustomSection {
104 name: std::borrow::Cow::Borrowed("component-type"),
105 data: std::borrow::Cow::Borrowed(&encoded),
106 };
107 wasm.push(section.id());
108 section.encode(&mut wasm);
109
110 let component = wit_component::ComponentEncoder::default()
111 .module(&wasm)
112 .unwrap()
113 .validate(true)
114 .encode()
115 .unwrap();
116
117 let wit_name = if wit_path.is_dir() {
118 wit_path
119 .parent()
120 .unwrap()
121 .file_stem()
122 .and_then(|s| s.to_str())
123 .unwrap()
124 } else {
125 wit_path.file_stem().and_then(|s| s.to_str()).unwrap()
126 };
127
128 let gen_name = format!("{gen_name}-{wit_name}",);
129 let dir = test_directory("codegen", &gen_name, &world_name);
130 std::fs::write(dir.join("component.wasm"), &component).unwrap();
131
132 let mut files = Default::default();
133 generate(&world_name, &component, &mut files);
134 for (file, contents) in files.iter() {
135 let dst = dir.join(file);
136 std::fs::create_dir_all(dst.parent().unwrap()).unwrap();
137 std::fs::write(&dst, contents).unwrap();
138 }
139
140 verify(&dir, &world_name);
141}
142
143fn parse_wit(path: &Path) -> (Resolve, WorldId) {
144 let mut resolve = Resolve::default();

Callers

nothing calls this directly

Calls 9

parse_witFunction · 0.85
test_directoryFunction · 0.85
generateFunction · 0.85
encodeMethod · 0.80
and_thenMethod · 0.80
joinMethod · 0.80
verifyFunction · 0.50
pushMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected