From c68cb30844090607a6439e2ceb5240bc1d7be64c Mon Sep 17 00:00:00 2001 From: Systemcluster Date: Sun, 4 Aug 2024 05:57:48 +0200 Subject: [PATCH] Add snapshot tests --- crates/cli-support/Cargo.toml | 7 + .../tests/snapshots/tests__bundler_js.snap | 8 + .../tests/snapshots/tests__deno_js.snap | 27 +++ .../tests/snapshots/tests__node_bg.snap | 14 ++ .../snapshots/tests__node_module_bg.snap | 8 + .../snapshots/tests__node_module_js.snap | 20 ++ .../tests/snapshots/tests__nodejs_js.snap | 14 ++ .../snapshots/tests__nodejs_module_js.snap | 8 + .../snapshots/tests__nodejs_module_start.snap | 20 ++ .../tests/snapshots/tests__nomodule_js.snap | 111 ++++++++++ .../tests/snapshots/tests__web_js.snap | 104 ++++++++++ crates/cli-support/tests/tests.rs | 193 ++++++++++++++++++ 12 files changed, 534 insertions(+) create mode 100644 crates/cli-support/tests/snapshots/tests__bundler_js.snap create mode 100644 crates/cli-support/tests/snapshots/tests__deno_js.snap create mode 100644 crates/cli-support/tests/snapshots/tests__node_bg.snap create mode 100644 crates/cli-support/tests/snapshots/tests__node_module_bg.snap create mode 100644 crates/cli-support/tests/snapshots/tests__node_module_js.snap create mode 100644 crates/cli-support/tests/snapshots/tests__nodejs_js.snap create mode 100644 crates/cli-support/tests/snapshots/tests__nodejs_module_js.snap create mode 100644 crates/cli-support/tests/snapshots/tests__nodejs_module_start.snap create mode 100644 crates/cli-support/tests/snapshots/tests__nomodule_js.snap create mode 100644 crates/cli-support/tests/snapshots/tests__web_js.snap create mode 100644 crates/cli-support/tests/tests.rs diff --git a/crates/cli-support/Cargo.toml b/crates/cli-support/Cargo.toml index 453f7613ccb2..ad34c587c94b 100644 --- a/crates/cli-support/Cargo.toml +++ b/crates/cli-support/Cargo.toml @@ -27,3 +27,10 @@ wasm-bindgen-threads-xform = { path = '../threads-xform', version = '=0.2.92' } wasm-bindgen-wasm-conventions = { path = '../wasm-conventions', version = '=0.2.92' } wasm-bindgen-wasm-interpreter = { path = "../wasm-interpreter", version = '=0.2.92' } unicode-ident = "1.0.5" + +[dev-dependencies] +insta = "1.39.0" + +[profile.dev.package] +insta.opt-level = 3 +similar.opt-level = 3 diff --git a/crates/cli-support/tests/snapshots/tests__bundler_js.snap b/crates/cli-support/tests/snapshots/tests__bundler_js.snap new file mode 100644 index 000000000000..96b4a94b4291 --- /dev/null +++ b/crates/cli-support/tests/snapshots/tests__bundler_js.snap @@ -0,0 +1,8 @@ +--- +source: crates/cli-support/tests/tests.rs +expression: output.js() +--- +let wasm; + export function __wbg_set_wasm(val) { + wasm = val; + } diff --git a/crates/cli-support/tests/snapshots/tests__deno_js.snap b/crates/cli-support/tests/snapshots/tests__deno_js.snap new file mode 100644 index 000000000000..f2778d15791a --- /dev/null +++ b/crates/cli-support/tests/snapshots/tests__deno_js.snap @@ -0,0 +1,27 @@ +--- +source: crates/cli-support/tests/tests.rs +expression: output.js() +--- +const imports = { + __wbindgen_placeholder__: { + }, + +}; + +const wasm_url = new URL('add_bg.wasm', import.meta.url); + let wasmCode = ''; + switch (wasm_url.protocol) { + case 'file:': + wasmCode = await Deno.readFile(wasm_url); + break + case 'https:': + case 'http:': + wasmCode = await (await fetch(wasm_url)).arrayBuffer(); + break + default: + throw new Error(`Unsupported protocol: ${wasm_url.protocol}`); + } + + const wasmInstance = (await WebAssembly.instantiate(wasmCode, imports)).instance; + const wasm = wasmInstance.exports; + export const __wasm = wasm; diff --git a/crates/cli-support/tests/snapshots/tests__node_bg.snap b/crates/cli-support/tests/snapshots/tests__node_bg.snap new file mode 100644 index 000000000000..cb502272eef4 --- /dev/null +++ b/crates/cli-support/tests/snapshots/tests__node_bg.snap @@ -0,0 +1,14 @@ +--- +source: crates/cli-support/tests/tests.rs +expression: output.js() +--- +let imports = {}; +let wasm; + +const path = require('path').join(__dirname, 'add_bg.wasm'); +const bytes = require('fs').readFileSync(path); + +const wasmModule = new WebAssembly.Module(bytes); +const wasmInstance = new WebAssembly.Instance(wasmModule, imports); +wasm = wasmInstance.exports; +module.exports.__wasm = wasm; diff --git a/crates/cli-support/tests/snapshots/tests__node_module_bg.snap b/crates/cli-support/tests/snapshots/tests__node_module_bg.snap new file mode 100644 index 000000000000..96b4a94b4291 --- /dev/null +++ b/crates/cli-support/tests/snapshots/tests__node_module_bg.snap @@ -0,0 +1,8 @@ +--- +source: crates/cli-support/tests/tests.rs +expression: output.js() +--- +let wasm; + export function __wbg_set_wasm(val) { + wasm = val; + } diff --git a/crates/cli-support/tests/snapshots/tests__node_module_js.snap b/crates/cli-support/tests/snapshots/tests__node_module_js.snap new file mode 100644 index 000000000000..a445f646800b --- /dev/null +++ b/crates/cli-support/tests/snapshots/tests__node_module_js.snap @@ -0,0 +1,20 @@ +--- +source: crates/cli-support/tests/tests.rs +expression: output.start().unwrap() +--- +let imports = {}; + +import * as path from 'node:path'; +import * as fs from 'node:fs'; +import * as process from 'node:process'; + +let file = path.dirname(new URL(import.meta.url).pathname); +if (process.platform === 'win32') { + file = file.substring(1); +} +const bytes = fs.readFileSync(path.join(file, 'add_bg.wasm')); + +const wasmModule = new WebAssembly.Module(bytes); +const wasmInstance = new WebAssembly.Instance(wasmModule, imports); +const wasm = wasmInstance.exports; +export const __wasm = wasm; diff --git a/crates/cli-support/tests/snapshots/tests__nodejs_js.snap b/crates/cli-support/tests/snapshots/tests__nodejs_js.snap new file mode 100644 index 000000000000..cb502272eef4 --- /dev/null +++ b/crates/cli-support/tests/snapshots/tests__nodejs_js.snap @@ -0,0 +1,14 @@ +--- +source: crates/cli-support/tests/tests.rs +expression: output.js() +--- +let imports = {}; +let wasm; + +const path = require('path').join(__dirname, 'add_bg.wasm'); +const bytes = require('fs').readFileSync(path); + +const wasmModule = new WebAssembly.Module(bytes); +const wasmInstance = new WebAssembly.Instance(wasmModule, imports); +wasm = wasmInstance.exports; +module.exports.__wasm = wasm; diff --git a/crates/cli-support/tests/snapshots/tests__nodejs_module_js.snap b/crates/cli-support/tests/snapshots/tests__nodejs_module_js.snap new file mode 100644 index 000000000000..96b4a94b4291 --- /dev/null +++ b/crates/cli-support/tests/snapshots/tests__nodejs_module_js.snap @@ -0,0 +1,8 @@ +--- +source: crates/cli-support/tests/tests.rs +expression: output.js() +--- +let wasm; + export function __wbg_set_wasm(val) { + wasm = val; + } diff --git a/crates/cli-support/tests/snapshots/tests__nodejs_module_start.snap b/crates/cli-support/tests/snapshots/tests__nodejs_module_start.snap new file mode 100644 index 000000000000..a445f646800b --- /dev/null +++ b/crates/cli-support/tests/snapshots/tests__nodejs_module_start.snap @@ -0,0 +1,20 @@ +--- +source: crates/cli-support/tests/tests.rs +expression: output.start().unwrap() +--- +let imports = {}; + +import * as path from 'node:path'; +import * as fs from 'node:fs'; +import * as process from 'node:process'; + +let file = path.dirname(new URL(import.meta.url).pathname); +if (process.platform === 'win32') { + file = file.substring(1); +} +const bytes = fs.readFileSync(path.join(file, 'add_bg.wasm')); + +const wasmModule = new WebAssembly.Module(bytes); +const wasmInstance = new WebAssembly.Instance(wasmModule, imports); +const wasm = wasmInstance.exports; +export const __wasm = wasm; diff --git a/crates/cli-support/tests/snapshots/tests__nomodule_js.snap b/crates/cli-support/tests/snapshots/tests__nomodule_js.snap new file mode 100644 index 000000000000..19f66c4c3c95 --- /dev/null +++ b/crates/cli-support/tests/snapshots/tests__nomodule_js.snap @@ -0,0 +1,111 @@ +--- +source: crates/cli-support/tests/tests.rs +expression: output.js() +--- +let wasm_bindgen; +(function() { +const __exports = {}; +let script_src; +if (typeof document !== 'undefined' && document.currentScript !== null) { + script_src = new URL(document.currentScript.src, location.href).toString(); + } +let wasm = undefined; +async function __wbg_load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } + } + } + + function __wbg_get_imports() { + const imports = {}; + imports.wbg = {}; + + return imports; + } + + function __wbg_init_memory(imports, memory) { + + } + + function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + + + + return wasm; + } + + function initSync(module) { + if (wasm !== undefined) return wasm; + + + if (typeof module !== 'undefined' && Object.getPrototypeOf(module) === Object.prototype) + ({module} = module) + else + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') + + const imports = __wbg_get_imports(); + + __wbg_init_memory(imports); + + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + + const instance = new WebAssembly.Instance(module, imports); + + return __wbg_finalize_init(instance, module); + } + + async function __wbg_init(module_or_path) { + if (wasm !== undefined) return wasm; + + + if (typeof module_or_path !== 'undefined' && Object.getPrototypeOf(module_or_path) === Object.prototype) + ({module_or_path} = module_or_path) + else + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + + + const imports = __wbg_get_imports(); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + __wbg_init_memory(imports); + + const { instance, module } = await __wbg_load(await module_or_path, imports); + + return __wbg_finalize_init(instance, module); + } + +wasm_bindgen = Object.assign(__wbg_init, { initSync }, __exports); + +})(); diff --git a/crates/cli-support/tests/snapshots/tests__web_js.snap b/crates/cli-support/tests/snapshots/tests__web_js.snap new file mode 100644 index 000000000000..7875de28dca3 --- /dev/null +++ b/crates/cli-support/tests/snapshots/tests__web_js.snap @@ -0,0 +1,104 @@ +--- +source: crates/cli-support/tests/tests.rs +expression: output.js() +--- +let wasm; + +async function __wbg_load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } + } + } + + function __wbg_get_imports() { + const imports = {}; + imports.wbg = {}; + + return imports; + } + + function __wbg_init_memory(imports, memory) { + + } + + function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + + + + return wasm; + } + + function initSync(module) { + if (wasm !== undefined) return wasm; + + + if (typeof module !== 'undefined' && Object.getPrototypeOf(module) === Object.prototype) + ({module} = module) + else + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') + + const imports = __wbg_get_imports(); + + __wbg_init_memory(imports); + + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + + const instance = new WebAssembly.Instance(module, imports); + + return __wbg_finalize_init(instance, module); + } + + async function __wbg_init(module_or_path) { + if (wasm !== undefined) return wasm; + + + if (typeof module_or_path !== 'undefined' && Object.getPrototypeOf(module_or_path) === Object.prototype) + ({module_or_path} = module_or_path) + else + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + + + const imports = __wbg_get_imports(); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + __wbg_init_memory(imports); + + const { instance, module } = await __wbg_load(await module_or_path, imports); + + return __wbg_finalize_init(instance, module); + } + +export { initSync }; +export default __wbg_init; diff --git a/crates/cli-support/tests/tests.rs b/crates/cli-support/tests/tests.rs new file mode 100644 index 000000000000..95767b4e02f8 --- /dev/null +++ b/crates/cli-support/tests/tests.rs @@ -0,0 +1,193 @@ +use wasm_bindgen_cli_support::Bindgen; + +#[test] +fn test_bundler_start() { + let mut bindgen = Bindgen::new(); + bindgen.bundler(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + assert!(output.start().is_none()); +} + +#[test] +fn test_bundler_js() { + let mut bindgen = Bindgen::new(); + bindgen.bundler(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + insta::assert_snapshot!(output.js()); +} + +#[test] +fn test_deno_start() { + let mut bindgen = Bindgen::new(); + bindgen.deno(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + assert!(output.start().is_none()); +} + +#[test] +fn test_deno_js() { + let mut bindgen = Bindgen::new(); + bindgen.deno(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + insta::assert_snapshot!(output.js()); +} + +#[test] +fn test_nodejs_module_start() { + let mut bindgen = Bindgen::new(); + bindgen.nodejs_module(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + insta::assert_snapshot!(output.start().unwrap()); +} + +#[test] +fn test_nodejs_module_js() { + let mut bindgen = Bindgen::new(); + bindgen.nodejs_module(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + insta::assert_snapshot!(output.js()); +} + +#[test] +fn test_nodejs_start() { + let mut bindgen = Bindgen::new(); + bindgen.nodejs(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + assert!(output.start().is_none()); +} + +#[test] +fn test_nodejs_js() { + let mut bindgen = Bindgen::new(); + bindgen.nodejs(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + insta::assert_snapshot!(output.js()); +} + +#[test] +fn test_web_start() { + let mut bindgen = Bindgen::new(); + bindgen.web(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + assert!(output.start().is_none()); +} + +#[test] +fn test_web_js() { + let mut bindgen = Bindgen::new(); + bindgen.web(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + insta::assert_snapshot!(output.js()); +} + +#[test] +fn test_nomodule_start() { + let mut bindgen = Bindgen::new(); + bindgen.no_modules(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + assert!(output.start().is_none()); +} + +#[test] +fn test_nomodule_js() { + let mut bindgen = Bindgen::new(); + bindgen.no_modules(true).unwrap(); + bindgen.input_path( + [ + std::env!("CARGO_MANIFEST_DIR"), + "/../../examples/wasm-in-wasm/src/add.wasm", + ] + .concat(), + ); + + let output = bindgen.generate_output().unwrap(); + insta::assert_snapshot!(output.js()); +}