Skip to content

Commit

Permalink
Address requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aDogCalledSpot committed Jan 16, 2024
1 parent 2fb8787 commit 49d083b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 39 deletions.
3 changes: 1 addition & 2 deletions crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Expand Up @@ -256,8 +256,7 @@ fn coverage_args(tmpdir: &Path) -> Option<PathBuf> {
}

// Profraw path is ignored if coverage isn't enabled
env::var_os("WASM_BINDGEN_TEST_COVERAGE")?;
log::warn!("Coverage support is still considered highly experimental!");
env::var_os("WASM_BINDGEN_UNSTABLE_TEST_COVERAGE")?;
// TODO coverage link to wasm-bindgen book documenting correct usage

match env::var_os("WASM_BINDGEN_TEST_PROFRAW_OUT") {
Expand Down
48 changes: 30 additions & 18 deletions crates/cli/src/bin/wasm-bindgen-test-runner/server.rs
Expand Up @@ -245,24 +245,21 @@ pub fn spawn(
};
return set_isolate_origin_headers(Response::from_data("text/html", s));
} else if request.url() == "/__coverage/dump" {
let profraw_path = coverage.as_ref().expect(
"Received coverage dump request but server wasn't set up to accept coverage",
);
// 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).expect("Couldn't create .profraw for coverage");
let mut data = Vec::new();
request
.data()
.expect("Expected coverage data in body")
.read_to_end(&mut data)
.expect("Failed to read message body");

profraw
.write_all(&data)
.expect("Couldn't dump coverage data to profraw");
return Response::text("Coverage dumped");
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 {
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::text("Coverage dumped"),
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 @@ -311,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
4 changes: 1 addition & 3 deletions crates/test/Cargo.toml
Expand Up @@ -10,9 +10,7 @@ rust-version = "1.57"

[features]
default = []
experimental = ["coverage"]

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

[dependencies]
console_error_panic_hook = '0.1'
Expand Down
4 changes: 2 additions & 2 deletions crates/test/src/coverage.rs
@@ -1,6 +1,6 @@
use wasm_bindgen::prelude::wasm_bindgen;

#[cfg(feature = "coverage")]
#[cfg(feature = "unstable-coverage")]
#[wasm_bindgen]
pub fn __wbgtest_cov_dump() -> Vec<u8> {
let mut coverage = Vec::new();
Expand All @@ -19,7 +19,7 @@ pub fn __wbgtest_cov_dump() -> Vec<u8> {
/// Called when setting WASM_BINDGEN_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 = "coverage"))]
#[cfg(not(feature = "unstable-coverage"))]
#[wasm_bindgen]
pub fn __wbgtest_cov_dump() -> Vec<u8> {
console_error!(
Expand Down
9 changes: 0 additions & 9 deletions crates/test/src/lib.rs
Expand Up @@ -21,15 +21,6 @@ macro_rules! console_log {
)
}

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

/// Helper macro which acts like `println!` only routes to `console.error`
/// instead.
#[macro_export]
Expand Down
5 changes: 0 additions & 5 deletions crates/test/src/rt/mod.rs
Expand Up @@ -251,11 +251,6 @@ pub fn log(args: &fmt::Arguments) {
js_console_log(&args.to_string());
}

/// Internal implementation detail of the `console_warn!` macro.
pub fn log_warn(args: &fmt::Arguments) {
js_console_warn(&args.to_string());
}

/// Internal implementation detail of the `console_error!` macro.
pub fn log_error(args: &fmt::Arguments) {
js_console_error(&args.to_string());
Expand Down

0 comments on commit 49d083b

Please sign in to comment.