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> = RefCell::new(None); + static TEST_NAME: RefCell> = RefCell::new(None); } pub struct TestIdGuard { _private: (), } -pub fn init_root(tmp_dir: Option<&'static str>) -> TestIdGuard { - static NEXT_ID: AtomicUsize = AtomicUsize::new(0); - - let id = NEXT_ID.fetch_add(1, Ordering::SeqCst); - TEST_ID.with(|n| *n.borrow_mut() = Some(id)); - +pub fn init_root(tmp_dir: Option<&'static str>, test_name: String) -> TestIdGuard { + TEST_NAME.with(|n| *n.borrow_mut() = Some(test_name)); let guard = TestIdGuard { _private: () }; set_global_root(tmp_dir); @@ -85,20 +80,20 @@ pub fn init_root(tmp_dir: Option<&'static str>) -> TestIdGuard { impl Drop for TestIdGuard { fn drop(&mut self) { - TEST_ID.with(|n| *n.borrow_mut() = None); + TEST_NAME.with(|n| *n.borrow_mut() = None); } } pub fn root() -> PathBuf { - let id = TEST_ID.with(|n| { - n.borrow().expect( + let test_name = TEST_NAME.with(|n| { + n.borrow().clone().expect( "Tests must use the `#[cargo_test]` attribute in \ order to be able to use the crate root.", ) }); let mut root = global_root(); - root.push(&format!("t{}", id)); + root.push(test_name); root } diff --git a/tests/testsuite/bad_config.rs b/tests/testsuite/bad_config.rs index a6fe93cf39ab..08215ffabb32 100644 --- a/tests/testsuite/bad_config.rs +++ b/tests/testsuite/bad_config.rs @@ -20,7 +20,7 @@ fn bad1() { .with_stderr( "\ [ERROR] expected table for configuration key `target.nonexistent-target`, \ -but found string in [..]config +but found string in [..]/config ", ) .run(); diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index d5ee1f99c9ab..ccccfca71a0e 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -3707,7 +3707,7 @@ fn panic_abort_with_build_scripts() { p.root().join("target").rm_rf(); p.cargo("test --release -v") - .with_stderr_does_not_contain("[..]panic[..]") + .with_stderr_does_not_contain("[..]panic=abort[..]") .run(); } diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 5eb9bb274c9f..f07cec332a6d 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -433,7 +433,6 @@ fn cargo_cmd_bins_vs_explicit_path() { } } -#[test] #[cargo_test] fn cargo_subcommand_args() { let p = echo_subcommand();