From 41ed1257d77cfe87bf2747801c473b5b681c62b1 Mon Sep 17 00:00:00 2001
From: Gregory Brail <greg@apigee.com>
Date: Wed, 21 Jan 2015 17:43:56 -0800
Subject: [PATCH] Fix JUnit output so that it can be actually be parsed by
 JUnit and Jenkins. Wrap XML output correctly and post-process failure
 messages to avoid invalid XML characters.

---
 tools/packaging/test262.py | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/tools/packaging/test262.py b/tools/packaging/test262.py
index 9a54818273..b9172b0672 100755
--- a/tools/packaging/test262.py
+++ b/tools/packaging/test262.py
@@ -170,6 +170,15 @@ class TestResult(object):
     if len(err) > 0:
        target.write("--- errors ---  \n %s" % err)
 
+  # This is a way to make the output from the "whitespace" tests into valid XML
+  def SafeFormat(self, msg):
+    try:
+      msg = msg.encode(encoding='ascii', errors='strict')
+      msg = msg.replace('\u000Bx', '?')
+      msg = msg.replace('\u000Cx', '?')
+    except:
+      return 'Output contained invalid characters'
+
   def XmlAssemble(self, result):
     test_name = self.case.GetName()
     test_mode = self.case.GetMode()
@@ -182,9 +191,9 @@ class TestResult(object):
       out = self.stdout.strip().decode('utf-8')
       err = self.stderr.strip().decode('utf-8')
       if len(out) > 0:
-        failureElement.text = out
+        failureElement.text = self.SafeFormat(out)
       if len(err) > 0:
-        failureElement.text = err
+        failureElement.text = self.SafeFormat(err)
       testCaseElement.append(failureElement)
     return testCaseElement
 
@@ -520,7 +529,9 @@ class TestSuite(object):
       self.logf = open(logname, "w")
     if junitfile:
       self.outfile = open(junitfile, "w")
+      TestSuitesElement = xmlj.Element("testsuites")
       TestSuiteElement = xmlj.Element("testsuite")
+      TestSuitesElement.append(TestSuiteElement)
       TestSuiteElement.attrib["name "] = "test262"
       for x in range(len(EXCLUDE_LIST)):
         if self.ShouldRun (unicode(EXCLUDE_LIST[x].encode('utf-8','ignore')), tests):
@@ -538,7 +549,7 @@ class TestSuite(object):
         TestCaseElement = result.XmlAssemble(result)
         TestSuiteElement.append(TestCaseElement)
         if case == cases[len(cases)-1]:
-             xmlj.ElementTree(TestSuiteElement).write(junitfile, "UTF-8")
+             xmlj.ElementTree(TestSuitesElement).write(junitfile, "UTF-8")
       if logname:
         self.WriteLog(result)
       progress.HasRun(result)
-- 
GitLab