From 55dd64ab6b7c164a3960b501b0380f6efa0cb634 Mon Sep 17 00:00:00 2001 From: Herman Skogseth Date: Wed, 17 Dec 2025 12:31:03 +0100 Subject: [PATCH 1/2] test(test2): Add test checking module structure Note that the list of passing tests is 1, even though 2 tests have passed. Probably because they both share the exact same test name: `foo`. --- crates/libtest2/tests/testsuite/macros.rs | 46 +++++++++++++++++++++++ crates/libtest2/tests/testsuite/main.rs | 1 + 2 files changed, 47 insertions(+) create mode 100644 crates/libtest2/tests/testsuite/macros.rs diff --git a/crates/libtest2/tests/testsuite/macros.rs b/crates/libtest2/tests/testsuite/macros.rs new file mode 100644 index 0000000..dc0ccac --- /dev/null +++ b/crates/libtest2/tests/testsuite/macros.rs @@ -0,0 +1,46 @@ +use snapbox::str; + +fn test_cmd() -> snapbox::cmd::Command { + static BIN: once_cell_polyfill::sync::OnceLock<(std::path::PathBuf, std::path::PathBuf)> = + once_cell_polyfill::sync::OnceLock::new(); + let (bin, current_dir) = BIN.get_or_init(|| { + let package_root = crate::util::new_test( + r#" +#[libtest2::main] +fn main() {} + +#[libtest2::test] +fn foo(_context: &libtest2::TestContext) {} + +mod some_module { + #[libtest2::test] + fn foo(_context: &libtest2::TestContext) {} +} +"#, + false, + ); + let bin = crate::util::compile_test(&package_root); + (bin, package_root) + }); + snapbox::cmd::Command::new(bin).current_dir(current_dir) +} + +#[test] +fn check() { + let data = str![[r#" + +running 2 tests +test foo ... ok +test foo ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 filtered out; finished in [..]s + + +"#]]; + + test_cmd() + .args(["--test-threads", "1"]) + .assert() + .success() + .stdout_eq(data); +} diff --git a/crates/libtest2/tests/testsuite/main.rs b/crates/libtest2/tests/testsuite/main.rs index 2d83e9d..3d176ee 100644 --- a/crates/libtest2/tests/testsuite/main.rs +++ b/crates/libtest2/tests/testsuite/main.rs @@ -1,5 +1,6 @@ mod all_passing; mod argfile; +mod macros; mod mixed_bag; mod panic; mod should_panic; From b6e63d8f2a60b1ae64f3a729f8877fd4ca26972a Mon Sep 17 00:00:00 2001 From: Herman Skogseth Date: Wed, 17 Dec 2025 10:54:44 +0100 Subject: [PATCH 2/2] fix(test2): Capture names as full paths Regarding the bug shown visible in the test in the previous commit, where two passing tests both named `foo` would give output that only 1 test passed: The test macros should now always give unique names to all tests, so this is no longer visible when using the macros. However, unique test names is noo guaranteed when using the imperative API, so the bug could still happen (just not when using the macros). --- crates/libtest2/src/macros.rs | 4 +++- crates/libtest2/tests/testsuite/macros.rs | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/libtest2/src/macros.rs b/crates/libtest2/src/macros.rs index 54eef9a..9441dbb 100644 --- a/crates/libtest2/src/macros.rs +++ b/crates/libtest2/src/macros.rs @@ -110,7 +110,9 @@ macro_rules! _test_parse { fn name(&self) -> &str { $crate::_private::push!(crate::TESTS, _: $crate::_private::DynCase = $crate::_private::DynCase(&$name)); - stringify!($name) + const FULL_PATH: &str = concat!(std::module_path!(), "::", stringify!($name)); + let i = FULL_PATH.find("::").expect("we have inserted this in the line above so it must be there"); + &FULL_PATH[(i+2)..] } fn kind(&self) -> $crate::_private::TestKind { Default::default() diff --git a/crates/libtest2/tests/testsuite/macros.rs b/crates/libtest2/tests/testsuite/macros.rs index dc0ccac..37b439c 100644 --- a/crates/libtest2/tests/testsuite/macros.rs +++ b/crates/libtest2/tests/testsuite/macros.rs @@ -30,10 +30,10 @@ fn check() { let data = str![[r#" running 2 tests -test foo ... ok -test foo ... ok +test foo ... ok +test some_module::foo ... ok -test result: ok. 1 passed; 0 failed; 0 ignored; 0 filtered out; finished in [..]s +test result: ok. 2 passed; 0 failed; 0 ignored; 0 filtered out; finished in [..]s "#]];