Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 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 <test/test.h> |
| 10 | |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame^] | 11 | int 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 | |
| 27 | int test_post_run(struct unit_test_state *uts, struct unit_test *test) |
| 28 | { |
| 29 | return 0; |
| 30 | } |
| 31 | |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 32 | int 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 Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame^] | 41 | int ret; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 42 | |
| 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 Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame^] | 52 | ret = test_pre_run(uts, test); |
| 53 | if (ret == -EAGAIN) |
| 54 | continue; |
| 55 | if (ret) |
| 56 | return ret; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 57 | |
| 58 | test->func(uts); |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame^] | 59 | |
| 60 | ret = test_post_run(uts, test); |
| 61 | if (ret) |
| 62 | return ret; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 63 | } |
| 64 | if (select_name && !found) |
| 65 | return -ENOENT; |
| 66 | |
| 67 | return uts->fail_count ? -EBADF : 0; |
| 68 | } |
| 69 | |
| 70 | int 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 | } |