Skip to content

Commit 420dfcb

Browse files
committed
feat: Use test name for dir when running tests
1 parent eff8d69 commit 420dfcb

File tree

5 files changed

+43
-20
lines changed

5 files changed

+43
-20
lines changed

crates/cargo-test-macro/src/lib.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
133133
add_attr(&mut ret, "ignore", reason);
134134
}
135135

136+
let mut test_name = None;
137+
let mut num = 0;
138+
136139
// Find where the function body starts, and add the boilerplate at the start.
137140
for token in item {
138141
let group = match token {
@@ -144,18 +147,44 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
144147
continue;
145148
}
146149
}
150+
TokenTree::Ident(i) => {
151+
// The first time through it will be `fn` the second time is the
152+
// name of the test.
153+
if test_name.is_none() && num == 1 {
154+
test_name = Some(i.to_string())
155+
} else {
156+
num += 1;
157+
}
158+
ret.extend(Some(TokenTree::Ident(i)));
159+
continue;
160+
}
147161
other => {
148162
ret.extend(Some(other));
149163
continue;
150164
}
151165
};
166+
std::file!();
167+
let name = test_name
168+
.clone()
169+
.map(|n| n.split("::").next().unwrap().to_string())
170+
.unwrap();
152171

153-
let mut new_body = to_token_stream(
154-
r#"let _test_guard = {
172+
let mut new_body = to_token_stream(&format!(
173+
r#"let _test_guard = {{
155174
let tmp_dir = option_env!("CARGO_TARGET_TMPDIR");
156-
cargo_test_support::paths::init_root(tmp_dir)
157-
};"#,
158-
);
175+
let file_name = std::path::PathBuf::from(
176+
std::file!().strip_prefix("tests/testsuite/").unwrap()
177+
)
178+
.ancestors()
179+
.next()
180+
.unwrap()
181+
.to_str()
182+
.unwrap()
183+
.trim_end_matches(".rs")
184+
.to_string();
185+
cargo_test_support::paths::init_root(tmp_dir, format!("{{}}/{name}", file_name))
186+
}};"#
187+
));
159188

160189
new_body.extend(group.stream());
161190
ret.extend(Some(TokenTree::from(Group::new(

crates/cargo-test-support/src/paths.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::fs;
77
use std::io::{self, ErrorKind};
88
use std::path::{Path, PathBuf};
99
use std::process::Command;
10-
use std::sync::atomic::{AtomicUsize, Ordering};
1110
use std::sync::Mutex;
1211

1312
static CARGO_INTEGRATION_TEST_DIR: &str = "cit";
@@ -60,19 +59,15 @@ pub fn global_root() -> PathBuf {
6059
// [*] It does set the thread name, but only when running concurrently. If not
6160
// running concurrently, all tests are run on the main thread.
6261
thread_local! {
63-
static TEST_ID: RefCell<Option<usize>> = RefCell::new(None);
62+
static TEST_NAME: RefCell<Option<String>> = RefCell::new(None);
6463
}
6564

6665
pub struct TestIdGuard {
6766
_private: (),
6867
}
6968

70-
pub fn init_root(tmp_dir: Option<&'static str>) -> TestIdGuard {
71-
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
72-
73-
let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
74-
TEST_ID.with(|n| *n.borrow_mut() = Some(id));
75-
69+
pub fn init_root(tmp_dir: Option<&'static str>, test_name: String) -> TestIdGuard {
70+
TEST_NAME.with(|n| *n.borrow_mut() = Some(test_name));
7671
let guard = TestIdGuard { _private: () };
7772

7873
set_global_root(tmp_dir);
@@ -85,20 +80,20 @@ pub fn init_root(tmp_dir: Option<&'static str>) -> TestIdGuard {
8580

8681
impl Drop for TestIdGuard {
8782
fn drop(&mut self) {
88-
TEST_ID.with(|n| *n.borrow_mut() = None);
83+
TEST_NAME.with(|n| *n.borrow_mut() = None);
8984
}
9085
}
9186

9287
pub fn root() -> PathBuf {
93-
let id = TEST_ID.with(|n| {
94-
n.borrow().expect(
88+
let test_name = TEST_NAME.with(|n| {
89+
n.borrow().clone().expect(
9590
"Tests must use the `#[cargo_test]` attribute in \
9691
order to be able to use the crate root.",
9792
)
9893
});
9994

10095
let mut root = global_root();
101-
root.push(&format!("t{}", id));
96+
root.push(test_name);
10297
root
10398
}
10499

tests/testsuite/bad_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn bad1() {
2020
.with_stderr(
2121
"\
2222
[ERROR] expected table for configuration key `target.nonexistent-target`, \
23-
but found string in [..]config
23+
but found string in [..]/config
2424
",
2525
)
2626
.run();

tests/testsuite/build_script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3707,7 +3707,7 @@ fn panic_abort_with_build_scripts() {
37073707
p.root().join("target").rm_rf();
37083708

37093709
p.cargo("test --release -v")
3710-
.with_stderr_does_not_contain("[..]panic[..]")
3710+
.with_stderr_does_not_contain("[..]panic=abort[..]")
37113711
.run();
37123712
}
37133713

tests/testsuite/cargo_command.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,6 @@ fn cargo_cmd_bins_vs_explicit_path() {
433433
}
434434
}
435435

436-
#[test]
437436
#[cargo_test]
438437
fn cargo_subcommand_args() {
439438
let p = echo_subcommand();

0 commit comments

Comments
 (0)