| 1554 | self._junit_failures.append((filename, linenum, message, category, confidence)) |
| 1555 | |
| 1556 | def FormatJUnitXML(self): |
| 1557 | num_errors = len(self._junit_errors) |
| 1558 | num_failures = len(self._junit_failures) |
| 1559 | |
| 1560 | testsuite = xml.etree.ElementTree.Element("testsuite") |
| 1561 | testsuite.attrib["errors"] = str(num_errors) |
| 1562 | testsuite.attrib["failures"] = str(num_failures) |
| 1563 | testsuite.attrib["name"] = "cpplint" |
| 1564 | |
| 1565 | if num_errors == 0 and num_failures == 0: |
| 1566 | testsuite.attrib["tests"] = str(1) |
| 1567 | xml.etree.ElementTree.SubElement(testsuite, "testcase", name="passed") |
| 1568 | |
| 1569 | else: |
| 1570 | testsuite.attrib["tests"] = str(num_errors + num_failures) |
| 1571 | if num_errors > 0: |
| 1572 | testcase = xml.etree.ElementTree.SubElement(testsuite, "testcase") |
| 1573 | testcase.attrib["name"] = "errors" |
| 1574 | error = xml.etree.ElementTree.SubElement(testcase, "error") |
| 1575 | error.text = "\n".join(self._junit_errors) |
| 1576 | if num_failures > 0: |
| 1577 | # Group failures by file |
| 1578 | failed_file_order = [] |
| 1579 | failures_by_file = {} |
| 1580 | for failure in self._junit_failures: |
| 1581 | failed_file = failure[0] |
| 1582 | if failed_file not in failed_file_order: |
| 1583 | failed_file_order.append(failed_file) |
| 1584 | failures_by_file[failed_file] = [] |
| 1585 | failures_by_file[failed_file].append(failure) |
| 1586 | # Create a testcase for each file |
| 1587 | for failed_file in failed_file_order: |
| 1588 | failures = failures_by_file[failed_file] |
| 1589 | testcase = xml.etree.ElementTree.SubElement(testsuite, "testcase") |
| 1590 | testcase.attrib["name"] = failed_file |
| 1591 | failure = xml.etree.ElementTree.SubElement(testcase, "failure") |
| 1592 | template = "{0}: {1} [{2}] [{3}]" |
| 1593 | texts = [template.format(f[1], f[2], f[3], f[4]) for f in failures] |
| 1594 | failure.text = "\n".join(texts) |
| 1595 | |
| 1596 | xml_decl = '<?xml version="1.0" encoding="UTF-8" ?>\n' |
| 1597 | return xml_decl + xml.etree.ElementTree.tostring(testsuite, "utf-8").decode("utf-8") |
| 1598 | |
| 1599 | |
| 1600 | _cpplint_state = _CppLintState() |