Skip to content

Commit 665785e

Browse files
add deno test mode, enabled by WASM_BINDGEN_USE_DENO=1
1 parent 77bf0e9 commit 665785e

File tree

3 files changed

+116
-33
lines changed

3 files changed

+116
-33
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::ffi::OsString;
2+
use std::fs;
3+
use std::path::Path;
4+
use std::process::Command;
5+
6+
use anyhow::{Context, Error};
7+
8+
use crate::node::{exec, SHARED_SETUP};
9+
10+
pub fn execute(
11+
module: &str,
12+
tmpdir: &Path,
13+
args: &[OsString],
14+
tests: &[String],
15+
) -> Result<(), Error> {
16+
let mut js_to_execute = format!(
17+
r#"import * as wasm from "./{0}.js";
18+
19+
{console_override}
20+
21+
// global.__wbg_test_invoke = f => f();
22+
23+
// Forward runtime arguments. These arguments are also arguments to the
24+
// `wasm-bindgen-test-runner` which forwards them to deno which we
25+
// forward to the test harness. this is basically only used for test
26+
// filters for now.
27+
cx.args(Deno.args.slice(1));
28+
29+
const ok = await cx.run(tests.map(n => wasm.__wasm[n]));
30+
if (!ok) Deno.exit(1);
31+
32+
const tests = [];
33+
"#,
34+
module,
35+
console_override = SHARED_SETUP,
36+
);
37+
38+
for test in tests {
39+
js_to_execute.push_str(&format!("tests.push('{}')\n", test));
40+
}
41+
42+
let js_path = tmpdir.join("run.js");
43+
fs::write(&js_path, js_to_execute).context("failed to write JS file")?;
44+
45+
/*
46+
// Augment `NODE_PATH` so things like `require("tests/my-custom.js")` work
47+
// and Rust code can import from custom JS shims. This is a bit of a hack
48+
// and should probably be removed at some point.
49+
let path = env::var("NODE_PATH").unwrap_or_default();
50+
let mut path = env::split_paths(&path).collect::<Vec<_>>();
51+
path.push(env::current_dir().unwrap());
52+
path.push(tmpdir.to_path_buf());
53+
let extra_node_args = env::var("NODE_ARGS")
54+
.unwrap_or_default()
55+
.split(",")
56+
.map(|s| s.to_string())
57+
.filter(|s| !s.is_empty())
58+
.collect::<Vec<_>>();
59+
exec(
60+
Command::new("node")
61+
.env("NODE_PATH", env::join_paths(&path).unwrap())
62+
.args(&extra_node_args)
63+
.arg(&js_path)
64+
.args(args),
65+
)*/
66+
exec(
67+
Command::new("deno")
68+
.arg("run")
69+
.arg("--allow-read")
70+
.arg(&js_path)
71+
.args(args),
72+
)
73+
}

crates/cli/src/bin/wasm-bindgen-test-runner/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use wasm_bindgen_cli_support::Bindgen;
2222
#[global_allocator]
2323
static ALLOC: std::alloc::System = std::alloc::System;
2424

25+
mod deno;
2526
mod headless;
2627
mod node;
2728
mod server;
@@ -30,6 +31,7 @@ mod shell;
3031
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
3132
enum TestMode {
3233
Node,
34+
Deno,
3335
Browser,
3436
}
3537

