[PATCH] D84233: [lit] Escape ANSI control character in xunit output

Alexander Richardson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 21 05:20:47 PDT 2020


arichardson created this revision.
arichardson added a reviewer: yln.
Herald added subscribers: llvm-commits, delcypher.
Herald added a project: LLVM.

The Jenkins JUnit XML parser throws an exception if the output contains
control characters like \x1b (e.g. if the failing test includes some
clang -fcolor-diagnostics output).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84233

Files:
  llvm/utils/lit/lit/reports.py


Index: llvm/utils/lit/lit/reports.py
===================================================================
--- llvm/utils/lit/lit/reports.py
+++ llvm/utils/lit/lit/reports.py
@@ -68,6 +68,18 @@
             file.write('\n')
 
 
+def escape_control_chars(s):
+    escape_dict = {chr(c): '\\x' + hex(c)[2:] for c in range(32) if chr(c) not in ('\t', '\n', '\r')}
+    try:
+        s = s.translate(str.maketrans(escape_dict))
+    except AttributeError:
+        # python 2.7 string.translate() does not support multi-character
+        # replacing, so fall back to multiple str.replace() calls.
+        for k, v in escape_dict.items():
+            s = s.replace(k, v)
+    return s
+
+
 class XunitReport(object):
     def __init__(self, output_file):
         self.output_file = output_file
@@ -114,6 +126,11 @@
             output = test.result.output.replace(']]>', ']]]]><![CDATA[>')
             if isinstance(output, bytes):
                 output.decode("utf-8", 'ignore')
+
+            # The Jenkins JUnit XML parser throws an exception if the output
+            # contains control characters like \x1b (e.g. if there is some
+            # -fcolor-diagnostics output).
+            output = escape_control_chars(output)
             file.write(output)
             file.write(']]></failure>\n</testcase>\n')
         elif test.result.code in self.skipped_codes:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84233.279491.patch
Type: text/x-patch
Size: 1373 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200721/e03ce11a/attachment.bin>


More information about the llvm-commits mailing list