blob: 7961fd8aa3ed8f86b57fdd83055bc3fbbcfd7236 [file] [log] [blame]
Simon Glass1c721752021-03-07 17:34:47 -07001// 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 <test/test.h>
10
Simon Glassd002a272021-03-07 17:34:48 -070011int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
12{
13 uts->start = mallinfo();
14
15 if (test->flags & UT_TESTF_CONSOLE_REC) {
16 int ret = console_record_reset_enable();
17
18 if (ret) {
19 printf("Skipping: Console recording disabled\n");
20 return -EAGAIN;
21 }
22 }
23
24 return 0;
25}
26
27int test_post_run(struct unit_test_state *uts, struct unit_test *test)
28{
29 return 0;
30}
31
Simon Glass1c721752021-03-07 17:34:47 -070032int ut_run_tests(struct unit_test_state *uts, const char *prefix,
33 struct unit_test *tests, int count, const char *select_name)
34{
35 struct unit_test *test;
36 int prefix_len = prefix ? strlen(prefix) : 0;
37 int found = 0;
38
39 for (test = tests; test < tests + count; test++) {
40 const char *test_name = test->name;
Simon Glassd002a272021-03-07 17:34:48 -070041 int ret;
Simon Glass1c721752021-03-07 17:34:47 -070042
43 /* Remove the prefix */
44 if (prefix && !strncmp(test_name, prefix, prefix_len))
45 test_name += prefix_len;
46
47 if (select_name && strcmp(select_name, test_name))
48 continue;
49 printf("Test: %s\n", test_name);
50 found++;
51
Simon Glassd002a272021-03-07 17:34:48 -070052 ret = test_pre_run(uts, test);
53 if (ret == -EAGAIN)
54 continue;
55 if (ret)
56 return ret;
Simon Glass1c721752021-03-07 17:34:47 -070057
58 test->func(uts);
Simon Glassd002a272021-03-07 17:34:48 -070059
60 ret = test_post_run(uts, test);
61 if (ret)
62 return ret;
Simon Glass1c721752021-03-07 17:34:47 -070063 }
64 if (select_name && !found)
65 return -ENOENT;
66
67 return uts->fail_count ? -EBADF : 0;
68}
69
70int ut_run_list(const char *category, const char *prefix,
71 struct unit_test *tests, int count, const char *select_name)
72{
73 struct unit_test_state uts = { .fail_count = 0 };
74 int ret;
75
76 if (!select_name)
77 printf("Running %d %s tests\n", count, category);
78
79 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
80
81 if (ret == -ENOENT)
82 printf("Test '%s' not found\n", select_name);
83 else
84 printf("Failures: %d\n", uts.fail_count);
85
86 return ret;
87}