Use linkme rather than collecting tests manually.
Bug: 260692911
Test: atest apkdmverity.test libdm_rust.test
Change-Id: I9355dc13e31c4487109be31e2d7a7a50b16fb889
diff --git a/apkdmverity/src/main.rs b/apkdmverity/src/main.rs
index 506af48..55953a9 100644
--- a/apkdmverity/src/main.rs
+++ b/apkdmverity/src/main.rs
@@ -153,28 +153,17 @@
}
#[cfg(test)]
-ignorabletest::test_main!(tests::all_tests());
+ignorabletest::test_main!();
#[cfg(test)]
mod tests {
use crate::*;
- use ignorabletest::{list_tests, test};
+ use ignorabletest::test;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::ops::Deref;
use std::os::unix::fs::FileExt;
- list_tests! {all_tests: [
- correct_inputs,
- incorrect_apk,
- incorrect_merkle_tree,
- tampered_apk,
- tampered_idsig,
- inputs_are_block_devices,
- correct_custom_roothash,
- verify_command,
- ]}
-
struct TestContext<'a> {
data_backing_file: &'a Path,
hash_backing_file: &'a Path,
diff --git a/libs/devicemapper/src/lib.rs b/libs/devicemapper/src/lib.rs
index 1b18b49..0170795 100644
--- a/libs/devicemapper/src/lib.rs
+++ b/libs/devicemapper/src/lib.rs
@@ -233,13 +233,13 @@
}
#[cfg(test)]
-ignorabletest::test_main!(tests::all_tests());
+ignorabletest::test_main!();
#[cfg(test)]
mod tests {
use super::*;
use crypt::{CipherType, DmCryptTargetBuilder};
- use ignorabletest::{list_tests, test};
+ use ignorabletest::test;
use rustutils::system_properties;
use std::fs::{read, File, OpenOptions};
use std::io::Write;
@@ -262,13 +262,6 @@
different_key: b"drowgnolyllaergnolsetybowtytriht",
};
- list_tests! {all_tests: [
- mapping_again_keeps_data_xts,
- mapping_again_keeps_data_hctr2,
- data_inaccessible_with_diff_key_xts,
- data_inaccessible_with_diff_key_hctr2,
- ]}
-
// Create a file in given temp directory with given size
fn prepare_tmpfile(test_dir: &Path, filename: &str, sz: u64) -> PathBuf {
let filepath = test_dir.join(filename);
diff --git a/libs/ignorabletest/Android.bp b/libs/ignorabletest/Android.bp
index 0947243..10aef8e 100644
--- a/libs/ignorabletest/Android.bp
+++ b/libs/ignorabletest/Android.bp
@@ -8,6 +8,7 @@
edition: "2021",
rustlibs: [
"liblibtest_mimic",
+ "liblinkme",
],
proc_macros: ["libpaste"],
apex_available: [
@@ -22,5 +23,12 @@
cfgs: ["test"],
rustlibs: [
"libignorabletest",
+ "liblinkme",
+ ],
+ // Without this flag we get linker errors saying to add it. See
+ // https://github.com/dtolnay/linkme/issues/49 and related issues.
+ ld_flags: [
+ "-z",
+ "nostart-stop-gc",
],
}
diff --git a/libs/ignorabletest/README.md b/libs/ignorabletest/README.md
index e03864f..77140bd 100644
--- a/libs/ignorabletest/README.md
+++ b/libs/ignorabletest/README.md
@@ -56,23 +56,11 @@
```
Somewhere in your main module, you need to use the `test_main` macro to generate an entry point for
-the test harness. This needs to be provided with a list of all tests, which can be generated by the `list_tests!` macro:
+the test harness:
```rust
#[cfg(test)]
-ignorabletest::test_main!(tests::all_tests());
-
-#[cfg(test)]
-mod tests {
- use ignorabletest::{list_tests, test};
-
- list_tests! {all_tests: [
- one_plus_one,
- clap_hands,
- ]}
-
- // ...
-}
+ignorabletest::test_main!();
```
You can then run your tests as usual with `atest`.
diff --git a/libs/ignorabletest/src/lib.rs b/libs/ignorabletest/src/lib.rs
index 746c0dd..c7243e6 100644
--- a/libs/ignorabletest/src/lib.rs
+++ b/libs/ignorabletest/src/lib.rs
@@ -5,42 +5,17 @@
#[doc(hidden)]
pub use libtest_mimic as _libtest_mimic;
#[doc(hidden)]
+pub use linkme as _linkme;
+#[doc(hidden)]
pub use paste as _paste;
/// Macro to generate the main function for the test harness.
#[macro_export]
macro_rules! test_main {
- ($tests:expr) => {
+ () => {
#[cfg(test)]
fn main() {
- ignorabletest::runner::main($tests)
- }
- };
-}
-
-/// Macro to generate a function which returns a list of tests to be run.
-///
-/// # Usage
-/// ```
-/// list_tests!{all_tests: [test_this, test_that]};
-///
-/// test!(test_this);
-/// fn test_this() {
-/// // ...
-/// }
-///
-/// test!(test_that);
-/// fn test_that() {
-/// // ...
-/// }
-/// ```
-#[macro_export]
-macro_rules! list_tests {
- {$function_name:ident: [$( $test_name:ident ),* $(,)? ]} => {
- pub fn $function_name() -> ::std::vec::Vec<$crate::_libtest_mimic::Trial> {
- vec![
- $( $crate::_paste::paste!([<__test_ $test_name>]()) ),*
- ]
+ ignorabletest::runner::main()
}
};
}
@@ -59,6 +34,7 @@
macro_rules! test {
($test_name:ident) => {
$crate::_paste::paste!(
+ #[$crate::_linkme::distributed_slice($crate::runner::IGNORABLETEST_TESTS)]
fn [< __test_ $test_name >]() -> $crate::_libtest_mimic::Trial {
$crate::_libtest_mimic::Trial::test(
::std::stringify!($test_name),
@@ -69,6 +45,7 @@
};
($test_name:ident, ignore_if: $ignore_expr:expr) => {
$crate::_paste::paste!(
+ #[$crate::_linkme::distributed_slice($crate::runner::IGNORABLETEST_TESTS)]
fn [< __test_ $test_name >]() -> $crate::_libtest_mimic::Trial {
$crate::_libtest_mimic::Trial::test(
::std::stringify!($test_name),
diff --git a/libs/ignorabletest/src/runner.rs b/libs/ignorabletest/src/runner.rs
index e1b14e0..4ec3d79 100644
--- a/libs/ignorabletest/src/runner.rs
+++ b/libs/ignorabletest/src/runner.rs
@@ -2,14 +2,21 @@
use core::ops::{Deref, FnOnce};
use libtest_mimic::{Arguments, Failed, Trial};
+use linkme::distributed_slice;
use std::env;
/// Command-line arguments to ignore, because they are not supported by libtest-mimic.
const IGNORED_ARGS: [&str; 2] = ["-Zunstable-options", "--report-time"];
+/// The collection of all tests to run.
+#[doc(hidden)]
+#[distributed_slice]
+pub static IGNORABLETEST_TESTS: [fn() -> Trial] = [..];
+
/// Runs all tests.
-pub fn main(tests: Vec<Trial>) {
+pub fn main() {
let args = Arguments::from_iter(env::args().filter(|arg| !IGNORED_ARGS.contains(&arg.deref())));
+ let tests = IGNORABLETEST_TESTS.iter().map(|test| test()).collect();
libtest_mimic::run(&args, tests).exit();
}