]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
test: Pass the test-state into ut_run_list()
authorSimon Glass <sjg@chromium.org>
Mon, 20 Jan 2025 21:25:26 +0000 (14:25 -0700)
committerTom Rini <trini@konsulko.com>
Fri, 24 Jan 2025 20:34:39 +0000 (14:34 -0600)
Pass this into the function so that callers can inspect the state
afterwards.

Signed-off-by: Simon Glass <sjg@chromium.org>
arch/sandbox/cpu/spl.c
include/test/ut.h
test/cmd_ut.c
test/test-main.c

index 7056cfd018064495650be2e77d85a4debaada999..0ad23e4ba95b3aa17e940a8b426fb6cb989255e1 100644 (file)
@@ -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 */
        }
 }
index 2e4d8edf826bebb64c30737b88be6867d6fdea6c..a51defc3b90333c7ad39cebd60688e9b8b2f3f0d 100644 (file)
@@ -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
index a14dbf4ca5e571a872585ff11bfe7adae3f4edf3..69f0230e556bc27082eca398660cbc0d127f8f1d 100644 (file)
@@ -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;
 }
index 871fc1f22b4e2c470ccc1540e83bceefed7b3392..e12c5a95e45d5728684570e38151f9440682b404 100644 (file)
@@ -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;
 }