Skip to content

Commit bd80ab8

Browse files
authored
Capture names as full paths (#165)
The tests emitted by rustc use the full path, not including the crate name, as the name for the test ([impl](https://github.com/rust-lang/rust/blob/ec6f62244c3a019e2224b779d2b606721cabf8f2/compiler/rustc_builtin_macros/src/test.rs#L66)). From what I can tell there's no way to get this path exactly from rust, but we can get the full path including crate name from [`std::module_path`](https://doc.rust-lang.org/std/macro.module_path.html). We can then find the first occurence of `::` and grab whatever follows. Fixes #154.
2 parents 63e8214 + b6e63d8 commit bd80ab8

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

crates/libtest2/src/macros.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ macro_rules! _test_parse {
110110
fn name(&self) -> &str {
111111
$crate::_private::push!(crate::TESTS, _: $crate::_private::DynCase = $crate::_private::DynCase(&$name));
112112

113-
stringify!($name)
113+
const FULL_PATH: &str = concat!(std::module_path!(), "::", stringify!($name));
114+
let i = FULL_PATH.find("::").expect("we have inserted this in the line above so it must be there");
115+
&FULL_PATH[(i+2)..]
114116
}
115117
fn kind(&self) -> $crate::_private::TestKind {
116118
Default::default()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use snapbox::str;
2+
3+
fn test_cmd() -> snapbox::cmd::Command {
4+
static BIN: once_cell_polyfill::sync::OnceLock<(std::path::PathBuf, std::path::PathBuf)> =
5+
once_cell_polyfill::sync::OnceLock::new();
6+
let (bin, current_dir) = BIN.get_or_init(|| {
7+
let package_root = crate::util::new_test(
8+
r#"
9+
#[libtest2::main]
10+
fn main() {}
11+
12+
#[libtest2::test]
13+
fn foo(_context: &libtest2::TestContext) {}
14+
15+
mod some_module {
16+
#[libtest2::test]
17+
fn foo(_context: &libtest2::TestContext) {}
18+
}
19+
"#,
20+
false,
21+
);
22+
let bin = crate::util::compile_test(&package_root);
23+
(bin, package_root)
24+
});
25+
snapbox::cmd::Command::new(bin).current_dir(current_dir)
26+
}
27+
28+
#[test]
29+
fn check() {
30+
let data = str![[r#"
31+
32+
running 2 tests
33+
test foo ... ok
34+
test some_module::foo ... ok
35+
36+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 filtered out; finished in [..]s
37+
38+
39+
"#]];
40+
41+
test_cmd()
42+
.args(["--test-threads", "1"])
43+
.assert()
44+
.success()
45+
.stdout_eq(data);
46+
}

crates/libtest2/tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod all_passing;
22
mod argfile;
3+
mod macros;
34
mod mixed_bag;
45
mod panic;
56
mod should_panic;

0 commit comments

Comments
 (0)