| 1382 | confidence)) |
| 1383 | |
| 1384 | def FormatJUnitXML(self): |
| 1385 | num_errors = len(self._junit_errors) |
| 1386 | num_failures = len(self._junit_failures) |
| 1387 | |
| 1388 | testsuite = xml.etree.ElementTree.Element('testsuite') |
| 1389 | testsuite.attrib['errors'] = str(num_errors) |
| 1390 | testsuite.attrib['failures'] = str(num_failures) |
| 1391 | testsuite.attrib['name'] = 'cpplint' |
| 1392 | |
| 1393 | if num_errors == 0 and num_failures == 0: |
| 1394 | testsuite.attrib['tests'] = str(1) |
| 1395 | xml.etree.ElementTree.SubElement(testsuite, 'testcase', name='passed') |
| 1396 | |
| 1397 | else: |
| 1398 | testsuite.attrib['tests'] = str(num_errors + num_failures) |
| 1399 | if num_errors > 0: |
| 1400 | testcase = xml.etree.ElementTree.SubElement(testsuite, 'testcase') |
| 1401 | testcase.attrib['name'] = 'errors' |
| 1402 | error = xml.etree.ElementTree.SubElement(testcase, 'error') |
| 1403 | error.text = '\n'.join(self._junit_errors) |
| 1404 | if num_failures > 0: |
| 1405 | # Group failures by file |
| 1406 | failed_file_order = [] |
| 1407 | failures_by_file = {} |
| 1408 | for failure in self._junit_failures: |
| 1409 | failed_file = failure[0] |
| 1410 | if failed_file not in failed_file_order: |
| 1411 | failed_file_order.append(failed_file) |
| 1412 | failures_by_file[failed_file] = [] |
| 1413 | failures_by_file[failed_file].append(failure) |
| 1414 | # Create a testcase for each file |
| 1415 | for failed_file in failed_file_order: |
| 1416 | failures = failures_by_file[failed_file] |
| 1417 | testcase = xml.etree.ElementTree.SubElement(testsuite, 'testcase') |
| 1418 | testcase.attrib['name'] = failed_file |
| 1419 | failure = xml.etree.ElementTree.SubElement(testcase, 'failure') |
| 1420 | template = '{0}: {1} [{2}] [{3}]' |
| 1421 | texts = [template.format(f[1], f[2], f[3], f[4]) for f in failures] |
| 1422 | failure.text = '\n'.join(texts) |
| 1423 | |
| 1424 | xml_decl = '<?xml version="1.0" encoding="UTF-8" ?>\n' |
| 1425 | return xml_decl + xml.etree.ElementTree.tostring(testsuite, 'utf-8').decode('utf-8') |
| 1426 | |
| 1427 | |
| 1428 | _cpplint_state = _CppLintState() |