This repository was archived by the owner on Jan 1, 2026. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed
Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 33"""cppcheck-junit tests."""
44
55import unittest
6+ from xml .etree import ElementTree
67
78from 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+
5468if __name__ == '__main__' :
5569 unittest .main ()
You can’t perform that action at this time.
0 commit comments