Package: python-xmlrunner
Version: 1.5.0-1
Severity: normal
Tags: upstream patch
Forwarded: https://github.com/danielfm/unittest-xml-reporting/issues/48


Following simple test snippet:

---8<--- [cut] ---8<---
#!/usr/bin/env python
import unittest
import xmlrunner

class Foo(unittest.TestCase):
    def testFoo(self):
        self.assertTrue(False, ']]>')

unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
---8<--- [cut] ---8<---

results with unhandled exception:

ValueError: ']]>' not allowed in a CDATA section

Multiple CDATA sections can be added in a manner that will result with 
following final output:

---8<--- [cut] ---8<---
        <error message="]]&gt;" type="AssertionError">
<![CDATA[Traceback (most recent call last):
  File "./t.py", line 7, in testFoo
    self.assertTrue(False, ']]]]><![CDATA[>')
AssertionError: ]]]]><![CDATA[>
]]>     </error>
---8<--- [cut] ---8<---



-- System Information:
Debian Release: jessie/sid
  APT prefers unstable
  APT policy: (500, 'unstable')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 3.12-1-amd64 (SMP w/1 CPU core)
Locale: LANG=C, LC_CTYPE=pl_PL.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages python-xmlrunner depends on:
ii  python  2.7.5-5

Versions of packages python-xmlrunner recommends:
pn  python-unittest2  <none>

python-xmlrunner suggests no packages.

-- no debconf information

-- 
zaoszczędzona benzyna napędza motory tak samo dobrze, jak zaoszczędzony cement
wiąże zaoszczędzone cegły
                                        /"Przygoda na Mariensztacie"/
diff --git a/src/xmlrunner/__init__.py b/src/xmlrunner/__init__.py
index 2ce86d5..5aab83a 100644
--- a/src/xmlrunner/__init__.py
+++ b/src/xmlrunner/__init__.py
@@ -268,6 +268,20 @@ class _XMLTestResult(_TextTestResult):
 
     _test_method_name = staticmethod(_test_method_name)
 
+    def _createCDATAsections(xmldoc, node, text):
+        pos = text.find(']]>')
+        while pos >= 0:
+            tmp=text[0:pos+2]
+            cdata = xmldoc.createCDATASection(tmp)
+            node.appendChild(cdata)
+            text=text[pos+2:]
+            pos = text.find(']]>')
+        cdata = xmldoc.createCDATASection(text)
+        node.appendChild(cdata)
+
+    _createCDATAsections = staticmethod(_createCDATAsections)
+
+
     def _report_testcase(suite_name, test_result, xml_testsuite, xml_document):
         """
         Appends a testcase section to the XML document.
@@ -289,8 +303,7 @@ class _XMLTestResult(_TextTestResult):
                 failure.setAttribute('type', test_result.err[0].__name__)
                 failure.setAttribute('message', str(test_result.err[1]))
                 error_info = str(test_result.get_error_info())
-                failureText = xml_document.createCDATASection(error_info)
-                failure.appendChild(failureText)
+                _XMLTestResult._createCDATAsections(xml_document, failure, error_info)
             else:
                 failure.setAttribute('type', 'skip')
                 failure.setAttribute('message', test_result.err)
@@ -305,14 +318,12 @@ class _XMLTestResult(_TextTestResult):
         systemout = xml_document.createElement('system-out')
         xml_testsuite.appendChild(systemout)
 
-        systemout_text = xml_document.createCDATASection(sys.stdout.getvalue())
-        systemout.appendChild(systemout_text)
+        _XMLTestResult._createCDATAsections(xml_document, systemout, sys.stdout.getvalue())
 
         systemerr = xml_document.createElement('system-err')
         xml_testsuite.appendChild(systemerr)
 
-        systemerr_text = xml_document.createCDATASection(sys.stderr.getvalue())
-        systemerr.appendChild(systemerr_text)
+        _XMLTestResult._createCDATAsections(xml_document, systemerr, sys.stderr.getvalue())
 
     _report_output = staticmethod(_report_output)
 

Reply via email to