@@ -92,6 +94,7 @@ fn main() -> anyhow::Result<()> {
9294
let test_mode = match custom_section {
9395
Some(section) if section.data.contains(&0x01) => TestMode::Browser,
9496
Some(_) => bail!("invalid __wasm_bingen_test_unstable value"),
97+
None if std::env::var("WASM_BINDGEN_USE_DENO").is_ok() => TestMode::Deno,
9598
None => TestMode::Node,
9699
};
97100

@@ -145,6 +148,7 @@ integration test.\
145148
let mut b = Bindgen::new();
146149
match test_mode {
147150
TestMode::Node => b.nodejs(true)?,
151+
TestMode::Deno => b.deno(true)?,
148152
TestMode::Browser => b.web(true)?,
149153
};
150154

@@ -160,6 +164,7 @@ integration test.\
160164

161165
match test_mode {
162166
TestMode::Node => node::execute(&module, &tmpdir, &args, &tests)?,
167+
TestMode::Deno => deno::execute(&module, &tmpdir, &args, &tests)?,
163168
TestMode::Browser => {
164169
let srv = server::spawn(
165170
&if headless {

crates/cli/src/bin/wasm-bindgen-test-runner/node.rs

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,38 @@ use std::process::Command;
66

77
use anyhow::{Context, Error};
88

9+
// depends on the variable 'wasm' and initializes te WasmBindgenTestContext cx
10+
pub const SHARED_SETUP: &str = r#"
11+
const handlers = {};
12+
13+
const wrap = method => {
14+
const og = console[method];
15+
const on_method = `on_console_${method}`;
16+
console[method] = function (...args) {
17+
og.apply(this, args);
18+
if (handlers[on_method]) {
19+
handlers[on_method](args);
20+
}
21+
};
22+
};
23+
24+
// override `console.log` and `console.error` etc... before we import tests to
25+
// ensure they're bound correctly in wasm. This'll allow us to intercept
26+
// all these calls and capture the output of tests
27+
wrap("debug");
28+
wrap("log");
29+
wrap("info");
30+
wrap("warn");
31+
wrap("error");
32+
33+
cx = new wasm.WasmBindgenTestContext();
34+
handlers.on_console_debug = wasm.__wbgtest_console_debug;
35+
handlers.on_console_log = wasm.__wbgtest_console_log;
36+
handlers.on_console_info = wasm.__wbgtest_console_info;
37+
handlers.on_console_warn = wasm.__wbgtest_console_warn;
38+
handlers.on_console_error = wasm.__wbgtest_console_error;
39+
"#;
40+
941
pub fn execute(
1042
module: &str,
1143
tmpdir: &Path,
@@ -15,41 +47,13 @@ pub fn execute(
1547
let mut js_to_execute = format!(
1648
r#"
1749
const {{ exit }} = require('process');
50+
const wasm = require("./{0}");
1851
19-
const handlers = {{}};
20-
21-
const wrap = method => {{
22-
const og = console[method];
23-
const on_method = `on_console_${{method}}`;
24-
console[method] = function (...args) {{
25-
og.apply(this, args);
26-
if (handlers[on_method]) {{
27-
handlers[on_method](args);
28-
}}
29-
}};
30-
}};
31-
32-
// override `console.log` and `console.error` etc... before we import tests to
33-
// ensure they're bound correctly in wasm. This'll allow us to intercept
34-
// all these calls and capture the output of tests
35-
wrap("debug");
36-
wrap("log");
37-
wrap("info");
38-
wrap("warn");
39-
wrap("error");
52+
{console_override}
4053
4154
global.__wbg_test_invoke = f => f();
4255
4356
async function main(tests) {{
44-
const wasm = require("./{0}");
45-
46-
cx = new wasm.WasmBindgenTestContext();
47-
handlers.on_console_debug = wasm.__wbgtest_console_debug;
48-
handlers.on_console_log = wasm.__wbgtest_console_log;
49-
handlers.on_console_info = wasm.__wbgtest_console_info;
50-
handlers.on_console_warn = wasm.__wbgtest_console_warn;
51-
handlers.on_console_error = wasm.__wbgtest_console_error;
52-
5357
// Forward runtime arguments. These arguments are also arguments to the
5458
// `wasm-bindgen-test-runner` which forwards them to node which we
5559
// forward to the test harness. this is basically only used for test
@@ -63,7 +67,8 @@ pub fn execute(
6367
6468
const tests = [];
6569
"#,
66-
module
70+
module,
71+
console_override = SHARED_SETUP,
6772
);
6873

6974
// Note that we're collecting *JS objects* that represent the functions to
@@ -109,15 +114,15 @@ pub fn execute(
109114
}
110115

111116
#[cfg(unix)]
112-
fn exec(cmd: &mut Command) -> Result<(), Error> {
117+
pub fn exec(cmd: &mut Command) -> Result<(), Error> {
113118
use std::os::unix::prelude::*;
114119
Err(Error::from(cmd.exec())
115120
.context("failed to execute `node`")
116121
.into())
117122
}
118123

119124
#[cfg(windows)]
120-
fn exec(cmd: &mut Command) -> Result<(), Error> {
125+
pub fn exec(cmd: &mut Command) -> Result<(), Error> {
121126
use std::process;
122127
let status = cmd.status()?;
123128
process::exit(status.code().unwrap_or(3));

0 commit comments

Comments
 (0)