Skip to content

Commit b18643c

Browse files
authored
test(*): hardening codegen tests by adding wasi proposals as submodules (#919)
* test(*): hardening codegen tests by adding wasi proposals as submodules this commit adds a few git submodules for wasi proposals and placed them in `tests/codegen` folder for testing purposes. it also changes the structure now that it requires wit packages to have a `wit` folder inside the directory similar to how wasi proposal structures. Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> * test(rust): skip wasi-http and wasi-cli both have multiple worlds which require another refactoring of the current test codebase to adopt that Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> * test(*): skip wasi-* tests on teavm and csharp Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> --------- Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
1 parent e7e18d7 commit b18643c

File tree

20 files changed

+90
-24
lines changed

20 files changed

+90
-24
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ jobs:
6060
runs-on: ${{ matrix.os }}
6161
steps:
6262
- uses: actions/checkout@v4
63+
with:
64+
submodules: true
6365
- name: Install Rust
6466
run: rustup update stable --no-self-update && rustup default stable
6567
- name: Install wasm32-unknown-unknown target

.gitmodules

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[submodule "tests/codegen/wasi-filesystem"]
2+
path = tests/codegen/wasi-filesystem
3+
url = https://github.com/WebAssembly/wasi-filesystem
4+
[submodule "tests/codegen/wasi-http"]
5+
path = tests/codegen/wasi-http
6+
url = https://github.com/WebAssembly/wasi-http
7+
[submodule "tests/codegen/wasi-cli"]
8+
path = tests/codegen/wasi-cli
9+
url = https://github.com/WebAssembly/wasi-cli
10+
[submodule "tests/codegen/wasi-io"]
11+
path = tests/codegen/wasi-io
12+
url = https://github.com/WebAssembly/wasi-io
13+
[submodule "tests/codegen/wasi-clocks"]
14+
path = tests/codegen/wasi-clocks
15+
url = https://github.com/WebAssembly/wasi-clocks

crates/csharp/tests/codegen.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ macro_rules! codegen_test {
5656
"worlds-with-types",
5757
"variants-unioning-types",
5858
"go_params",
59+
"wasi-cli",
60+
"wasi-clocks",
61+
"wasi-filesystem",
62+
"wasi-http",
63+
"wasi-io",
5964
]
6065
.contains(&$name)
6166
{

crates/rust/tests/codegen.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
mod codegen_tests {
55
macro_rules! codegen_test {
6+
(wasi_cli $name:tt $test:tt) => {};
7+
(wasi_http $name:tt $test:tt) => {};
68
($id:ident $name:tt $test:tt) => {
79
mod $id {
810
wit_bindgen::generate!({

crates/rust/tests/codegen_no_std.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ extern crate alloc;
1414

1515
mod codegen_tests {
1616
macro_rules! codegen_test {
17+
(wasi_cli $name:tt $test:tt) => {};
18+
(wasi_http $name:tt $test:tt) => {};
1719
($id:ident $name:tt $test:tt) => {
1820
mod $id {
1921
wit_bindgen::generate!({

crates/teavm-java/tests/codegen.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ macro_rules! codegen_test {
2020
(resources_in_aggregates $name:tt $test:tt) => {};
2121
(issue668 $name:tt $test:tt) => {};
2222
(multiversion $name:tt $test:tt) => {};
23+
(wasi_cli $name:tt $test:tt) => {};
24+
(wasi_clocks $name:tt $test:tt) => {};
25+
(wasi_filesystem $name:tt $test:tt) => {};
26+
(wasi_http $name:tt $test:tt) => {};
27+
(wasi_io $name:tt $test:tt) => {};
2328

2429
($id:ident $name:tt $test:tt) => {
2530
#[test]

crates/test-helpers/codegen-macro/src/lib.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@ use std::env;
77
/// `codegen_test!` macro then does what's necessary to actually run the test.
88
#[proc_macro]
99
pub fn codegen_tests(_input: TokenStream) -> TokenStream {
10-
let mut tests = Vec::new();
11-
for entry in env::current_dir()
12-
.unwrap()
13-
.join("tests/codegen")
10+
let tests_dir = env::current_dir().unwrap().join("tests/codegen");
11+
let tests = tests_dir
1412
.read_dir()
1513
.unwrap()
16-
{
17-
let entry = entry.unwrap();
18-
let test = entry.path();
14+
.filter_map(|entry| {
15+
let entry = entry.ok()?;
16+
let path = entry.path();
17+
let is_dir = entry.file_type().unwrap().is_dir();
18+
if is_dir || path.extension().and_then(|s| s.to_str()) == Some("wit") {
19+
let test_path = if is_dir {
20+
path.join("wit")
21+
} else {
22+
path.clone()
23+
};
24+
let name = path.file_stem().unwrap().to_str().unwrap();
25+
let ident = quote::format_ident!("{}", name.to_snake_case());
26+
let path = test_path.to_str().unwrap();
27+
Some(quote::quote! {
28+
codegen_test!(#ident #name #path);
29+
})
30+
} else {
31+
None
32+
}
33+
})
34+
.collect::<Vec<_>>();
1935

20-
if entry.file_type().unwrap().is_dir()
21-
|| test.extension().and_then(|s| s.to_str()) == Some("wit")
22-
{
23-
let name = test.file_stem().unwrap().to_str().unwrap();
24-
let path = test.to_str().unwrap();
25-
let ident = quote::format_ident!("{}", name.to_snake_case());
26-
tests.push(quote::quote! {
27-
codegen_test!(#ident #name #path);
28-
});
29-
}
30-
}
3136
(quote::quote!(#(#tests)*)).into()
3237
}

crates/test-helpers/src/lib.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ pub fn run_world_codegen_test(
6666
let (resolve, world) = parse_wit(wit_path);
6767
let world_name = &resolve.worlds[world].name;
6868

69-
let wit_name = wit_path.file_stem().and_then(|s| s.to_str()).unwrap();
69+
let wit_name = if wit_path.is_dir() {
70+
wit_path
71+
.parent()
72+
.unwrap()
73+
.file_stem()
74+
.and_then(|s| s.to_str())
75+
.unwrap()
76+
} else {
77+
wit_path.file_stem().and_then(|s| s.to_str()).unwrap()
78+
};
7079
let gen_name = format!("{gen_name}-{wit_name}");
7180
let dir = test_directory("codegen", &gen_name, &world_name);
7281

@@ -106,7 +115,16 @@ pub fn run_component_codegen_test(
106115
.encode()
107116
.unwrap();
108117

109-
let wit_name = wit_path.file_stem().and_then(|s| s.to_str()).unwrap();
118+
let wit_name = if wit_path.is_dir() {
119+
wit_path
120+
.parent()
121+
.unwrap()
122+
.file_stem()
123+
.and_then(|s| s.to_str())
124+
.unwrap()
125+
} else {
126+
wit_path.file_stem().and_then(|s| s.to_str()).unwrap()
127+
};
110128

111129
let gen_name = format!("{gen_name}-{wit_name}",);
112130
let dir = test_directory("codegen", &gen_name, &world_name);
@@ -126,6 +144,9 @@ pub fn run_component_codegen_test(
126144
fn parse_wit(path: &Path) -> (Resolve, WorldId) {
127145
let mut resolve = Resolve::default();
128146
let (pkg, _files) = resolve.push_path(path).unwrap();
129-
let world = resolve.select_world(pkg, None).unwrap();
147+
let world = resolve.select_world(pkg, None).unwrap_or_else(|_| {
148+
// note: if there are multiples worlds in the wit package, we assume the "imports" world
149+
resolve.select_world(pkg, Some("imports")).unwrap()
150+
});
130151
(resolve, world)
131152
}

tests/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ Read on to learn more about the testing layout. It's all a bit convoluted so fee
4141

4242
## Testing wit-bindgen - `codegen`
4343

44-
Any tests placed into the `tests/codegen` directory should be raw `*.wit`
45-
files. These files will be executed in all code generators by default most
46-
likely, and the purpose of these files is to execute language-specific
44+
Any tests placed in to the `tests/codegen` directory should follow either of the following formats:
45+
1. `*.wit` files that are raw wit files that should be executed by the code generator.
46+
2. wit package in it's own directory which must contain a `wit` subdirectory with `*.wit` files and `deps` in it. (e.g. see` tests/codegen/issue569`)
47+
48+
The purpose of these files is to execute language-specific
4749
validation for each bindings generator. Basically if there's a bug where
4850
something generates invalid code then this is probably where the test should go.
4951
Note that this directory can have whatever it wants since nothing implements the
5052
interfaces or tries to call them.
5153

54+
It also contains git submodules for the wasi proposals like `wasi-http`.
55+
5256
The tests are generated by a macro `codegen_tests` in [crates/test-helpers](../crates/test-helpers/).
5357

5458
## Testing wit-bindgen - `runtime`

0 commit comments

Comments
 (0)