test: Pass the test-state into ut_run_list()

Pass this into the function so that callers can inspect the state
afterwards.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2025-01-20 14:25:26 -07:00 committed by Tom Rini
parent 7b576f0847
commit 94b5284398
4 changed files with 51 additions and 23 deletions

View file

@ -147,10 +147,13 @@ void spl_board_init(void)
if (state->run_unittests) { if (state->run_unittests) {
struct unit_test *tests = UNIT_TEST_ALL_START(); struct unit_test *tests = UNIT_TEST_ALL_START();
const int count = UNIT_TEST_ALL_COUNT(); const int count = UNIT_TEST_ALL_COUNT();
struct unit_test_state uts;
int ret; int ret;
ret = ut_run_list("spl", NULL, tests, count, ut_init_state(&uts);
ret = ut_run_list(&uts, "spl", NULL, tests, count,
state->select_unittests, 1, false, NULL); state->select_unittests, 1, false, NULL);
ut_uninit_state(&uts);
/* continue execution into U-Boot */ /* continue execution into U-Boot */
} }
} }

View file

@ -479,12 +479,34 @@ struct unit_test_state *ut_get_state(void);
*/ */
void ut_set_state(struct unit_test_state *uts); void ut_set_state(struct unit_test_state *uts);
/**
* ut_init_state() - Set up a new test state
*
* This must be called before using the test state with ut_run_tests()
*
* @uts: Test state to init
*/
void ut_init_state(struct unit_test_state *uts);
/**
* ut_uninit_state() - Free memory used by test state
*
* This must be called before after the test state with ut_run_tests(). To later
* reuse the test state to run more tests, call test_state_init() first
*
* @uts: Test state to uninit
*/
void ut_uninit_state(struct unit_test_state *uts);
/** /**
* ut_run_tests() - Run a set of tests * ut_run_tests() - Run a set of tests
* *
* This runs the test, handling any preparation and clean-up needed. It prints * This runs the test, handling any preparation and clean-up needed. It prints
* the name of each test before running it. * the name of each test before running it.
* *
* @uts: Unit-test state, which must be ready for use, i.e. ut_init_state()
* has been called. The caller is responsible for calling
* ut_uninit_state() after this function returns
* @category: Category of these tests. This is a string printed at the start to * @category: Category of these tests. This is a string printed at the start to
* announce the the number of tests * announce the the number of tests
* @prefix: String prefix for the tests. Any tests that have this prefix will be * @prefix: String prefix for the tests. Any tests that have this prefix will be
@ -503,8 +525,9 @@ void ut_set_state(struct unit_test_state *uts);
* Pass NULL to disable this * Pass NULL to disable this
* Return: 0 if all tests passed, -1 if any failed * Return: 0 if all tests passed, -1 if any failed
*/ */
int ut_run_list(const char *name, const char *prefix, struct unit_test *tests, int ut_run_list(struct unit_test_state *uts, const char *category,
int count, const char *select_name, int runs_per_test, const char *prefix, struct unit_test *tests, int count,
bool force_run, const char *test_insert); const char *select_name, int runs_per_test, bool force_run,
const char *test_insert);
#endif #endif

View file

@ -21,6 +21,7 @@ int cmd_ut_category(const char *name, const char *prefix,
struct unit_test *tests, int n_ents, struct unit_test *tests, int n_ents,
int argc, char *const argv[]) int argc, char *const argv[])
{ {
struct unit_test_state uts;
const char *test_insert = NULL; const char *test_insert = NULL;
int runs_per_text = 1; int runs_per_text = 1;
bool force_run = false; bool force_run = false;
@ -44,9 +45,11 @@ int cmd_ut_category(const char *name, const char *prefix,
argc--; argc--;
} }
ret = ut_run_list(name, prefix, tests, n_ents, ut_init_state(&uts);
ret = ut_run_list(&uts, name, prefix, tests, n_ents,
cmd_arg1(argc, argv), runs_per_text, force_run, cmd_arg1(argc, argv), runs_per_text, force_run,
test_insert); test_insert);
ut_uninit_state(&uts);
return ret ? CMD_RET_FAILURE : 0; return ret ? CMD_RET_FAILURE : 0;
} }

View file

@ -673,15 +673,15 @@ static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
return uts->fail_count ? -EBADF : 0; return uts->fail_count ? -EBADF : 0;
} }
int ut_run_list(const char *category, const char *prefix, int ut_run_list(struct unit_test_state *uts, const char *category,
struct unit_test *tests, int count, const char *select_name, const char *prefix, struct unit_test *tests, int count,
int runs_per_test, bool force_run, const char *test_insert) const char *select_name, int runs_per_test, bool force_run,
const char *test_insert)
{ {
struct unit_test_state uts; ;
bool has_dm_tests = false; bool has_dm_tests = false;
int ret; int ret;
ut_init_state(&uts);
if (!CONFIG_IS_ENABLED(OF_PLATDATA) && if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
ut_list_has_dm_tests(tests, count, prefix, select_name)) { ut_list_has_dm_tests(tests, count, prefix, select_name)) {
has_dm_tests = true; has_dm_tests = true;
@ -699,32 +699,31 @@ int ut_run_list(const char *category, const char *prefix,
if (!select_name) if (!select_name)
printf("Running %d %s tests\n", count, category); printf("Running %d %s tests\n", count, category);
uts.of_root = gd_of_root(); uts->of_root = gd_of_root();
uts.runs_per_test = runs_per_test; uts->runs_per_test = runs_per_test;
if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) { if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
uts.fdt_size = fdt_totalsize(gd->fdt_blob); uts->fdt_size = fdt_totalsize(gd->fdt_blob);
uts.fdt_copy = os_malloc(uts.fdt_size); uts->fdt_copy = os_malloc(uts->fdt_size);
if (!uts.fdt_copy) { if (!uts->fdt_copy) {
printf("Out of memory for device tree copy\n"); printf("Out of memory for device tree copy\n");
return -ENOMEM; return -ENOMEM;
} }
memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size); memcpy(uts->fdt_copy, gd->fdt_blob, uts->fdt_size);
} }
uts.force_run = force_run; uts->force_run = force_run;
ret = ut_run_tests(&uts, prefix, tests, count, select_name, ret = ut_run_tests(uts, prefix, tests, count, select_name,
test_insert); test_insert);
/* Best efforts only...ignore errors */ /* Best efforts only...ignore errors */
if (has_dm_tests) if (has_dm_tests)
dm_test_restore(uts.of_root); dm_test_restore(uts->of_root);
if (uts.skip_count) if (uts->skip_count)
printf("Skipped: %d, ", uts.skip_count); printf("Skipped: %d, ", uts->skip_count);
if (ret == -ENOENT) if (ret == -ENOENT)
printf("Test '%s' not found\n", select_name); printf("Test '%s' not found\n", select_name);
else else
printf("Failures: %d\n", uts.fail_count); printf("Failures: %d\n", uts->fail_count);
ut_uninit_state(&uts);
return ret; return ret;
} }