aa0848
From 0be677fb6663ab6bfd02eae6ad32e7f031cfde0f Mon Sep 17 00:00:00 2001
aa0848
From: Yu Watanabe <watanabe.yu+github@gmail.com>
aa0848
Date: Wed, 2 Feb 2022 11:06:41 +0900
aa0848
Subject: [PATCH] test: allow to set NULL to intro or outro
aa0848
aa0848
Addresses https://github.com/systemd/systemd/pull/22338#discussion_r796741033.
aa0848
aa0848
(cherry picked from commit e85fdacc8ad7d91f140a135aaa3fd5372d3fa47c)
aa0848
aa0848
Related: #2017035
aa0848
---
aa0848
 src/shared/tests.h              | 45 +++++++++++++++++----------------
aa0848
 src/test/test-barrier.c         |  2 +-
aa0848
 src/test/test-cgroup-setup.c    |  2 +-
aa0848
 src/test/test-chown-rec.c       |  2 +-
aa0848
 src/test/test-format-table.c    |  2 +-
aa0848
 src/test/test-fs-util.c         |  2 +-
aa0848
 src/test/test-hashmap.c         |  2 +-
aa0848
 src/test/test-install-root.c    |  2 +-
aa0848
 src/test/test-load-fragment.c   |  2 +-
aa0848
 src/test/test-mountpoint-util.c |  2 +-
aa0848
 src/test/test-namespace.c       |  2 +-
aa0848
 src/test/test-proc-cmdline.c    |  2 +-
aa0848
 src/test/test-process-util.c    |  2 +-
aa0848
 src/test/test-sd-hwdb.c         |  2 +-
aa0848
 src/test/test-serialize.c       |  2 +-
aa0848
 src/test/test-sleep.c           |  2 +-
aa0848
 src/test/test-stat-util.c       |  2 +-
aa0848
 src/test/test-time-util.c       |  2 +-
aa0848
 src/test/test-unit-file.c       |  2 +-
aa0848
 src/test/test-unit-name.c       |  2 +-
aa0848
 src/test/test-unit-serialize.c  |  2 +-
aa0848
 src/test/test-utf8.c            |  2 +-
aa0848
 22 files changed, 44 insertions(+), 43 deletions(-)
aa0848
aa0848
diff --git a/src/shared/tests.h b/src/shared/tests.h
aa0848
index 59448f38f6..ef6acd368e 100644
aa0848
--- a/src/shared/tests.h
aa0848
+++ b/src/shared/tests.h
aa0848
@@ -110,27 +110,28 @@ static inline int run_test_table(void) {
aa0848
         return r;
aa0848
 }
aa0848
 
