test: Run driver-model tests using ut_run_list()
Use this function instead of implementing it separately for driver model.
Make ut_run_tests() private since it is only used in test-main.c
Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/test/test-main.c b/test/test-main.c
index 139fc1f..16c0d13 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -112,6 +112,38 @@
}
/**
+ * test_matches() - Check if a test should be run
+ *
+ * This checks if the a test should be run. In the normal case of running all
+ * tests, @select_name is NULL.
+ *
+ * @prefix: String prefix for the tests. Any tests that have this prefix will be
+ * printed without the prefix, so that it is easier to see the unique part
+ * of the test name. If NULL, no prefix processing is done
+ * @test_name: Name of current test
+ * @select_name: Name of test to run (or NULL for all)
+ * @return true to run this test, false to skip it
+ */
+static bool test_matches(const char *prefix, const char *test_name,
+ const char *select_name)
+{
+ if (!select_name)
+ return true;
+
+ if (!strcmp(test_name, select_name))
+ return true;
+
+ /* All tests have this prefix */
+ if (prefix && !strncmp(test_name, prefix, strlen(prefix)))
+ test_name += strlen(prefix);
+
+ if (!strcmp(test_name, select_name))
+ return true;
+
+ return false;
+}
+
+/**
* test_pre_run() - Handle any preparation needed to run a test
*
* @uts: Test state
@@ -213,8 +245,25 @@
return 0;
}
-int ut_run_test_live_flat(struct unit_test_state *uts, struct unit_test *test,
- const char *name)
+/**
+ * ut_run_test_live_flat() - Run a test with both live and flat tree
+ *
+ * This calls ut_run_test() with livetree enabled, which is the standard setup
+ * for runnig tests. Then, for driver model test, it calls it again with
+ * livetree disabled. This allows checking of flattree being used when OF_LIVE
+ * is enabled, as is the case in U-Boot proper before relocation, as well as in
+ * SPL.
+ *
+ * @uts: Test state to update. The caller should ensure that this is zeroed for
+ * the first call to this function. On exit, @uts->fail_count is
+ * incremented by the number of failures (0, one hopes)
+ * @test: Test to run
+ * @name: Name of test, possibly skipping a prefix that should not be displayed
+ * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
+ * any failed
+ */
+static int ut_run_test_live_flat(struct unit_test_state *uts,
+ struct unit_test *test, const char *name)
{
int runs;
@@ -242,24 +291,39 @@
return 0;
}
-int ut_run_tests(struct unit_test_state *uts, const char *prefix,
- struct unit_test *tests, int count, const char *select_name)
+/**
+ * ut_run_tests() - Run a set of tests
+ *
+ * This runs the tests, handling any preparation and clean-up needed. It prints
+ * the name of each test before running it.
+ *
+ * @uts: Test state to update. The caller should ensure that this is zeroed for
+ * the first call to this function. On exit, @uts->fail_count is
+ * incremented by the number of failures (0, one hopes)
+ * @prefix: String prefix for the tests. Any tests that have this prefix will be
+ * printed without the prefix, so that it is easier to see the unique part
+ * of the test name. If NULL, no prefix processing is done
+ * @tests: List of tests to run
+ * @count: Number of tests to run
+ * @select_name: Name of a single test to run (from the list provided). If NULL
+ * then all tests are run
+ * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
+ * -EBADF if any failed
+ */
+static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
+ struct unit_test *tests, int count,
+ const char *select_name)
{
struct unit_test *test;
- int prefix_len = prefix ? strlen(prefix) : 0;
int found = 0;
for (test = tests; test < tests + count; test++) {
const char *test_name = test->name;
int ret;
- /* Remove the prefix */
- if (prefix && !strncmp(test_name, prefix, prefix_len))
- test_name += prefix_len;
-
- if (select_name && strcmp(select_name, test_name))
+ if (!test_matches(prefix, test_name, select_name))
continue;
- ret = ut_run_test_live_flat(uts, test, test_name);
+ ret = ut_run_test_live_flat(uts, test, select_name);
found++;
if (ret == -EAGAIN)
continue;
@@ -281,6 +345,7 @@
if (!select_name)
printf("Running %d %s tests\n", count, category);
+ uts.of_root = gd_of_root();
ret = ut_run_tests(&uts, prefix, tests, count, select_name);
if (ret == -ENOENT)