Skip to content

Commit c462e63

Browse files
committed
test: Use TEST macro in more cases
This converts to TEST macro in less trivial cases. This is mostly due to having an intro or outro before/after the actual tests. Some notable changes: - add a "test" to make sure the hashmap and ordered_hashmap tests from different compilation units are actually run in test-hashmap.c - make root arg a global var in test-install-root.c - slightly rework an EFI specific test in test-proc-cmdline.c - usage of saved_argv/saved_argc in test-process-util.c - splitting test-rlimit-util.c into several tests - moving the hwdb open check into intro in test-sd-hwdb.c - condense several "tests" into one in test-udev-util.c
1 parent 4f7452a commit c462e63

13 files changed

+299
-668
lines changed

src/test/test-cgroup-setup.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "tests.h"
1212
#include "version.h"
1313

14-
static void test_is_wanted_print(bool header) {
14+
static void test_is_wanted_print_one(bool header) {
1515
_cleanup_free_ char *cmdline = NULL;
1616

1717
log_info("-- %s --", __func__);
@@ -28,46 +28,46 @@ static void test_is_wanted_print(bool header) {
2828
log_info(" ");
2929
}
3030

31-
static void test_is_wanted(void) {
31+
TEST(is_wanted_print) {
32+
test_is_wanted_print_one(true);
33+
test_is_wanted_print_one(false); /* run twice to test caching */
34+
}
35+
36+
TEST(is_wanted) {
3237
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
3338
"systemd.unified_cgroup_hierarchy", 1) >= 0);
34-
test_is_wanted_print(false);
39+
test_is_wanted_print_one(false);
3540

3641
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
3742
"systemd.unified_cgroup_hierarchy=0", 1) >= 0);
38-
test_is_wanted_print(false);
43+
test_is_wanted_print_one(false);
3944

4045
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
4146
"systemd.unified_cgroup_hierarchy=0 "
4247
"systemd.legacy_systemd_cgroup_controller", 1) >= 0);
43-
test_is_wanted_print(false);
48+
test_is_wanted_print_one(false);
4449

4550
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
4651
"systemd.unified_cgroup_hierarchy=0 "
4752
"systemd.legacy_systemd_cgroup_controller=0", 1) >= 0);
48-
test_is_wanted_print(false);
53+
test_is_wanted_print_one(false);
4954

5055
/* cgroup_no_v1=all implies unified cgroup hierarchy, unless otherwise
5156
* explicitly specified. */
5257
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
5358
"cgroup_no_v1=all", 1) >= 0);
54-
test_is_wanted_print(false);
59+
test_is_wanted_print_one(false);
5560

5661
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
5762
"cgroup_no_v1=all "
5863
"systemd.unified_cgroup_hierarchy=0", 1) >= 0);
59-
test_is_wanted_print(false);
64+
test_is_wanted_print_one(false);
6065
}
6166

62-
int main(void) {
63-
test_setup_logging(LOG_DEBUG);
64-
65-
if (access("/proc/cmdline", R_OK) < 0 && ERRNO_IS_PRIVILEGE(errno))
66-
return log_tests_skipped("can't read /proc/cmdline");
67-
68-
test_is_wanted_print(true);
69-
test_is_wanted_print(false); /* run twice to test caching */
70-
test_is_wanted();
71-
72-
return 0;
73-
}
67+
DEFINE_CUSTOM_TEST_MAIN(
68+
LOG_DEBUG,
69+
({
70+
if (access("/proc/cmdline", R_OK) < 0 && ERRNO_IS_PRIVILEGE(errno))
71+
return log_tests_skipped("can't read /proc/cmdline");
72+
}),
73+
/* no outro */);

src/test/test-chown-rec.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static bool has_xattr(const char *p) {
4040
return true;
4141
}
4242

