From: Simon Glass <simon.glass@canonical.com>
Update the 'ut' command to have a -R command option to prevent ut_fail()
and ut_failf() from clearing GD_FLG_RECORD. This is useful when testing
the test framework itself, where error messages need to be captured.
Refactor ut_fail() and ut_failf() to call ut_unsilence_console() instead
of duplicating the flag-clearing logic.
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---
include/test/test.h | 2 ++
test/cmd_ut.c | 8 +++++++-
test/ut.c | 7 ++++---
3 files changed, 13 insertions(+), 4 deletions(-)
@@ -96,6 +96,7 @@ struct ut_arg {
* @args: Parsed argument values for current test
* @arg_count: Number of parsed arguments
* @arg_error: Set if ut_str/int/bool() detects a type mismatch
+ * @keep_record: Preserve console recording when ut_fail() is called
* @priv: Private data for tests to use as needed
*/
struct unit_test_state {
@@ -126,6 +127,7 @@ struct unit_test_state {
struct ut_arg args[UT_MAX_ARGS];
int arg_count;
bool arg_error;
+ bool keep_record;
char priv[UT_PRIV_SIZE];
};
@@ -252,6 +252,7 @@ static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
struct unit_test_state uts;
bool show_suites = false;
bool force_run = false;
+ bool keep_record = false;
int runs_per_text = 1;
struct suite *ste;
char *name;
@@ -276,6 +277,9 @@ static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
if (!strchr(test_insert, ':'))
return CMD_RET_USAGE;
break;
+ case 'R':
+ keep_record = true;
+ break;
case 's':
show_suites = true;
break;
@@ -288,6 +292,7 @@ static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
return CMD_RET_USAGE;
ut_init_state(&uts);
+ uts.keep_record = keep_record;
name = argv[0];
select_name = cmd_arg1(argc, argv);
@@ -333,10 +338,11 @@ static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
}
U_BOOT_LONGHELP(ut,
- "[-rs] [-f] [-I<n>:<one_test>] <suite> [<test> [<args>...]] - run unit tests\n"
+ "[-rs] [-f] [-R] [-I<n>:<one_test>] <suite> [<test> [<args>...]] - run unit tests\n"
" -r<runs> Number of times to run each test\n"
" -f Force 'manual' tests to run as well\n"
" -I Test to run after <n> other tests have run\n"
+ " -R Preserve console recording on test failure\n"
" -s Show all suites with ut info\n"
" <suite> Test suite to run (or comma-separated list)\n"
" <test> Specific test to run (optional)\n"
@@ -23,7 +23,7 @@ DECLARE_GLOBAL_DATA_PTR;
void ut_fail(struct unit_test_state *uts, const char *fname, int line,
const char *func, const char *cond)
{
- gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
+ ut_unsilence_console(uts);
printf("%s:%d, %s(): %s\n", fname, line, func, cond);
uts->cur.fail_count++;
}
@@ -33,7 +33,7 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line,
{
va_list args;
- gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
+ ut_unsilence_console(uts);
printf("%s:%d, %s(): %s: ", fname, line, func, cond);
va_start(args, fmt);
vprintf(fmt, args);
@@ -286,7 +286,8 @@ void ut_silence_console(struct unit_test_state *uts)
void ut_unsilence_console(struct unit_test_state *uts)
{
- gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
+ if (!uts->keep_record)
+ gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
}
void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays)