[PATCH -next] selftests: Add color to keywords of results report

From: Zhao Gongyi
Date: Thu Nov 03 2022 - 08:02:00 EST


For ksft_test_result_*()/ksft_exit_*(), we could add color to some
keywords like:

function color
*pass* green
*fail* red
*skip* yellow
*error* magenta

And it is hoped to improve the readability of the output.

Signed-off-by: Zhao Gongyi <zhaogongyi@xxxxxxxxxx>
---
tools/testing/selftests/kselftest.h | 28 ++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h
index 33a0dbd26bd3..8c53a7520578 100644
--- a/tools/testing/selftests/kselftest.h
+++ b/tools/testing/selftests/kselftest.h
@@ -75,6 +75,18 @@
#define KSFT_XPASS 3
#define KSFT_SKIP 4

+#define COLOR_RED "\033[1;31m"
+#define COLOR_GREEN "\033[1;32m"
+#define COLOR_YELLOW "\033[1;33m"
+#define COLOR_MAGENTA "\033[1;35m"
+
+#define RESET "\033[0m"
+
+#define RED(msg) COLOR_RED msg RESET
+#define GREEN(msg) COLOR_GREEN msg RESET
+#define YELLOW(msg) COLOR_YELLOW msg RESET
+#define MAGENTA(msg) COLOR_MAGENTA msg RESET
+
/* counters */
struct ksft_count {
unsigned int ksft_pass;
@@ -152,7 +164,7 @@ static inline void ksft_test_result_pass(const char *msg, ...)
ksft_cnt.ksft_pass++;

va_start(args, msg);
- printf("ok %d ", ksft_test_num());
+ printf(GREEN("ok %d "), ksft_test_num());
errno = saved_errno;
vprintf(msg, args);
va_end(args);
@@ -166,7 +178,7 @@ static inline void ksft_test_result_fail(const char *msg, ...)
ksft_cnt.ksft_fail++;

va_start(args, msg);
- printf("not ok %d ", ksft_test_num());
+ printf(RED("not ok %d "), ksft_test_num());
errno = saved_errno;
vprintf(msg, args);
va_end(args);
@@ -192,7 +204,7 @@ static inline void ksft_test_result_xfail(const char *msg, ...)
ksft_cnt.ksft_xfail++;

va_start(args, msg);
- printf("ok %d # XFAIL ", ksft_test_num());
+ printf(RED("ok %d # XFAIL "), ksft_test_num());
errno = saved_errno;
vprintf(msg, args);
va_end(args);
@@ -206,7 +218,7 @@ static inline void ksft_test_result_skip(const char *msg, ...)
ksft_cnt.ksft_xskip++;

va_start(args, msg);
- printf("ok %d # SKIP ", ksft_test_num());
+ printf(YELLOW("ok %d # SKIP "), ksft_test_num());
errno = saved_errno;
vprintf(msg, args);
va_end(args);
@@ -221,7 +233,7 @@ static inline void ksft_test_result_error(const char *msg, ...)
ksft_cnt.ksft_error++;

va_start(args, msg);
- printf("not ok %d # error ", ksft_test_num());
+ printf(MAGENTA("not ok %d # error "), ksft_test_num());
errno = saved_errno;
vprintf(msg, args);
va_end(args);
@@ -266,7 +278,7 @@ static inline int ksft_exit_fail_msg(const char *msg, ...)
va_list args;

va_start(args, msg);
- printf("Bail out! ");
+ printf(RED("Bail out! "));
errno = saved_errno;
vprintf(msg, args);
va_end(args);
@@ -302,9 +314,9 @@ static inline int ksft_exit_skip(const char *msg, ...)
*/
if (ksft_plan || ksft_test_num()) {
ksft_cnt.ksft_xskip++;
- printf("ok %d # SKIP ", 1 + ksft_test_num());
+ printf(YELLOW("ok %d # SKIP "), 1 + ksft_test_num());
} else {
- printf("1..0 # SKIP ");
+ printf(YELLOW("1..0 # SKIP "));
}
if (msg) {
errno = saved_errno;
--
2.17.1