Skip to content
This repository was archived by the owner on Jan 1, 2026. It is now read-only.

Commit 432319c

Browse files
committed
Handle XML parsing failures and Cppcheck XML version 1 error.
1 parent c5c8c5c commit 432319c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

cppcheck_junit.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ def parse_cppcheck(file_name):
5656
Dict[str, List[CppcheckError]]: Parsed errors grouped by file name.
5757
5858
Raises:
59+
FileNotFoundError: If file_name does not exist.
60+
xml.etree.ElementTree.ParseError: If file_name is not a valid XML file.
5961
ValueError: If unsupported Cppcheck XML version.
6062
"""
6163
root = ElementTree.parse(file_name).getroot() # type: ElementTree.Element
6264

63-
if not int(root.get('version')) == 2:
64-
raise ValueError('Parser only supports Cppcheck XML version 2. Use --xml-version=2')
65+
if (root.get('version') is None or
66+
int(root.get('version')) != 2):
67+
raise ValueError('Parser only supports Cppcheck XML version 2. Use --xml-version=2.')
6568

6669
error_root = root.find('errors')
6770

@@ -123,9 +126,16 @@ def main():
123126

124127
try:
125128
errors = parse_cppcheck(args.input_file)
129+
except ValueError as e:
130+
print(str(e))
131+
return EXIT_FAILURE
126132
except FileNotFoundError as e:
127133
print(str(e))
128134
return EXIT_FAILURE
135+
except ElementTree.ParseError as e:
136+
print('{} is a malformed XML file. Did you use --xml-version=2?\n{}'.format(
137+
args.input_file, e))
138+
return EXIT_FAILURE
129139

130140
if len(errors) > 0:
131141
generate_test_suite(errors, args.output_file)

test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""cppcheck-junit tests."""
44

55
import unittest
6+
from xml.etree import ElementTree
67

78
from cppcheck_junit import parse_cppcheck
89

@@ -51,5 +52,18 @@ def test_all(self):
5152
self.assertEqual(errors[file2][1].message,
5253
"Array 'a[10]' accessed at index 10, which is out of bounds.")
5354

55+
def test_xml_version_1(self):
56+
with self.assertRaises(ValueError):
57+
parse_cppcheck('tests/cppcheck-out-bad-xml-version-1.xml')
58+
59+
def test_file_not_found(self):
60+
with self.assertRaises(FileNotFoundError):
61+
parse_cppcheck('tests/file_does_not_exist.xml')
62+
63+
def test_malformed(self):
64+
with self.assertRaises(ElementTree.ParseError):
65+
parse_cppcheck('tests/cppcheck-out-malformed.xml')
66+
67+
5468
if __name__ == '__main__':
5569
unittest.main()

0 commit comments

Comments
 (0)