@@ -6,6 +6,38 @@ use std::process::Command;
6
6
7
7
use anyhow:: { Context , Error } ;
8
8
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
+
9
41
pub fn execute (
10
42
module : & str ,
11
43
tmpdir : & Path ,
@@ -15,41 +47,13 @@ pub fn execute(
15
47
let mut js_to_execute = format ! (
16
48
r#"
17
49
const {{ exit }} = require('process');
50
+ const wasm = require("./{0}");
18
51
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}
40
53
41
54
global.__wbg_test_invoke = f => f();
42
55
43
56
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
-
53
57
// Forward runtime arguments. These arguments are also arguments to the
54
58
// `wasm-bindgen-test-runner` which forwards them to node which we
55
59
// forward to the test harness. this is basically only used for test
@@ -63,7 +67,8 @@ pub fn execute(
63
67
64
68
const tests = [];
65
69
"# ,
66
- module
70
+ module,
71
+ console_override = SHARED_SETUP ,
67
72
) ;
68
73
69
74
// Note that we're collecting *JS objects* that represent the functions to
@@ -109,15 +114,15 @@ pub fn execute(
109
114
}
110
115
111
116
#[ cfg( unix) ]
112
- fn exec ( cmd : & mut Command ) -> Result < ( ) , Error > {
117
+ pub fn exec ( cmd : & mut Command ) -> Result < ( ) , Error > {
113
118
use std:: os:: unix:: prelude:: * ;
114
119
Err ( Error :: from ( cmd. exec ( ) )
115
120
. context ( "failed to execute `node`" )
116
121
. into ( ) )
117
122
}
118
123
119
124
#[ cfg( windows) ]
120
- fn exec ( cmd : & mut Command ) -> Result < ( ) , Error > {
125
+ pub fn exec ( cmd : & mut Command ) -> Result < ( ) , Error > {
121
126
use std:: process;
122
127
let status = cmd. status ( ) ?;
123
128
process:: exit ( status. code ( ) . unwrap_or ( 3 ) ) ;
0 commit comments