diff --git a/Cargo.lock b/Cargo.lock
index 7b7d9272e54..f7727eca4d0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -476,7 +476,7 @@ version = "0.4.9"
[[package]]
name = "cargo-test-support"
-version = "0.9.2"
+version = "0.10.0"
dependencies = [
"anstream",
"anstyle",
diff --git a/Cargo.toml b/Cargo.toml
index d80b6228fb6..392758a17db 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -32,7 +32,7 @@ cargo-credential-macos-keychain = { version = "0.4.20", path = "credential/cargo
cargo-credential-wincred = { version = "0.4.20", path = "credential/cargo-credential-wincred" }
cargo-platform = { path = "crates/cargo-platform", version = "0.3.0" }
cargo-test-macro = { version = "0.4.9", path = "crates/cargo-test-macro" }
-cargo-test-support = { version = "0.9.2", path = "crates/cargo-test-support" }
+cargo-test-support = { version = "0.10.0", path = "crates/cargo-test-support" }
cargo-util = { version = "0.2.27", path = "crates/cargo-util" }
cargo-util-schemas = { version = "0.12.0", 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 8acf4f114fb..623b2c05e8f 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,35 @@ 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 name = &test_name
+ .clone()
+ .map(|n| n.split("::").next().unwrap().to_string())
+ .unwrap();
+
+ let mut new_body = to_token_stream(&format!(
+ r#"let _test_guard = {{
let tmp_dir = env!("CARGO_TARGET_TMPDIR");
- cargo_test_support::paths::init_root(tmp_dir)
- };"#,
- );
+ 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 0c62f1bdb66..e0441080a7c 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.2"
+version = "0.10.0"
edition.workspace = true
rust-version = "1.92" # MSRV:1
license.workspace = true
diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs
index 1a749007b78..0fe13139f4f 100644
--- a/crates/cargo-test-support/src/lib.rs
+++ b/crates/cargo-test-support/src/lib.rs
@@ -1423,7 +1423,13 @@ pub trait TestEnvCommandExt: Sized {
.env("__CARGO_TEST_DISABLE_GLOBAL_KNOWN_HOST", "1")
// Set retry sleep to 1 millisecond.
.env("__CARGO_TEST_FIXED_RETRY_SLEEP_MS", "1")
- .env("__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS", "200")
+ // Setting this to a large number helps avoid problems with long
+ // paths getting trimmed in snapshot tests.
+ //
+ // When updating this value, keep in mind that the `CARGO_TARGET_DIR`
+ // that gets set when Cargo's tests get run in `rust-lang/rust` can
+ // easily cause path lengths to exceed 200 characters.
+ .env("__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS", "400")
// Incremental generates a huge amount of data per test, which we
// don't particularly need. Tests that specifically need to check
// the incremental behavior should turn this back on.
diff --git a/crates/cargo-test-support/src/paths.rs b/crates/cargo-test-support/src/paths.rs
index a7400380ee5..f9542c0aed8 100644
--- a/crates/cargo-test-support/src/paths.rs
+++ b/crates/cargo-test-support/src/paths.rs
@@ -46,16 +46,13 @@ 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! {
static TEST_ID: RefCell