Skip to content

Commit fa1a53d

Browse files
kuba-moodavem330
authored andcommitted
selftests: kselftest: add ksft_test_result_code(), handling all exit codes
For generic test harness code it's more useful to deal with exit codes directly, rather than having to switch on them and call the right ksft_test_result_*() helper. Add such function to kselftest.h. Note that "directive" and "diagnostic" are what ktap docs call those parts of the message. Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 796a344 commit fa1a53d

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

tools/testing/selftests/kselftest.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* ksft_test_result_skip(fmt, ...);
2626
* ksft_test_result_xfail(fmt, ...);
2727
* ksft_test_result_error(fmt, ...);
28+
* ksft_test_result_code(exit_code, test_name, fmt, ...);
2829
*
2930
* When all tests are finished, clean up and exit the program with one of:
3031
*
@@ -254,6 +255,44 @@ static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
254255
va_end(args);
255256
}
256257

258+
static inline __printf(2, 3)
259+
void ksft_test_result_code(int exit_code, const char *msg, ...)
260+
{
261+
const char *tap_code = "ok";
262+
const char *directive = "";
263+
int saved_errno = errno;
264+
va_list args;
265+
266+
switch (exit_code) {
267+
case KSFT_PASS:
268+
ksft_cnt.ksft_pass++;
269+
break;
270+
case KSFT_XFAIL:
271+
directive = " # XFAIL ";
272+
ksft_cnt.ksft_xfail++;
273+
break;
274+
case KSFT_XPASS:
275+
directive = " # XPASS ";
276+
ksft_cnt.ksft_xpass++;
277+
break;
278+
case KSFT_SKIP:
279+
directive = " # SKIP ";
280+
ksft_cnt.ksft_xskip++;
281+
break;
282+
case KSFT_FAIL:
283+
default:
284+
tap_code = "not ok";
285+
ksft_cnt.ksft_fail++;
286+
break;
287+
}
288+
289+
va_start(args, msg);
290+
printf("%s %u%s", tap_code, ksft_test_num(), directive);
291+
errno = saved_errno;
292+
vprintf(msg, args);
293+
va_end(args);
294+
}
295+
257296
static inline int ksft_exit_pass(void)
258297
{
259298
ksft_print_cnts();

tools/testing/selftests/kselftest_harness.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,7 @@ void __run_test(struct __fixture_metadata *f,
11111111
struct __test_metadata *t)
11121112
{
11131113
char test_name[LINE_MAX];
1114+
const char *diagnostic;
11141115

11151116
/* reset test struct */
11161117
t->exit_code = KSFT_PASS;
@@ -1140,9 +1141,13 @@ void __run_test(struct __fixture_metadata *f,
11401141
ksft_print_msg(" %4s %s\n",
11411142
__test_passed(t) ? "OK" : "FAIL", test_name);
11421143

1144+
if (t->results->reason[0])
1145+
diagnostic = t->results->reason;
1146+
else
1147+
diagnostic = "unknown";
1148+
11431149
if (t->exit_code == KSFT_SKIP)
1144-
ksft_test_result_skip("%s\n", t->results->reason[0] ?
1145-
t->results->reason : "unknown");
1150+
ksft_test_result_code(t->exit_code, "%s\n", diagnostic);
11461151
else
11471152
ksft_test_result(__test_passed(t), "%s\n", test_name);
11481153
}

0 commit comments

Comments
 (0)