Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement coverage output #3782

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion crates/cli/src/bin/wasm-bindgen-test-runner/deno.rs
@@ -1,7 +1,7 @@
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::process::Command;
use std::{ffi::OsString, path::PathBuf};

use anyhow::{Context, Error};

Expand All @@ -12,12 +12,37 @@ pub fn execute(
tmpdir: &Path,
args: &[OsString],
tests: &[String],
coverage: Option<PathBuf>,
) -> Result<(), Error> {
let (cov_fn, cov_dump) = if let Some(profraw_path) = coverage {
(
format!(
r#"
async function dumpCoverage() {{
const fs = require('node:fs');
const data = wasm.__wbgtest_cov_dump();
fs.writeFile("{}", data, err => {{
if (err) {{
console.error(err);
}}
}})
}}
"#,
profraw_path.display()
),
"await dumpCoverage();",
)
} else {
(String::new(), "")
};

let mut js_to_execute = format!(
r#"import * as wasm from "./{0}.js";

{console_override}

{cov_fn}

// global.__wbg_test_invoke = f => f();

// Forward runtime arguments. These arguments are also arguments to the
Expand All @@ -27,6 +52,7 @@ pub fn execute(
cx.args(Deno.args.slice(1));

const ok = await cx.run(tests.map(n => wasm.__wasm[n]));
{cov_dump}
if (!ok) Deno.exit(1);

const tests = [];
Expand Down
38 changes: 36 additions & 2 deletions crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Expand Up @@ -15,6 +15,7 @@ use anyhow::{anyhow, bail, Context};
use log::error;
use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::thread;
use wasm_bindgen_cli_support::Bindgen;
Expand Down Expand Up @@ -187,6 +188,8 @@ fn main() -> anyhow::Result<()> {
b.split_linked_modules(true);
}

let coverage = coverage_args(&tmpdir);

b.debug(debug)
.input_module(module, wasm)
.keep_debug(false)
Expand All @@ -198,8 +201,8 @@ fn main() -> anyhow::Result<()> {
let args: Vec<_> = args.collect();

match test_mode {
TestMode::Node => node::execute(module, &tmpdir, &args, &tests)?,
TestMode::Deno => deno::execute(module, &tmpdir, &args, &tests)?,
TestMode::Node => node::execute(module, &tmpdir, &args, &tests, coverage)?,
TestMode::Deno => deno::execute(module, &tmpdir, &args, &tests, coverage)?,
Comment on lines +204 to +205
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't really review Node or Deno stuff, so I will be pulling in another maintainer when we get there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the only thing missing.

TestMode::Browser { no_modules } | TestMode::Worker { no_modules } => {
let srv = server::spawn(
&if headless {
Expand All @@ -216,6 +219,7 @@ fn main() -> anyhow::Result<()> {
&tests,
no_modules,
matches!(test_mode, TestMode::Worker { no_modules: _ }),
coverage,
)
.context("failed to spawn server")?;
let addr = srv.server_addr();
Expand All @@ -242,3 +246,33 @@ fn main() -> anyhow::Result<()> {
}
Ok(())
}

fn coverage_args(tmpdir: &Path) -> Option<PathBuf> {
fn generated(tmpdir: &Path, prefix: &str) -> String {
let res = format!(
"{prefix}{}.profraw",
tmpdir.file_name().and_then(|s| s.to_str()).unwrap()
);
res
}

// Profraw path is ignored if coverage isn't enabled
env::var_os("WASM_BINDGEN_UNSTABLE_TEST_COVERAGE")?;
// TODO coverage link to wasm-bindgen book documenting correct usage

let prefix = env::var_os("WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_PREFIX")
.map(|s| s.to_str().unwrap().to_string())
.unwrap_or_default();

match env::var_os("WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_OUT") {
Some(s) => {
let mut buf = PathBuf::from(s);
if buf.is_dir() {
buf.push(generated(tmpdir, &prefix));
}
buf
}
None => PathBuf::from(generated(tmpdir, &prefix)),
}
.into()
}
28 changes: 27 additions & 1 deletion crates/cli/src/bin/wasm-bindgen-test-runner/node.rs
@@ -1,8 +1,8 @@
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::process::Command;
use std::{env, path::PathBuf};

use anyhow::{Context, Error};

Expand Down Expand Up @@ -43,7 +43,30 @@ pub fn execute(
tmpdir: &Path,
args: &[OsString],
tests: &[String],
coverage: Option<PathBuf>,
) -> Result<(), Error> {
let (cov_fn, cov_dump) = if let Some(profraw_path) = coverage {
(
format!(
r#"
async function dumpCoverage() {{
const fs = require('node:fs');
const data = wasm.__wbgtest_cov_dump();
fs.writeFile("{}", data, err => {{
if (err) {{
console.error(err);
}}
}})
}}
"#,
profraw_path.display()
),
"await dumpCoverage();",
)
} else {
(String::new(), "")
};

let mut js_to_execute = format!(
r#"
const {{ exit }} = require('process');
Expand All @@ -53,6 +76,8 @@ pub fn execute(

global.__wbg_test_invoke = f => f();

{cov_fn}

async function main(tests) {{
// Forward runtime arguments. These arguments are also arguments to the
// `wasm-bindgen-test-runner` which forwards them to node which we
Expand All @@ -61,6 +86,7 @@ pub fn execute(
cx.args(process.argv.slice(2));

const ok = await cx.run(tests.map(n => wasm.__wasm[n]));
{cov_dump}
if (!ok)
exit(1);
}}
Expand Down
59 changes: 58 additions & 1 deletion crates/cli/src/bin/wasm-bindgen-test-runner/server.rs
@@ -1,8 +1,9 @@
use std::borrow::Cow;
use std::ffi::OsString;
use std::fs;
use std::io::{Read, Write};
use std::net::SocketAddr;
use std::path::Path;
use std::{ffi::OsString, path::PathBuf};

use anyhow::{anyhow, Context, Error};
use rouille::{Request, Response, Server};
Expand All @@ -16,9 +17,30 @@ pub fn spawn(
tests: &[String],
no_module: bool,
worker: bool,
coverage: Option<PathBuf>,
) -> Result<Server<impl Fn(&Request) -> Response + Send + Sync>, Error> {
let mut js_to_execute = String::new();

let (cov_import, cov_dump) = if coverage.is_some() {
(
if no_module {
"let __wbgtest_cov_dump = wasm_bindgen.__wbgtest_cov_dump;"
} else {
"__wbgtest_cov_dump,"
},
r#"
// Dump the coverage data collected during the tests
const coverage = __wbgtest_cov_dump();
await fetch("/__coverage/dump", {
method: "POST",
body: coverage
});
"#,
)
} else {
("", "")
};

let wbg_import_script = if no_module {
String::from(
r#"
Expand All @@ -28,6 +50,7 @@ pub fn spawn(
let __wbgtest_console_info = wasm_bindgen.__wbgtest_console_info;
let __wbgtest_console_warn = wasm_bindgen.__wbgtest_console_warn;
let __wbgtest_console_error = wasm_bindgen.__wbgtest_console_error;
{cov_import}
let init = wasm_bindgen;
"#,
)
Expand All @@ -41,6 +64,7 @@ pub fn spawn(
__wbgtest_console_info,
__wbgtest_console_warn,
__wbgtest_console_error,
{cov_import}
default as init,
}} from './{}';
"#,
Expand Down Expand Up @@ -95,6 +119,7 @@ pub fn spawn(

cx.args({1:?});
await cx.run(tests.map(s => wasm[s]));
{cov_dump}
}}

onmessage = function(e) {{
Expand Down Expand Up @@ -175,6 +200,7 @@ pub fn spawn(
cx.args({1:?});

await cx.run(test.map(s => wasm[s]));
{cov_dump}
}}

const tests = [];
Expand Down Expand Up @@ -218,6 +244,22 @@ pub fn spawn(
)
};
return set_isolate_origin_headers(Response::from_data("text/html", s));
} else if request.url() == "/__coverage/dump" {
fn internal_err(s: &str) -> Response {
log::error!("{s}");
let mut ret = Response::text(s);
ret.status_code = 500;
ret
}
let Some(profraw_path) = &coverage else {
aDogCalledSpot marked this conversation as resolved.
Show resolved Hide resolved
return internal_err(
"Received coverage dump request but server wasn't set up to accept coverage",
);
};
return match handle_coverage_dump(profraw_path, request) {
Ok(()) => Response::empty_204(),
Err(e) => internal_err(&format!("Failed to dump coverage: {e}")),
};
}

// Otherwise we need to find the asset here. It may either be in our
Expand Down Expand Up @@ -266,6 +308,21 @@ pub fn spawn(
}
}

fn handle_coverage_dump(profraw_path: &Path, request: &Request) -> anyhow::Result<()> {
// This is run after all tests are done and dumps the data received in the request
// into a single profraw file
let mut profraw = std::fs::File::create(profraw_path)?;
let mut data = Vec::new();
if let Some(mut r_data) = request.data() {
r_data.read_to_end(&mut data)?;
}
// Warnings about empty data should have already been handled by
// the client

profraw.write_all(&data)?;
Ok(())
}

/*
* Set the Cross-Origin-Opener-Policy and Cross-Origin_Embedder-Policy headers
* on the Server response to enable worker context sharing, as described in:
Expand Down
7 changes: 7 additions & 0 deletions crates/test/Cargo.toml
Expand Up @@ -8,6 +8,10 @@ repository = "https://github.com/rustwasm/wasm-bindgen"
edition = "2018"
rust-version = "1.57"

[features]
default = []
unstable-coverage = ["minicov"]

[dependencies]
console_error_panic_hook = '0.1'
js-sys = { path = '../js-sys', version = '0.3.67' }
Expand All @@ -17,5 +21,8 @@ wasm-bindgen-futures = { path = '../futures', version = '0.4.40' }
wasm-bindgen-test-macro = { path = '../test-macro', version = '=0.3.40' }
gg-alloc = { version = "1.0", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
minicov = { version = "0.3", optional = true }

[lib]
test = false
29 changes: 29 additions & 0 deletions crates/test/src/coverage.rs
@@ -0,0 +1,29 @@
use wasm_bindgen::prelude::wasm_bindgen;

#[cfg(feature = "unstable-coverage")]
#[wasm_bindgen]
pub fn __wbgtest_cov_dump() -> Vec<u8> {
let mut coverage = Vec::new();
unsafe {
minicov::capture_coverage(&mut coverage).unwrap();
}
if coverage.is_empty() {
console_error!(
"Empty coverage data received. Make sure you compile the tests with
RUSTFLAGS=\"-Cinstrument-coverage -Zno-profile-runtime --emit=llvm-ir\"",
);
}
coverage
}

/// Called when setting WASM_BINDGEN_UNSTABLE_TEST_COVERAGE but coverage feature is disabled.
/// Currently not being used because of issues in the interpreter regarding profiling
/// information which cause an error before we get here.
#[cfg(not(feature = "unstable-coverage"))]
#[wasm_bindgen]
pub fn __wbgtest_cov_dump() -> Vec<u8> {
console_error!(
"Coverage was supposed to be dumped, but the \"coverage\" feature is disabled in wasm-bindgen-test",
);
Vec::new()
}
15 changes: 15 additions & 0 deletions crates/test/src/lib.rs
Expand Up @@ -21,6 +21,15 @@ macro_rules! console_log {
)
}

/// Helper macro which acts like `println!` only routes to `console.error`
/// instead.
#[macro_export]
macro_rules! console_error {
($($arg:tt)*) => (
$crate::__rt::log_error(&format_args!($($arg)*))
)
}

/// A macro used to configured how this test is executed by the
/// `wasm-bindgen-test-runner` harness.
///
Expand Down Expand Up @@ -59,3 +68,9 @@ macro_rules! wasm_bindgen_test_configure {

#[path = "rt/mod.rs"]
pub mod __rt;

// Make this only available to wasm32 so that we don't
// import minicov on other archs.
// That way you can use normal cargo test without minicov
aDogCalledSpot marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(target_arch = "wasm32")]
mod coverage;