[PATCH 04/43] test: Pass the test-state into ut_run_list()

Simon Glass sjg at chromium.org
Wed Jan 15 14:30:34 CET 2025


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

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 arch/sandbox/cpu/spl.c |  5 ++++-
 include/test/ut.h      | 29 ++++++++++++++++++++++++++---
 test/cmd_ut.c          |  5 ++++-
 test/test-main.c       | 35 +++++++++++++++++------------------
 4 files changed, 51 insertions(+), 23 deletions(-)

diff --git a/arch/sandbox/cpu/spl.c b/arch/sandbox/cpu/spl.c
index 7056cfd0180..0ad23e4ba95 100644
--- a/arch/sandbox/cpu/spl.c
+++ b/arch/sandbox/cpu/spl.c
@@ -147,10 +147,13 @@ void spl_board_init(void)
 	if (state->run_unittests) {
 		struct unit_test *tests = UNIT_TEST_ALL_START();
 		const int count = UNIT_TEST_ALL_COUNT();
+		struct unit_test_state uts;
 		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);
+		ut_uninit_state(&uts);
 		/* continue execution into U-Boot */
 	}
 }
diff --git a/include/test/ut.h b/include/test/ut.h
index 2e4d8edf826..a51defc3b90 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -479,12 +479,34 @@ struct unit_test_state *ut_get_state(void);
  */
 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
  *
  * This runs the test, handling any preparation and clean-up needed. It prints
  * 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
  *	announce the the number of tests
  * @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
  * 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 count, const char *select_name, int runs_per_test,
-		bool force_run, const char *test_insert);
+int ut_run_list(struct unit_test_state *uts, const char *category,
+		const char *prefix, struct unit_test *tests, int count,
+		const char *select_name, int runs_per_test, bool force_run,
+		const char *test_insert);
 
 #endif
diff --git a/test/cmd_ut.c b/test/cmd_ut.c
index 195b7ea50ac..d25c2c37e7a 100644
--- a/test/cmd_ut.c
+++ b/test/cmd_ut.c
@@ -21,6 +21,7 @@ int cmd_ut_category(const char *name, const char *prefix,
 		    struct unit_test *tests, int n_ents,
 		    int argc, char *const argv[])
 {
+	struct unit_test_state uts;
 	const char *test_insert = NULL;
 	int runs_per_text = 1;
 	bool force_run = false;
@@ -44,9 +45,11 @@ int cmd_ut_category(const char *name, const char *prefix,
 		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,
 			  test_insert);
+	ut_uninit_state(&uts);
 
 	return ret ? CMD_RET_FAILURE : 0;
 }
diff --git a/test/test-main.c b/test/test-main.c
index 871fc1f22b4..e12c5a95e45 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -673,15 +673,15 @@ static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
 	return uts->fail_count ? -EBADF : 0;
 }
 
-int ut_run_list(const char *category, const char *prefix,
-		struct unit_test *tests, int count, const char *select_name,
-		int runs_per_test, bool force_run, const char *test_insert)
+int ut_run_list(struct unit_test_state *uts, const char *category,
+		const char *prefix, struct unit_test *tests, int count,
+		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;
 	int ret;
 
-	ut_init_state(&uts);
 	if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
 	    ut_list_has_dm_tests(tests, count, prefix, select_name)) {
 		has_dm_tests = true;
@@ -699,32 +699,31 @@ int ut_run_list(const char *category, const char *prefix,
 	if (!select_name)
 		printf("Running %d %s tests\n", count, category);
 
-	uts.of_root = gd_of_root();
-	uts.runs_per_test = runs_per_test;
+	uts->of_root = gd_of_root();
+	uts->runs_per_test = runs_per_test;
 	if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
-		uts.fdt_size = fdt_totalsize(gd->fdt_blob);
-		uts.fdt_copy = os_malloc(uts.fdt_size);
-		if (!uts.fdt_copy) {
+		uts->fdt_size = fdt_totalsize(gd->fdt_blob);
+		uts->fdt_copy = os_malloc(uts->fdt_size);
+		if (!uts->fdt_copy) {
 			printf("Out of memory for device tree copy\n");
 			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;
-	ret = ut_run_tests(&uts, prefix, tests, count, select_name,
+	uts->force_run = force_run;
+	ret = ut_run_tests(uts, prefix, tests, count, select_name,
 			   test_insert);
 
 	/* Best efforts only...ignore errors */
 	if (has_dm_tests)
-		dm_test_restore(uts.of_root);
+		dm_test_restore(uts->of_root);
 
-	if (uts.skip_count)
-		printf("Skipped: %d, ", uts.skip_count);
+	if (uts->skip_count)
+		printf("Skipped: %d, ", uts->skip_count);
 	if (ret == -ENOENT)
 		printf("Test '%s' not found\n", select_name);
 	else
-		printf("Failures: %d\n", uts.fail_count);
-	ut_uninit_state(&uts);
+		printf("Failures: %d\n", uts->fail_count);
 
 	return ret;
 }
-- 
2.34.1



More information about the U-Boot mailing list