report fragment: strip ansi colors from junit XML

Jenkins does support showing ANSI colors on the web, but apparently not
in the junit results output. Strip ansi colors from report fragment
<system-out> text, to make it less annoying to read those on jenkins.

Change-Id: I656ecc23bbfd3f25bdf012c890e0c998168844d3
This commit is contained in:
Neels Hofmeyr 2020-12-03 22:45:53 +01:00
parent 9596b210c5
commit 16c8be41c9
2 changed files with 6 additions and 1 deletions

View File

@ -22,6 +22,7 @@ import sys
import time
import traceback
import atexit
import re
from datetime import datetime # we need this for strftime as the one from time doesn't carry microsecond info
from inspect import getframeinfo, stack

View File

@ -41,11 +41,15 @@ if sys.maxunicode >= 0x10000: # not narrow build
invalid_xml_char_ranges_str = ['%s-%s' % (chr(low), chr(high))
for (low, high) in invalid_xml_char_ranges]
invalid_xml_char_ranges_regex = re.compile('[%s]' % ''.join(invalid_xml_char_ranges_str))
ansi_color_re = re.compile('\033[0-9;]{1,4}m')
def escape_xml_invalid_characters(str):
replacement_char = '\uFFFD' # Unicode replacement character
return invalid_xml_char_ranges_regex.sub(replacement_char, escape(str))
def strip_ansi_colors(text):
return ''.join(ansi_color_re.split(text))
def hash_info_to_junit(testsuite, hash_info):
properties = et.SubElement(testsuite, 'properties')
for key, val in hash_info.items():
@ -156,7 +160,7 @@ def suite_to_junit(suite):
if report_fragment.output:
sout = et.SubElement(el, 'system-out')
sout.text = escape_xml_invalid_characters(report_fragment.output)
sout.text = escape_xml_invalid_characters(strip_ansi_colors(report_fragment.output))
testsuite.append(el)
testsuite.set('errors', str(errors))