43-
static void test_chown_recursive(void) {
43+
TEST(chown_recursive) {
4444
_cleanup_(rm_rf_physical_and_freep) char *t = NULL;
4545
struct stat st;
4646
const char *p;
@@ -149,13 +149,10 @@ static void test_chown_recursive(void) {
149149
assert_se(!has_xattr(p));
150150
}
151151

152-
int main(int argc, char *argv[]) {
153-
test_setup_logging(LOG_DEBUG);
154-
155-
if (geteuid() != 0)
156-
return log_tests_skipped("not running as root");
157-
158-
test_chown_recursive();
159-
160-
return EXIT_SUCCESS;
161-
}
152+
DEFINE_CUSTOM_TEST_MAIN(
153+
LOG_DEBUG,
154+
({
155+
if (geteuid() != 0)
156+
return log_tests_skipped("not running as root");
157+
}),
158+
/* no outro */);

src/test/test-format-table.c

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
#include "format-table.h"
77
#include "string-util.h"
88
#include "strv.h"
9+
#include "tests.h"
910
#include "time-util.h"
1011

11-
static void test_issue_9549(void) {
12+
TEST(issue_9549) {
1213
_cleanup_(table_unrefp) Table *table = NULL;
1314
_cleanup_free_ char *formatted = NULL;
1415

15-
log_info("/* %s */", __func__);
16-
1716
assert_se(table = table_new("name", "type", "ro", "usage", "created", "modified"));
1817
assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(3), 100) >= 0);
1918
assert_se(table_add_many(table,
@@ -34,12 +33,10 @@ static void test_issue_9549(void) {
3433
));
3534
}
3635

37-
static void test_multiline(void) {
36+
TEST(multiline) {
3837
_cleanup_(table_unrefp) Table *table = NULL;
3938
_cleanup_free_ char *formatted = NULL;
4039

41-
log_info("/* %s */", __func__);
42-
4340
assert_se(table = table_new("foo", "bar"));
4441

4542
assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0);
@@ -148,12 +145,10 @@ static void test_multiline(void) {
148145
formatted = mfree(formatted);
149146
}
150147

151-
static void test_strv(void) {
148+
TEST(strv) {
152149
_cleanup_(table_unrefp) Table *table = NULL;
153150
_cleanup_free_ char *formatted = NULL;
154151

155-
log_info("/* %s */", __func__);
156-
157152
assert_se(table = table_new("foo", "bar"));
158153

159154
assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0);
@@ -262,12 +257,10 @@ static void test_strv(void) {
262257
formatted = mfree(formatted);
263258
}
264259

265-
static void test_strv_wrapped(void) {
260+
TEST(strv_wrapped) {
266261
_cleanup_(table_unrefp) Table *table = NULL;
267262
_cleanup_free_ char *formatted = NULL;
268263

269-
log_info("/* %s */", __func__);
270-
271264
assert_se(table = table_new("foo", "bar"));
272265

273266
assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0);
@@ -366,12 +359,10 @@ static void test_strv_wrapped(void) {
366359
formatted = mfree(formatted);
367360
}
368361

369-
static void test_json(void) {
362+
TEST(json) {
370363
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
371364
_cleanup_(table_unrefp) Table *t = NULL;
372365

373-
log_info("/* %s */", __func__);
374-
375366
assert_se(t = table_new("foo bar", "quux", "piep miau"));
376367
assert_se(table_set_json_field_name(t, 2, "zzz") >= 0);
377368

@@ -401,13 +392,10 @@ static void test_json(void) {
401392
assert_se(json_variant_equal(v, w));
402393
}
403394

404-
int main(int argc, char *argv[]) {
395+
TEST(table) {
405396
_cleanup_(table_unrefp) Table *t = NULL;
406397
_cleanup_free_ char *formatted = NULL;
407398

408-
assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
409-
assert_se(setenv("COLUMNS", "40", 1) >= 0);
410-
411399
assert_se(t = table_new("one", "two", "three"));
412400

413401
assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0);
@@ -539,12 +527,12 @@ int main(int argc, char *argv[]) {
539527
" yes fäää yes fäää fäää\n"
540528
" yes xxx yes xxx xxx\n"
541529
"5min 5min \n"));
542-
543-
test_issue_9549();
544-
test_multiline();
545-
test_strv();
546-
test_strv_wrapped();
547-
test_json();
548-
549-
return 0;
550530
}
531+
532+
DEFINE_CUSTOM_TEST_MAIN(
533+
LOG_INFO,
534+
({
535+
assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
536+
assert_se(setenv("COLUMNS", "40", 1) >= 0);
537+
}),
538+
/* no outro */);

0 commit comments

Comments
 (0)