diff --git a/crates/cargo-test-macro/src/lib.rs b/crates/cargo-test-macro/src/lib.rs
index aa06f477de03..b0259c13614c 100644
--- a/crates/cargo-test-macro/src/lib.rs
+++ b/crates/cargo-test-macro/src/lib.rs
@@ -133,6 +133,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 {
@@ -144,18 +147,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;
}
};
+ std::file!();
+ let name = test_name
+ .clone()
+ .map(|n| n.split("::").next().unwrap().to_string())
+ .unwrap();
- let mut new_body = to_token_stream(
- r#"let _test_guard = {
+ let mut new_body = to_token_stream(&format!(
+ r#"let _test_guard = {{
let tmp_dir = option_env!("CARGO_TARGET_TMPDIR");
- cargo_test_support::paths::init_root(tmp_dir)
- };"#,
- );
+ let file_name = std::path::PathBuf::from(
+ std::file!().strip_prefix("tests/testsuite/").unwrap()
+ )
+ .ancestors()
+ .next()
+ .unwrap()
+ .to_str()
+ .unwrap()
+ .trim_end_matches(".rs")
+ .to_string();
+ cargo_test_support::paths::init_root(tmp_dir, format!("{{}}/{name}", file_name))
+ }};"#
+ ));
new_body.extend(group.stream());
ret.extend(Some(TokenTree::from(Group::new(
diff --git a/crates/cargo-test-support/src/paths.rs b/crates/cargo-test-support/src/paths.rs
index ef1fddb70035..d4f19939d29b 100644
--- a/crates/cargo-test-support/src/paths.rs
+++ b/crates/cargo-test-support/src/paths.rs
@@ -7,7 +7,6 @@ use std::fs;
use std::io::{self, ErrorKind};
use std::path::{Path, PathBuf};
use std::process::Command;
-use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
static CARGO_INTEGRATION_TEST_DIR: &str = "cit";
@@ -60,19 +59,15 @@ pub fn global_root() -> PathBuf {
// [*] It does set the thread name, but only when running concurrently. If not
// running concurrently, all tests are run on the main thread.
thread_local! {
- static TEST_ID: RefCell