Skip to content

Commit 4c29127

Browse files
author
Pat Hickey
committed
test-programs build: ensure that the user writes a #[test] for each module, component
1 parent 1058345 commit 4c29127

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

crates/test-programs/build.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ fn build_and_generate_tests() {
5252
//components_rs(&meta, "wasi-http-tests", "bin", &command_adapter, &out_dir);
5353
}
5454

55-
// Creates an `${out_dir}/${package}_modules.rs` file that exposes a `get_module(&str) -> Module`
55+
// Creates an `${out_dir}/${package}_modules.rs` file that exposes a `get_module(&str) -> Module`,
56+
// and a contains a `use self::{module} as _;` for each module that ensures that the user defines
57+
// a symbol (ideally a #[test]) corresponding to each module.
5658
fn modules_rs(meta: &cargo_metadata::Metadata, package: &str, kind: &str, out_dir: &PathBuf) {
5759
let modules = targets_in_package(meta, package, kind)
5860
.into_iter()
@@ -73,8 +75,10 @@ fn modules_rs(meta: &cargo_metadata::Metadata, package: &str, kind: &str, out_di
7375

7476
let mut decls = String::new();
7577
let mut cases = String::new();
78+
let mut uses = String::new();
7679
for (stem, file) in modules {
7780
let global = format!("{}_MODULE", stem.to_uppercase());
81+
// Load the module from disk only once, in case it is used many times:
7882
decls += &format!(
7983
"
8084
lazy_static::lazy_static!{{
@@ -84,20 +88,24 @@ fn modules_rs(meta: &cargo_metadata::Metadata, package: &str, kind: &str, out_di
8488
}}
8589
"
8690
);
87-
cases += &format!("{stem:?} => {global}.clone(),");
91+
// Match the stem str literal to the module. Cloning is just a ref count incr.
92+
cases += &format!("{stem:?} => {global}.clone(),\n");
93+
// Statically ensure that the user defines a function (ideally a #[test]) for each stem.
94+
uses += &format!("#[allow(unused_imports)] use self::{stem} as _;\n");
8895
}
8996

9097
std::fs::write(
9198
out_dir.join(&format!("{}_modules.rs", package.to_snake_case())),
9299
format!(
93100
"
94-
{decls}\n
101+
{decls}
95102
pub fn get_module(s: &str) -> wasmtime::Module {{
96103
match s {{
97104
{cases}
98105
_ => panic!(\"no such module: {{}}\", s),
99106
}}
100107
}}
108+
{uses}
101109
"
102110
),
103111
)
@@ -135,6 +143,8 @@ fn build_adapter(out_dir: &PathBuf, name: &str, features: &[&str]) -> Vec<u8> {
135143

136144
// Builds components out of modules, and creates an `${out_dir}/${package}_component.rs` file that
137145
// exposes a `get_component(&str) -> Component`
146+
// and a contains a `use self::{component} as _;` for each module that ensures that the user defines
147+
// a symbol (ideally a #[test]) corresponding to each component.
138148
fn components_rs(
139149
meta: &cargo_metadata::Metadata,
140150
package: &str,
@@ -144,6 +154,7 @@ fn components_rs(
144154
) {
145155
let mut decls = String::new();
146156
let mut cases = String::new();
157+
let mut uses = String::new();
147158
for target_name in targets_in_package(&meta, package, kind) {
148159
let stem = target_name.to_snake_case();
149160
let file = compile_component(&stem, out_dir, adapter);
@@ -158,20 +169,22 @@ fn components_rs(
158169
}}
159170
"
160171
);
161-
cases += &format!("{stem:?} => {global}.clone(),");
172+
cases += &format!("{stem:?} => {global}.clone(),\n");
173+
uses += &format!("use self::{stem} as _;\n");
162174
}
163175

164176
std::fs::write(
165177
out_dir.join(&format!("{}_components.rs", package.to_snake_case())),
166178
format!(
167179
"
168-
{decls}\n
180+
{decls}
169181
pub fn get_component(s: &str) -> wasmtime::component::Component {{
170182
match s {{
171183
{cases}
172184
_ => panic!(\"no such component: {{}}\", s),
173185
}}
174186
}}
187+
{uses}
175188
"
176189
),
177190
)

0 commit comments

Comments
 (0)