From 977c44c92316931489151cb9cb334c1120b3a087 Mon Sep 17 00:00:00 2001
From: Alexandre Julliard <julliard@winehq.org>
Date: Fri, 13 Dec 2024 18:40:12 +0100
Subject: [PATCH] winetest: Filter out color escapes for junit output.

---
 programs/winetest/main.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/programs/winetest/main.c b/programs/winetest/main.c
index b84f93f2433..3ded6632cd5 100644
--- a/programs/winetest/main.c
+++ b/programs/winetest/main.c
@@ -902,6 +902,24 @@ static void report_test_start( struct wine_test *test, const char *subtest, cons
     xprintf( "%s:%s start %s\n", test->name, subtest, file );
 }
 
+/* filter out color escapes and null characters from test output data */
+static void *filter_data( const char *data, DWORD size )
+{
+    DWORD i, j;
+    char *ret = malloc( size + 1 );
+
+    for (i = j = 0; i < size; i++)
+    {
+        if (data[i] == '\x1b' && data[i + 1] == '[')
+        {
+            while (data[i] && data[i] != 'm') i++;
+        }
+        else if (data[i]) ret[j++] = data[i];
+    }
+    ret[j] = 0;
+    return ret;
+}
+
 static void report_test_done( struct wine_test *test, const char *subtest, const char *file, DWORD pid, DWORD ticks,
                               HANDLE out_file, UINT status, const char *data, DWORD size )
 {
@@ -914,8 +932,9 @@ static void report_test_done( struct wine_test *test, const char *subtest, const
         int total = 0, fail_total = 0, skip_total = 0, failures = 0;
         const char *next, *line, *ptr, *dir = strrchr( file, '/' );
         float time, last_time = 0;
+        char *filtered_data = filter_data( data, size );
 
-        for (line = next = data; *line; line = next)
+        for (line = next = filtered_data; *line; line = next)
         {
             int count, todo_count, flaky_count, fail_count, skip_count;
 
@@ -935,7 +954,7 @@ static void report_test_done( struct wine_test *test, const char *subtest, const
         output( junit, "  <testsuite name=\"%s:%s\" file=\"%s\" time=\"%f\" tests=\"%d\" failures=\"%d\" skipped=\"%d\">\n",
                 test->name, subtest, file, ticks / 1000.0, total, fail_total, skip_total );
 
-        for (line = next = data; *line; line = next)
+        for (line = next = filtered_data; *line; line = next)
         {
             struct { const char *pattern; int length; int error; } patterns[] =
             {
@@ -1009,6 +1028,7 @@ static void report_test_done( struct wine_test *test, const char *subtest, const
         }
 
         output( junit, "  </testsuite>\n" );
+        free( filtered_data );
     }
 }
 
@@ -1673,7 +1693,6 @@ int __cdecl main( int argc, char *argv[] )
         }
         if (junit)
         {
-            SetEnvironmentVariableA( "WINETEST_COLOR", "0" );
             SetEnvironmentVariableA( "WINETEST_TIME", "1" );
         }
 
-- 
GitLab