Skip to content

Commit

Permalink
feat: Use test name for dir when running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Feb 18, 2023
1 parent eff8d69 commit 420dfcb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
39 changes: 34 additions & 5 deletions crates/cargo-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
19 changes: 7 additions & 12 deletions crates/cargo-test-support/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<Option<usize>> = RefCell::new(None);
static TEST_NAME: RefCell<Option<String>> = 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);
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/bad_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ fn cargo_cmd_bins_vs_explicit_path() {
}
}

#[test]
#[cargo_test]
fn cargo_subcommand_args() {
let p = echo_subcommand();
Expand Down

0 comments on commit 420dfcb

Please sign in to comment.