aa0848
-static inline int test_nop(void) {
aa0848
-        return EXIT_SUCCESS;
aa0848
-}
aa0848
-
aa0848
-#define DEFINE_CUSTOM_TEST_MAIN(log_level, intro, outro) \
aa0848
-        int main(int argc, char *argv[]) {               \
aa0848
-                int _r, _q;                              \
aa0848
-                test_setup_logging(log_level);           \
aa0848
-                save_argc_argv(argc, argv);              \
aa0848
-                _r = intro();                            \
aa0848
-                if (_r == EXIT_SUCCESS)                  \
aa0848
-                        _r = run_test_table();           \
aa0848
-                _q = outro();                            \
aa0848
-                static_destruct();                       \
aa0848
-                if (_r < 0)                              \
aa0848
-                        return EXIT_FAILURE;             \
aa0848
-                if (_r != EXIT_SUCCESS)                  \
aa0848
-                        return _r;                       \
aa0848
-                if (_q < 0)                              \
aa0848
-                        return EXIT_FAILURE;             \
aa0848
-                return _q;                               \
aa0848
+#define DEFINE_TEST_MAIN_FULL(log_level, intro, outro)    \
aa0848
+        int main(int argc, char *argv[]) {                \
aa0848
+                int (*_intro)(void) = intro;              \
aa0848
+                int (*_outro)(void) = outro;              \
aa0848
+                int _r, _q;                               \
aa0848
+                test_setup_logging(log_level);            \
aa0848
+                save_argc_argv(argc, argv);               \
aa0848
+                _r = _intro ? _intro() : EXIT_SUCCESS;    \
aa0848
+                if (_r == EXIT_SUCCESS)                   \
aa0848
+                        _r = run_test_table();            \
aa0848
+                _q = _outro ? _outro() : EXIT_SUCCESS;    \
aa0848
+                static_destruct();                        \
aa0848
+                if (_r < 0)                               \
aa0848
+                        return EXIT_FAILURE;              \
aa0848
+                if (_r != EXIT_SUCCESS)                   \
aa0848
+                        return _r;                        \
aa0848
+                if (_q < 0)                               \
aa0848
+                        return EXIT_FAILURE;              \
aa0848
+                return _q;                                \
aa0848
         }
aa0848
 
aa0848
-#define DEFINE_TEST_MAIN(log_level) DEFINE_CUSTOM_TEST_MAIN(log_level, test_nop, test_nop)
aa0848
+#define DEFINE_TEST_MAIN_WITH_INTRO(log_level, intro)   \
aa0848
+        DEFINE_TEST_MAIN_FULL(log_level, intro, NULL)
aa0848
+#define DEFINE_TEST_MAIN(log_level)                     \
aa0848
+        DEFINE_TEST_MAIN_FULL(log_level, NULL, NULL)
aa0848
diff --git a/src/test/test-barrier.c b/src/test/test-barrier.c
aa0848
index b87538806a..bbd7e2bddb 100644
aa0848
--- a/src/test/test-barrier.c
aa0848
+++ b/src/test/test-barrier.c
aa0848
@@ -444,4 +444,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
  }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-cgroup-setup.c b/src/test/test-cgroup-setup.c
aa0848
index 6f93647685..c377ff0a00 100644
aa0848
--- a/src/test/test-cgroup-setup.c
aa0848
+++ b/src/test/test-cgroup-setup.c
aa0848
@@ -71,4 +71,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_DEBUG, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
aa0848
diff --git a/src/test/test-chown-rec.c b/src/test/test-chown-rec.c
aa0848
index 691cfe767f..97711f58b0 100644
aa0848
--- a/src/test/test-chown-rec.c
aa0848
+++ b/src/test/test-chown-rec.c
aa0848
@@ -156,4 +156,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_DEBUG, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
aa0848
diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c
aa0848
index 7515a74c12..1b4963d928 100644
aa0848
--- a/src/test/test-format-table.c
aa0848
+++ b/src/test/test-format-table.c
aa0848
@@ -535,4 +535,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c
aa0848
index da5a16b4bc..602ce75f98 100644
aa0848
--- a/src/test/test-fs-util.c
aa0848
+++ b/src/test/test-fs-util.c
aa0848
@@ -973,4 +973,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c
aa0848
index 4dc155d818..dbf762cc0b 100644
aa0848
--- a/src/test/test-hashmap.c
aa0848
+++ b/src/test/test-hashmap.c
aa0848
@@ -169,4 +169,4 @@ static int outro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, outro);
aa0848
+DEFINE_TEST_MAIN_FULL(LOG_INFO, intro, outro);
aa0848
diff --git a/src/test/test-install-root.c b/src/test/test-install-root.c
aa0848
index f540a832bd..f718689c3a 100644
aa0848
--- a/src/test/test-install-root.c
aa0848
+++ b/src/test/test-install-root.c
aa0848
@@ -1272,4 +1272,4 @@ static int intro(void) {
aa0848
 }
