test: Move dm_extended_scan() to test_pre_run()
[platform/kernel/u-boot.git] / test / test-main.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2021 Google LLC
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #include <common.h>
8 #include <console.h>
9 #include <dm.h>
10 #include <dm/root.h>
11 #include <test/test.h>
12 #include <test/ut.h>
13
14 DECLARE_GLOBAL_DATA_PTR;
15
16 int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
17 {
18         /* DM tests have already done this */
19         if (!(test->flags & UT_TESTF_DM))
20                 uts->start = mallinfo();
21
22         if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
23             (test->flags & UT_TESTF_SCAN_FDT))
24                 ut_assertok(dm_extended_scan(false));
25
26         if (test->flags & UT_TESTF_CONSOLE_REC) {
27                 int ret = console_record_reset_enable();
28
29                 if (ret) {
30                         printf("Skipping: Console recording disabled\n");
31                         return -EAGAIN;
32                 }
33         }
34
35         return 0;
36 }
37
38 int test_post_run(struct unit_test_state *uts, struct unit_test *test)
39 {
40         gd->flags &= ~GD_FLG_RECORD;
41
42         return 0;
43 }
44
45 int ut_run_tests(struct unit_test_state *uts, const char *prefix,
46                  struct unit_test *tests, int count, const char *select_name)
47 {
48         struct unit_test *test;
49         int prefix_len = prefix ? strlen(prefix) : 0;
50         int found = 0;
51
52         for (test = tests; test < tests + count; test++) {
53                 const char *test_name = test->name;
54                 int ret;
55
56                 /* Remove the prefix */
57                 if (prefix && !strncmp(test_name, prefix, prefix_len))
58                         test_name += prefix_len;
59
60                 if (select_name && strcmp(select_name, test_name))
61                         continue;
62                 printf("Test: %s\n", test_name);
63                 found++;
64
65                 ret = test_pre_run(uts, test);
66                 if (ret == -EAGAIN)
67                         continue;
68                 if (ret)
69                         return ret;
70
71                 test->func(uts);
72
73                 ret = test_post_run(uts, test);
74                 if (ret)
75                         return ret;
76         }
77         if (select_name && !found)
78                 return -ENOENT;
79
80         return uts->fail_count ? -EBADF : 0;
81 }
82
83 int ut_run_list(const char *category, const char *prefix,
84                 struct unit_test *tests, int count, const char *select_name)
85 {
86         struct unit_test_state uts = { .fail_count = 0 };
87         int ret;
88
89         if (!select_name)
90                 printf("Running %d %s tests\n", count, category);
91
92         ret = ut_run_tests(&uts, prefix, tests, count, select_name);
93
94         if (ret == -ENOENT)
95                 printf("Test '%s' not found\n", select_name);
96         else
97                 printf("Failures: %d\n", uts.fail_count);
98
99         return ret;
100 }