|
34 | 34 | //!
|
35 | 35 | //! Multiple dependencies can be declared in a single test file using multiple
|
36 | 36 | //! `DEPENDENCY:` comments.
|
| 37 | +//! |
| 38 | +//! ## Custom CLI flags |
| 39 | +//! |
| 40 | +//! By default, tests will use the `bundler` target. Custom CLI flags can be |
| 41 | +//! passed to the `wasm-bindgen` CLI by declaring them in a comment at the top |
| 42 | +//! of the test file. For example: |
| 43 | +//! |
| 44 | +//! ```rust |
| 45 | +//! // FLAGS: --target=web --reference-types |
| 46 | +//! ``` |
| 47 | +//! |
| 48 | +//! Multiple comments can be used to run the test multiple times with different |
| 49 | +//! flags. |
| 50 | +//! |
| 51 | +//! ```rust |
| 52 | +//! // FLAGS: --target=web |
| 53 | +//! // FLAGS: --target=nodejs |
| 54 | +//! ``` |
37 | 55 |
|
38 | 56 | use anyhow::{bail, Result};
|
39 | 57 | use assert_cmd::prelude::*;
|
@@ -101,6 +119,16 @@ fn runtest(test: &Path) -> Result<()> {
|
101 | 119 | let root = repo_root();
|
102 | 120 | let root = root.display();
|
103 | 121 |
|
| 122 | + // parse target declarations |
| 123 | + let mut all_flags: Vec<_> = contents |
| 124 | + .lines() |
| 125 | + .filter_map(|l| l.strip_prefix("// FLAGS: ")) |
| 126 | + .map(|l| l.trim()) |
| 127 | + .collect(); |
| 128 | + if all_flags.is_empty() { |
| 129 | + all_flags.push(""); |
| 130 | + } |
| 131 | + |
104 | 132 | // parse additional dependency declarations
|
105 | 133 | let dependencies = contents
|
106 | 134 | .lines()
|
@@ -144,25 +172,58 @@ fn runtest(test: &Path) -> Result<()> {
|
144 | 172 | .join("debug")
|
145 | 173 | .join("reference_test.wasm");
|
146 | 174 |
|
147 |
| - let mut bindgen = Command::cargo_bin("wasm-bindgen")?; |
148 |
| - bindgen |
149 |
| - .arg("--out-dir") |
150 |
| - .arg(td.path()) |
151 |
| - .arg(&wasm) |
152 |
| - .arg("--remove-producers-section"); |
153 |
| - if contents.contains("// enable-externref") { |
154 |
| - bindgen.env("WASM_BINDGEN_EXTERNREF", "1"); |
155 |
| - } |
156 |
| - exec(&mut bindgen)?; |
| 175 | + for (flags_index, &flags) in all_flags.iter().enumerate() { |
| 176 | + // extract the target from the flags |
| 177 | + let target = flags |
| 178 | + .split_whitespace() |
| 179 | + .find_map(|f| f.strip_prefix("--target=")) |
| 180 | + .unwrap_or("bundler"); |
| 181 | + |
| 182 | + let out_dir = &td.path().join(target); |
| 183 | + fs::create_dir(out_dir)?; |
| 184 | + |
| 185 | + let mut bindgen = Command::cargo_bin("wasm-bindgen")?; |
| 186 | + bindgen |
| 187 | + .arg("--out-dir") |
| 188 | + .arg(out_dir) |
| 189 | + .arg(&wasm) |
| 190 | + .arg("--remove-producers-section"); |
| 191 | + for flag in flags.split_whitespace() { |
| 192 | + bindgen.arg(flag); |
| 193 | + } |
| 194 | + if contents.contains("// enable-externref") { |
| 195 | + bindgen.env("WASM_BINDGEN_EXTERNREF", "1"); |
| 196 | + } |
| 197 | + exec(&mut bindgen)?; |
157 | 198 |
|
158 |
| - if !contents.contains("async") { |
159 |
| - let js = fs::read_to_string(td.path().join("reference_test_bg.js"))?; |
160 |
| - assert_same(&js, &test.with_extension("js"))?; |
161 |
| - let wat = sanitize_wasm(&td.path().join("reference_test_bg.wasm"))?; |
162 |
| - assert_same(&wat, &test.with_extension("wat"))?; |
| 199 | + // suffix the file name with the target |
| 200 | + let test = if all_flags.len() > 1 { |
| 201 | + let base_file_name = format!( |
| 202 | + "{}-{}.rs", |
| 203 | + test.file_stem().unwrap().to_string_lossy(), |
| 204 | + flags_index |
| 205 | + ); |
| 206 | + test.with_file_name(base_file_name) |
| 207 | + } else { |
| 208 | + test.to_owned() |
| 209 | + }; |
| 210 | + |
| 211 | + // bundler uses a different main JS file, because its |
| 212 | + // reference_test.js just imports the reference_test_bg.js |
| 213 | + let main_js_file = match target { |
| 214 | + "bundler" => "reference_test_bg.js", |
| 215 | + _ => "reference_test.js", |
| 216 | + }; |
| 217 | + |
| 218 | + if !contents.contains("async") { |
| 219 | + let js = fs::read_to_string(out_dir.join(main_js_file))?; |
| 220 | + assert_same(&js, &test.with_extension("js"))?; |
| 221 | + let wat = sanitize_wasm(&out_dir.join("reference_test_bg.wasm"))?; |
| 222 | + assert_same(&wat, &test.with_extension("wat"))?; |
| 223 | + } |
| 224 | + let d_ts = fs::read_to_string(out_dir.join("reference_test.d.ts"))?; |
| 225 | + assert_same(&d_ts, &test.with_extension("d.ts"))?; |
163 | 226 | }
|
164 |
| - let d_ts = fs::read_to_string(td.path().join("reference_test.d.ts"))?; |
165 |
| - assert_same(&d_ts, &test.with_extension("d.ts"))?; |
166 | 227 |
|
167 | 228 | Ok(())
|
168 | 229 | }
|
|
0 commit comments