From 41eefa7425f3f1e9df92bfafa18aea802443a26d Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 15 Jan 2019 14:05:22 -0800 Subject: [PATCH] wasm-bindgen-test: Capture more console logging methods' output Fixes #1183 --- .../index-headless.html | 59 +++++++++---------- .../bin/wasm-bindgen-test-runner/index.html | 34 +++++------ .../src/bin/wasm-bindgen-test-runner/node.rs | 42 +++++++------ .../bin/wasm-bindgen-test-runner/server.rs | 12 +++- crates/test/src/rt/mod.rs | 50 ++++++++++++---- 5 files changed, 115 insertions(+), 82 deletions(-) diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/index-headless.html b/crates/cli/src/bin/wasm-bindgen-test-runner/index-headless.html index 140dd2d9a..11c4d5615 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/index-headless.html +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/index-headless.html @@ -3,41 +3,38 @@ -
Loading scripts...
-

-    

+    
Loading scripts...
+

+    

+    

+    

+    

     
     
   
diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/index.html b/crates/cli/src/bin/wasm-bindgen-test-runner/index.html
index f45987e71..1fc86aafb 100644
--- a/crates/cli/src/bin/wasm-bindgen-test-runner/index.html
+++ b/crates/cli/src/bin/wasm-bindgen-test-runner/index.html
@@ -5,26 +5,24 @@
   
     
Loading scripts...
diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs index c00d81ff2..a74e0472e 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs @@ -16,26 +16,27 @@ pub fn execute( r#" const {{ exit }} = require('process'); - let on_console_log = null; - let on_console_error = null; + const handlers = {{}}; - // override `console.log` and `console.error` before we import tests to + const wrap = method => {{ + const og = console[method]; + const on_method = `on_console_${{method}}`; + console[method] = function (...args) {{ + if (handlers[on_method]) {{ + handlers[on_method](args); + }} + og.apply(this, args); + }}; + }}; + + // override `console.log` and `console.error` etc... before we import tests to // ensure they're bound correctly in wasm. This'll allow us to intercept // all these calls and capture the output of tests - const prev_log = console.log; - console.log = function(...args) {{ - if (on_console_log) {{ - on_console_log(args); - }} - prev_log.apply(null, args); - }}; - const prev_error = console.error; - console.error = function(...args) {{ - if (on_console_error) {{ - on_console_error(args); - }} - prev_error.apply(null, args); - }}; + wrap("debug"); + wrap("log"); + wrap("info"); + wrap("warn"); + wrap("error"); global.__wbg_test_invoke = f => f(); @@ -44,8 +45,11 @@ pub fn execute( const wasm = require("./{0}_bg"); cx = new support.Context(); - on_console_log = support.__wbgtest_console_log; - on_console_error = support.__wbgtest_console_error; + handlers.on_console_debug = support.__wbgtest_console_debug; + handlers.on_console_log = support.__wbgtest_console_log; + handlers.on_console_info = support.__wbgtest_console_info; + handlers.on_console_warn = support.__wbgtest_console_warn; + handlers.on_console_error = support.__wbgtest_console_error; // Forward runtime arguments. These arguments are also arguments to the // `wasm-bindgen-test-runner` which forwards them to node which we diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/server.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/server.rs index f90978d3e..29942b2cf 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/server.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/server.rs @@ -17,7 +17,14 @@ pub fn spawn( ) -> Result Response + Send + Sync>, Error> { let mut js_to_execute = format!( r#" - import {{ Context, __wbgtest_console_log, __wbgtest_console_error }} from './{0}'; + import {{ + Context, + __wbgtest_console_debug, + __wbgtest_console_log, + __wbgtest_console_info, + __wbgtest_console_warn, + __wbgtest_console_error + }} from './{0}'; import * as wasm from './{0}_bg'; // Now that we've gotten to the point where JS is executing, update our @@ -31,7 +38,10 @@ pub fn spawn( await wasm.booted; const cx = new Context(); + window.on_console_debug = __wbgtest_console_debug; window.on_console_log = __wbgtest_console_log; + window.on_console_info = __wbgtest_console_info; + window.on_console_warn = __wbgtest_console_warn; window.on_console_error = __wbgtest_console_error; // Forward runtime arguments. These arguments are also arguments to the diff --git a/crates/test/src/rt/mod.rs b/crates/test/src/rt/mod.rs index e38af1651..45b3801ff 100644 --- a/crates/test/src/rt/mod.rs +++ b/crates/test/src/rt/mod.rs @@ -164,7 +164,10 @@ struct Test { /// Captured output of each test. #[derive(Default)] struct Output { + debug: String, log: String, + info: String, + warn: String, error: String, } @@ -309,9 +312,25 @@ pub fn __wbgtest_console_log(args: &Array) { record(args, |output| &mut output.log) } -/// Handler for `console.error` invocations. -/// -/// Works the same as `console_log` above. +/// Handler for `console.debug` invocations. See above. +#[wasm_bindgen] +pub fn __wbgtest_console_debug(args: &Array) { + record(args, |output| &mut output.debug) +} + +/// Handler for `console.info` invocations. See above. +#[wasm_bindgen] +pub fn __wbgtest_console_info(args: &Array) { + record(args, |output| &mut output.info) +} + +/// Handler for `console.warn` invocations. See above. +#[wasm_bindgen] +pub fn __wbgtest_console_warn(args: &Array) { + record(args, |output| &mut output.warn) +} + +/// Handler for `console.error` invocations. See above. #[wasm_bindgen] pub fn __wbgtest_console_error(args: &Array) { record(args, |output| &mut output.error) @@ -477,19 +496,24 @@ impl State { )); } + fn accumulate_console_output(&self, logs: &mut String, which: &str, output: &str) { + if output.is_empty() { + return; + } + logs.push_str(which); + logs.push_str(" output:\n"); + logs.push_str(&tab(output)); + logs.push('\n'); + } + fn print_failure(&self, test: &Test, error: &JsValue) { let mut logs = String::new(); let output = test.output.borrow(); - if output.log.len() > 0 { - logs.push_str("log output:\n"); - logs.push_str(&tab(&output.log)); - logs.push_str("\n"); - } - if output.error.len() > 0 { - logs.push_str("error output:\n"); - logs.push_str(&tab(&output.error)); - logs.push_str("\n"); - } + self.accumulate_console_output(&mut logs, "debug", &output.debug); + self.accumulate_console_output(&mut logs, "log", &output.log); + self.accumulate_console_output(&mut logs, "info", &output.info); + self.accumulate_console_output(&mut logs, "warn", &output.warn); + self.accumulate_console_output(&mut logs, "error", &output.error); logs.push_str("JS exception that was thrown:\n"); let error_string = self.formatter.stringify_error(error); logs.push_str(&tab(&error_string));