Skip to content

Commit

Permalink
Merge pull request rustwasm#1184 from fitzgen/issue-1183
Browse files Browse the repository at this point in the history
wasm-bindgen-test: Capture more console logging methods' output
  • Loading branch information
alexcrichton authored Jan 15, 2019
2 parents d5b6c52 + 41eefa7 commit 0d3f9eb
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 82 deletions.
59 changes: 28 additions & 31 deletions crates/cli/src/bin/wasm-bindgen-test-runner/index-headless.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,38 @@
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
</head>
<body>
<pre id='output'>Loading scripts...</pre>
<pre id='console_log'></pre>
<pre id='console_error'></pre>
<pre id="output">Loading scripts...</pre>
<pre id="console_debug"></pre>
<pre id="console_log"></pre>
<pre id="console_info"></pre>
<pre id="console_warn"></pre>
<pre id="console_error"></pre>
<script>
const orig_console_log = function(...args) {
const logs = document.getElementById('console_log');
for (let msg of args) {
logs.innerHTML += `${msg}\n`;
}
};
const orig = id => (...args) => {
const logs = document.getElementById(id);
for (let msg of args) {
logs.innerHTML += `${msg}\n`;
}
};

const orig_console_error = function(...args) {
const logs = document.getElementById('console_error');
for (let msg of args) {
logs.innerHTML += `${msg}\n`;
}
};
const wrap = method => {
const og = orig(`console_${method}`);
const on_method = `on_console_${method}`;
console[method] = function (...args) {
if (window[on_method]) {
window[on_method](args);
}
og.apply(this, args);
};
};

console.log = function(...args) {
if (window.on_console_log) {
window.on_console_log(args);
}
wrap("debug");
wrap("log");
wrap("info");
wrap("warn");
wrap("error");

orig_console_log.apply(this, args);
};

console.error = function(...args) {
if (window.on_console_error) {
window.on_console_error(args);
}

orig_console_error.apply(this, args);
};

window.__wbg_test_invoke = f => f();
window.__wbg_test_invoke = f => f();
</script>
<script src='run.js' type=module></script>
</body>
Expand Down
34 changes: 16 additions & 18 deletions crates/cli/src/bin/wasm-bindgen-test-runner/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@
<body>
<pre id='output'>Loading scripts...</pre>
<script>
const orig_console_log = console.log;
const orig_console_error = console.error;
const wrap = method => {
const og = console[method];
const on_method = `on_console_${method}`;
console[method] = function (...args) {
if (window[on_method]) {
window[on_method](args);
}
og.apply(this, args);
};
};

console.log = function(...args) {
if (window.on_console_log) {
window.on_console_log(args);
}
wrap("debug");
wrap("log");
wrap("info");
wrap("warn");
wrap("error");

orig_console_log.apply(this, args);
};

console.error = function(...args) {
if (window.on_console_error) {
window.on_console_error(args);
}

orig_console_error.apply(this, args);
};

window.__wbg_test_invoke = f => f();
window.__wbg_test_invoke = f => f();
</script>
<script src='run.js' type=module></script>
</body>
Expand Down
42 changes: 23 additions & 19 deletions crates/cli/src/bin/wasm-bindgen-test-runner/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion crates/cli/src/bin/wasm-bindgen-test-runner/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ pub fn spawn(
) -> Result<Server<impl Fn(&Request) -> 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
Expand All @@ -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
Expand Down
50 changes: 37 additions & 13 deletions crates/test/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 0d3f9eb

Please sign in to comment.