| 1 | use std::path::Path; |
| 2 | |
| 3 | #[test] |
| 4 | fn no_host_imports() { |
| 5 | let wasm_path = "target/wasm32-unknown-unknown/release/docs_core.wasm"; |
| 6 | |
| 7 | // DOCS_REQUIRE_WASM=1 is set by the CI gate, which always builds the |
| 8 | // wasm artifact before running this test — so under the gate a missing |
| 9 | // artifact is a real failure, not something to shrug off. Left unset, |
| 10 | // a bare `cargo test` on a fresh clone (no wasm build yet) still skips, |
| 11 | // so local iteration on the native tests isn't forced to build wasm |
| 12 | // first. |
| 13 | let require_wasm = std::env::var("DOCS_REQUIRE_WASM").as_deref() == Ok("1"); |
| 14 | |
| 15 | if !Path::new(wasm_path).exists() { |
| 16 | if require_wasm { |
| 17 | panic!( |
| 18 | "FAILED (DOCS_REQUIRE_WASM=1): wasm artifact not found at {}. \ |
| 19 | The gate must build it before running this test.", |
| 20 | wasm_path |
| 21 | ); |
| 22 | } |
| 23 | eprintln!( |
| 24 | "SKIPPED (DOCS_REQUIRE_WASM unset): wasm artifact not found at {}. \ |
| 25 | Run ./scripts/build-wasm.sh first.", |
| 26 | wasm_path |
| 27 | ); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | let wasm_bytes = std::fs::read(wasm_path).expect("Failed to read wasm file"); |
| 32 | let parser = wasmparser::Parser::new(0); |
| 33 | |
| 34 | let mut imports = Vec::new(); |
| 35 | |
| 36 | for payload in parser.parse_all(&wasm_bytes) { |
| 37 | let payload = payload.expect("Failed to parse wasm module"); |
| 38 | |
| 39 | if let wasmparser::Payload::ImportSection(reader) = payload { |
| 40 | let import_reader = reader.into_imports(); |
| 41 | for import_result in import_reader { |
| 42 | let import = import_result.expect("Failed to parse import"); |
| 43 | imports.push(format!("{}.{}", import.module, import.name)); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | if !imports.is_empty() { |
| 49 | panic!( |
| 50 | "WASM module has host imports (breaks Go/wazero compatibility):\n {}", |
| 51 | imports.join("\n ") |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | eprintln!("PASSED (checked {} bytes, 0 imports)", wasm_bytes.len()); |
| 56 | } |
| 57 | |