aa0848
 
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-load-fragment.c b/src/test/test-load-fragment.c
aa0848
index 2e105df56a..1bd68c7e0a 100644
aa0848
--- a/src/test/test-load-fragment.c
aa0848
+++ b/src/test/test-load-fragment.c
aa0848
@@ -906,4 +906,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-mountpoint-util.c b/src/test/test-mountpoint-util.c
aa0848
index 102d2850bf..4d140c42b6 100644
aa0848
--- a/src/test/test-mountpoint-util.c
aa0848
+++ b/src/test/test-mountpoint-util.c
aa0848
@@ -313,4 +313,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_DEBUG, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
aa0848
diff --git a/src/test/test-namespace.c b/src/test/test-namespace.c
aa0848
index f9e34f3bfa..7a634adca9 100644
aa0848
--- a/src/test/test-namespace.c
aa0848
+++ b/src/test/test-namespace.c
aa0848
@@ -227,4 +227,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-proc-cmdline.c b/src/test/test-proc-cmdline.c
aa0848
index 064b4d838f..1f43bb3eb0 100644
aa0848
--- a/src/test/test-proc-cmdline.c
aa0848
+++ b/src/test/test-proc-cmdline.c
aa0848
@@ -254,4 +254,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c
aa0848
index 8661934929..7a8adad50c 100644
aa0848
--- a/src/test/test-process-util.c
aa0848
+++ b/src/test/test-process-util.c
aa0848
@@ -900,4 +900,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-sd-hwdb.c b/src/test/test-sd-hwdb.c
aa0848
index 88992a6c2b..4251e2a809 100644
aa0848
--- a/src/test/test-sd-hwdb.c
aa0848
+++ b/src/test/test-sd-hwdb.c
aa0848
@@ -63,4 +63,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_DEBUG, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
aa0848
diff --git a/src/test/test-serialize.c b/src/test/test-serialize.c
aa0848
index 9aeb6c5920..bcf2e843b0 100644
aa0848
--- a/src/test/test-serialize.c
aa0848
+++ b/src/test/test-serialize.c
aa0848
@@ -195,4 +195,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-sleep.c b/src/test/test-sleep.c
aa0848
index f56e7e0167..5aebcdd935 100644
aa0848
--- a/src/test/test-sleep.c
aa0848
+++ b/src/test/test-sleep.c
aa0848
@@ -125,4 +125,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_DEBUG, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
aa0848
diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c
aa0848
index 2965ee679f..7f633ab259 100644
aa0848
--- a/src/test/test-stat-util.c
aa0848
+++ b/src/test/test-stat-util.c
aa0848
@@ -241,4 +241,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c
aa0848
index f21d8b7794..554693834b 100644
aa0848
--- a/src/test/test-time-util.c
aa0848
+++ b/src/test/test-time-util.c
aa0848
@@ -607,4 +607,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c
aa0848
index 6c9f245c7e..cc08a4ae4b 100644
aa0848
--- a/src/test/test-unit-file.c
aa0848
+++ b/src/test/test-unit-file.c
aa0848
@@ -107,4 +107,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_DEBUG, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
aa0848
diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c
aa0848
index 1f65407e5f..8cd0e0b4a1 100644
aa0848
--- a/src/test/test-unit-name.c
aa0848
+++ b/src/test/test-unit-name.c
aa0848
@@ -856,4 +856,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
aa0848
diff --git a/src/test/test-unit-serialize.c b/src/test/test-unit-serialize.c
aa0848
index 5d39176db2..3ef15f3b1e 100644
aa0848
--- a/src/test/test-unit-serialize.c
aa0848
+++ b/src/test/test-unit-serialize.c
aa0848
@@ -60,4 +60,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_DEBUG, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
aa0848
diff --git a/src/test/test-utf8.c b/src/test/test-utf8.c
aa0848
index 1b31d1f852..7337b81227 100644
aa0848
--- a/src/test/test-utf8.c
aa0848
+++ b/src/test/test-utf8.c
aa0848
@@ -236,4 +236,4 @@ static int intro(void) {
aa0848
         return EXIT_SUCCESS;
aa0848
 }
aa0848
 
aa0848
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, intro, test_nop);
aa0848
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);