|
| 1 | +# Cloned from https://github.com/miki725/xunitmerge |
| 2 | +# to fix a Python3 error. |
| 3 | +# |
| 4 | +# xunitmerge is MIT licensed by Miroslav Shubernetskiy https://github.com/miki725 |
| 5 | +# |
| 6 | +# The MIT License (MIT) |
| 7 | +# |
| 8 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +# of this software and associated documentation files (the "Software"), to deal |
| 10 | +# in the Software without restriction, including without limitation the rights |
| 11 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +# copies of the Software, and to permit persons to whom the Software is |
| 13 | +# furnished to do so, subject to the following conditions: |
| 14 | +# |
| 15 | +# The above copyright notice and this permission notice shall be included in |
| 16 | +# all copies or substantial portions of the Software. |
| 17 | +# |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | +# THE SOFTWARE. |
| 25 | + |
| 26 | +from contextlib import contextmanager |
| 27 | +from xml.etree import ElementTree as etree |
| 28 | +from xml.sax.saxutils import quoteattr |
| 29 | + |
| 30 | +import six |
| 31 | + |
| 32 | + |
| 33 | +CNAME_TAGS = ('system-out', 'skipped', 'error', 'failure') |
| 34 | +CNAME_PATTERN = '<![CDATA[{}]]>' |
| 35 | +TAG_PATTERN = '<{tag}{attrs}>{text}</{tag}>' |
| 36 | + |
| 37 | + |
| 38 | +@contextmanager |
| 39 | +def patch_etree_cname(etree): |
| 40 | + """ |
| 41 | + Patch ElementTree's _serialize_xml function so that it will |
| 42 | + write text as CDATA tag for tags tags defined in CNAME_TAGS. |
| 43 | +
|
| 44 | + >>> import re |
| 45 | + >>> from xml.etree import ElementTree |
| 46 | + >>> xml_string = ''' |
| 47 | + ... <testsuite name="nosetests" tests="1" errors="0" failures="0" skip="0"> |
| 48 | + ... <testcase classname="some.class.Foo" name="test_system_out" time="0.001"> |
| 49 | + ... <system-out>Some output here</system-out> |
| 50 | + ... </testcase> |
| 51 | + ... <testcase classname="some.class.Foo" name="test_skipped" time="0.001"> |
| 52 | + ... <skipped type="unittest.case.SkipTest" message="Skipped">Skipped</skipped> |
| 53 | + ... </testcase> |
| 54 | + ... <testcase classname="some.class.Foo" name="test_error" time="0.001"> |
| 55 | + ... <error type="KeyError" message="Error here">Error here</error> |
| 56 | + ... </testcase> |
| 57 | + ... <testcase classname="some.class.Foo" name="test_failure" time="0.001"> |
| 58 | + ... <failure type="AssertionError" message="Failure here">Failure here</failure> |
| 59 | + ... </testcase> |
| 60 | + ... </testsuite> |
| 61 | + ... ''' |
| 62 | + >>> tree = ElementTree.fromstring(xml_string) |
| 63 | + >>> with patch_etree_cname(ElementTree): |
| 64 | + ... saved = str(ElementTree.tostring(tree)) |
| 65 | + >>> systemout = re.findall(r'(<system-out>.*?</system-out>)', saved)[0] |
| 66 | + >>> print(systemout) |
| 67 | + <system-out><![CDATA[Some output here]]></system-out> |
| 68 | + >>> skipped = re.findall(r'(<skipped.*?</skipped>)', saved)[0] |
| 69 | + >>> print(skipped) |
| 70 | + <skipped message="Skipped" type="unittest.case.SkipTest"><![CDATA[Skipped]]></skipped> |
| 71 | + >>> error = re.findall(r'(<error.*?</error>)', saved)[0] |
| 72 | + >>> print(error) |
| 73 | + <error message="Error here" type="KeyError"><![CDATA[Error here]]></error> |
| 74 | + >>> failure = re.findall(r'(<failure.*?</failure>)', saved)[0] |
| 75 | + >>> print(failure) |
| 76 | + <failure message="Failure here" type="AssertionError"><![CDATA[Failure here]]></failure> |
| 77 | + """ |
| 78 | + original_serialize = etree._serialize_xml |
| 79 | + |
| 80 | + def _serialize_xml(write, elem, *args, **kwargs): |
| 81 | + if elem.tag in CNAME_TAGS: |
| 82 | + attrs = ' '.join( |
| 83 | + ['{}={}'.format(k, quoteattr(v)) |
| 84 | + for k, v in sorted(elem.attrib.items())] |
| 85 | + ) |
| 86 | + attrs = ' ' + attrs if attrs else '' |
| 87 | + text = CNAME_PATTERN.format(elem.text) |
| 88 | + write(TAG_PATTERN.format( |
| 89 | + tag=elem.tag, |
| 90 | + attrs=attrs, |
| 91 | + text=text |
| 92 | + )) |
| 93 | + else: |
| 94 | + original_serialize(write, elem, *args, **kwargs) |
| 95 | + |
| 96 | + etree._serialize_xml = etree._serialize['xml'] = _serialize_xml |
| 97 | + |
| 98 | + yield |
| 99 | + |
| 100 | + etree._serialize_xml = etree._serialize['xml'] = original_serialize |
| 101 | + |
| 102 | + |
| 103 | +def merge_trees(*trees): |
| 104 | + """ |
| 105 | + Merge all given XUnit ElementTrees into a single ElementTree. |
| 106 | + This combines all of the children test-cases and also merges |
| 107 | + all of the metadata of how many tests were executed, etc. |
| 108 | + """ |
| 109 | + first_tree = trees[0] |
| 110 | + first_root = first_tree.getroot() |
| 111 | + |
| 112 | + if len(trees) == 0: |
| 113 | + return first_tree |
| 114 | + |
| 115 | + for tree in trees[1:]: |
| 116 | + root = tree.getroot() |
| 117 | + |
| 118 | + # append children elements (testcases) |
| 119 | + first_root.extend(root.getchildren()) |
| 120 | + |
| 121 | + # combine root attributes which stores the number |
| 122 | + # of executed tests, skipped tests, etc |
| 123 | + for key, value in first_root.attrib.items(): |
| 124 | + if not value.isdigit(): |
| 125 | + continue |
| 126 | + combined = six.text_type(int(value) + int(root.attrib.get(key, '0'))) |
| 127 | + first_root.set(key, combined) |
| 128 | + |
| 129 | + return first_tree |
| 130 | + |
| 131 | + |
| 132 | +def merge_xunit(files, output, callback=None): |
| 133 | + """ |
| 134 | + Merge the given xunit xml files into a single output xml file. |
| 135 | +
|
| 136 | + If callback is not None, it will be called with the merged ElementTree |
| 137 | + before the output file is written (useful for applying other fixes to |
| 138 | + the merged file). This can either modify the element tree in place (and |
| 139 | + return None) or return a completely new ElementTree to be written. |
| 140 | + """ |
| 141 | + trees = [] |
| 142 | + |
| 143 | + for f in files: |
| 144 | + trees.append(etree.parse(f)) |
| 145 | + |
| 146 | + merged = merge_trees(*trees) |
| 147 | + |
| 148 | + if callback is not None: |
| 149 | + result = callback(merged) |
| 150 | + if result is not None: |
| 151 | + merged = result |
| 152 | + |
| 153 | + with patch_etree_cname(etree): |
| 154 | + merged.write(output, encoding='utf-8', xml_declaration=True) |
0 commit comments