diff --git a/Cargo.lock b/Cargo.lock
index 3e101ff0577..e9be88d9c06 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -449,7 +449,7 @@ version = "0.4.8"
[[package]]
name = "cargo-test-support"
-version = "0.9.1"
+version = "0.10.0"
dependencies = [
"anstream",
"anstyle",
diff --git a/Cargo.toml b/Cargo.toml
index e80e07f64ed..9bad275f5cf 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -32,7 +32,7 @@ cargo-credential-macos-keychain = { version = "0.4.19", path = "credential/cargo
cargo-credential-wincred = { version = "0.4.19", path = "credential/cargo-credential-wincred" }
cargo-platform = { path = "crates/cargo-platform", version = "0.3.0" }
cargo-test-macro = { version = "0.4.8", path = "crates/cargo-test-macro" }
-cargo-test-support = { version = "0.9.1", path = "crates/cargo-test-support" }
+cargo-test-support = { version = "0.10.0", path = "crates/cargo-test-support" }
cargo-util = { version = "0.2.26", path = "crates/cargo-util" }
cargo-util-schemas = { version = "0.10.3", path = "crates/cargo-util-schemas" }
cargo_metadata = "0.23.1"
diff --git a/crates/cargo-test-macro/src/lib.rs b/crates/cargo-test-macro/src/lib.rs
index 5f0eecfeb6c..6926de38c91 100644
--- a/crates/cargo-test-macro/src/lib.rs
+++ b/crates/cargo-test-macro/src/lib.rs
@@ -200,6 +200,9 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
add_attr(&mut ret, "ignore", reason);
}
+ let mut test_name = None;
+ let mut num = 0;
+
// Find where the function body starts, and add the boilerplate at the start.
for token in item {
let group = match token {
@@ -211,18 +214,44 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
continue;
}
}
+ TokenTree::Ident(i) => {
+ // The first time through it will be `fn` the second time is the
+ // name of the test.
+ if test_name.is_none() && num == 1 {
+ test_name = Some(i.to_string())
+ } else {
+ num += 1;
+ }
+ ret.extend(Some(TokenTree::Ident(i)));
+ continue;
+ }
other => {
ret.extend(Some(other));
continue;
}
};
- let mut new_body = to_token_stream(
- r#"let _test_guard = {
- let tmp_dir = option_env!("CARGO_TARGET_TMPDIR");
- cargo_test_support::paths::init_root(tmp_dir)
- };"#,
- );
+ let name = &test_name
+ .clone()
+ .map(|n| n.split("::").next().unwrap().to_string())
+ .unwrap();
+
+ let mut new_body = if cfg!(windows) {
+ to_token_stream(
+ r#"let _test_guard = {
+ let tmp_dir = option_env!("CARGO_TARGET_TMPDIR");
+ cargo_test_support::paths::init_root(tmp_dir)
+ };"#,
+ )
+ } else {
+ to_token_stream(&format!(
+ r#"let _test_guard = {{
+ let tmp_dir = option_env!("CARGO_TARGET_TMPDIR");
+ let test_dir = cargo_test_support::paths::test_dir(std::file!(), "{name}");
+ cargo_test_support::paths::init_root(tmp_dir, test_dir)
+ }};"#
+ ))
+ };
new_body.extend(group.stream());
ret.extend(Some(TokenTree::from(Group::new(
diff --git a/crates/cargo-test-support/Cargo.toml b/crates/cargo-test-support/Cargo.toml
index 89d13913f1a..30c382ca779 100644
--- a/crates/cargo-test-support/Cargo.toml
+++ b/crates/cargo-test-support/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "cargo-test-support"
-version = "0.9.1"
+version = "0.10.0"
edition.workspace = true
rust-version = "1.91" # MSRV:1
license.workspace = true
diff --git a/crates/cargo-test-support/src/paths.rs b/crates/cargo-test-support/src/paths.rs
index 9c08afa9020..0a82efa9de0 100644
--- a/crates/cargo-test-support/src/paths.rs
+++ b/crates/cargo-test-support/src/paths.rs
@@ -62,16 +62,15 @@ pub fn global_root() -> PathBuf {
}
}
-// We need to give each test a unique id. The test name could serve this
-// purpose, but the `test` crate doesn't have a way to obtain the current test
-// name.[*] Instead, we used the `cargo-test-macro` crate to automatically
-// insert an init function for each test that sets the test name in a thread
-// local variable.
-//
-// [*] It does set the thread name, but only when running concurrently. If not
-// running concurrently, all tests are run on the main thread.
+// We need to give each test a unique id. The test name serve this
+// purpose. We are able to get the test name by having the `cargo-test-macro`
+// crate automatically insert an init function for each test that sets the
+// test name in a thread local variable.
thread_local! {
+ #[cfg(windows)]
static TEST_ID: RefCell