Skip to content

Commit

Permalink
Use OS provided temporary directory for tests (#4361)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Dec 17, 2024
1 parent e250852 commit c0fd24c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 49 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
* Pass along an `ignore` attribute to `unsupported` tests.
[#4360](https://github.com/rustwasm/wasm-bindgen/pull/4360)

* Use OS provided temporary directory for tests instead of Cargo's `target` directory.
[#4361](https://github.com/rustwasm/wasm-bindgen/pull/4361)

--------------------------------------------------------------------------------

## [0.2.99](https://github.com/rustwasm/wasm-bindgen/compare/0.2.98...0.2.99)
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ rouille = { version = "3.0.0", default-features = false }
serde = { version = "1.0", features = ['derive'] }
serde_derive = "1.0"
serde_json = "1.0"
tempfile = "3.0"
ureq = { version = "2.7", default-features = false, features = ["brotli", "gzip"] }
walrus = "0.23"
wasm-bindgen-cli-support = { path = "../cli-support", version = "=0.2.99" }
Expand All @@ -40,7 +41,6 @@ assert_cmd = "2"
diff = "0.1"
predicates = "3"
rayon = "1.0"
tempfile = "3.0"
wasmparser = "0.214"
wasmprinter = "0.214"

Expand Down
60 changes: 12 additions & 48 deletions crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
//! For more documentation about this see the `wasm-bindgen-test` crate README
//! and source code.
use anyhow::{anyhow, bail, Context};
use anyhow::{bail, Context};
use clap::Parser;
use log::error;
use std::env;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -83,32 +82,10 @@ fn main() -> anyhow::Result<()> {
let file_name = cli
.file
.file_name()
.and_then(|s| s.to_str())
.map(Path::new)
.context("file to test is not a valid file, can't extract file name")?;

// wasm_file_to_test may be
// - a cargo-like directory layout and generate output at
// `target/wasm32-unknown-unknown/...`
// - a tmp directory, generated by rustdoc
// we would like a directory we have write access to. if we assume cargo-like directories,
// we end up with the path `/wbg-out`
let wasm_file_str = cli.file.to_string_lossy();
let tmpdir =
if wasm_file_str.starts_with("/tmp/rustdoc") || wasm_file_str.starts_with("/var/folders") {
cli.file.parent() // chop off the file name and give us the /tmp/rustdoc<hash> directory
} else {
cli.file
.parent() // chop off file name
.and_then(|p| p.parent()) // chop off `deps`
.and_then(|p| p.parent()) // chop off `debug`
}
.map(|p| p.join(format!("wbg-tmp-{}", file_name)))
.ok_or_else(|| anyhow!("file to test doesn't follow the expected Cargo conventions"))?;

// Make sure there's no stale state from before
drop(fs::remove_dir_all(&tmpdir));
fs::create_dir(&tmpdir).context("creating temporary directory")?;
let _guard = TmpDirDeleteGuard(tmpdir.clone());
let tmpdir = tempfile::tempdir()?;

let module = "wasm-bindgen-test";

Expand Down Expand Up @@ -247,7 +224,7 @@ fn main() -> anyhow::Result<()> {
b.split_linked_modules(true);
}

let coverage = coverage_args(&tmpdir);
let coverage = coverage_args(file_name);

b.debug(debug)
.input_module(module, wasm)
Expand All @@ -259,9 +236,9 @@ fn main() -> anyhow::Result<()> {

match test_mode {
TestMode::Node { no_modules } => {
node::execute(module, &tmpdir, cli, &tests, !no_modules, coverage)?
node::execute(module, tmpdir.path(), cli, &tests, !no_modules, coverage)?
}
TestMode::Deno => deno::execute(module, &tmpdir, cli, &tests)?,
TestMode::Deno => deno::execute(module, tmpdir.path(), cli, &tests)?,
TestMode::Browser { .. }
| TestMode::DedicatedWorker { .. }
| TestMode::SharedWorker { .. }
Expand All @@ -276,7 +253,7 @@ fn main() -> anyhow::Result<()> {
},
headless,
module,
&tmpdir,
tmpdir.path(),
cli,
&tests,
test_mode,
Expand Down Expand Up @@ -350,22 +327,9 @@ impl TestMode {
}
}

struct TmpDirDeleteGuard(PathBuf);

impl Drop for TmpDirDeleteGuard {
fn drop(&mut self) {
if let Err(e) = fs::remove_dir_all(&self.0) {
error!("failed to remove temporary directory: {}", e);
}
}
}

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

Expand All @@ -377,10 +341,10 @@ fn coverage_args(tmpdir: &Path) -> PathBuf {
Some(s) => {
let mut buf = PathBuf::from(s);
if buf.is_dir() {
buf.push(generated(tmpdir, &prefix));
buf.push(generated(file_name, &prefix));
}
buf
}
None => PathBuf::from(generated(tmpdir, &prefix)),
None => PathBuf::from(generated(file_name, &prefix)),
}
}

0 comments on commit c0fd24c

Please sign in to comment.