From e190306da0c4a1b7737aebb5b7a7046f563fd001 Mon Sep 17 00:00:00 2001 From: johnthagen Date: Wed, 13 Apr 2016 21:31:09 -0400 Subject: [PATCH] Fixes #2. Handle Cppcheck errors that don't have a location tag. --- README.rst | 5 + cppcheck_junit.py | 11 +- setup.py | 2 +- test.py | 20 ++ tests/cppcheck-out-bad-large.xml | 355 +++++++++++++++++++++ tests/cppcheck-out-no-location-element.xml | 7 + 6 files changed, 397 insertions(+), 3 deletions(-) create mode 100644 tests/cppcheck-out-bad-large.xml create mode 100644 tests/cppcheck-out-no-location-element.xml diff --git a/README.rst b/README.rst index d99ea42..38e6993 100644 --- a/README.rst +++ b/README.rst @@ -60,6 +60,11 @@ output so that CI tools like Bamboo will not fail on the JUnit task. Releases -------- +1.1.2 - 2016-04-13 +^^^^^^^^^^^^^^^^^^ + +Handle ``cppcheck`` errors that don't have a ```` tag. + 1.1.1 - 2016-04-11 ^^^^^^^^^^^^^^^^^^ diff --git a/cppcheck_junit.py b/cppcheck_junit.py index 311b47f..c635405 100644 --- a/cppcheck_junit.py +++ b/cppcheck_junit.py @@ -80,8 +80,15 @@ def parse_cppcheck(file_name): errors = collections.defaultdict(list) for error_element in error_root: location_element = error_element.find('location') # type: ElementTree.Element - error = CppcheckError(file=location_element.get('file'), - line=int(location_element.get('line')), + if location_element is not None: + file = location_element.get('file') + line = int(location_element.get('line')) + else: + file = '' + line = 0 + + error = CppcheckError(file=file, + line=line, message=error_element.get('msg'), severity=error_element.get('severity'), error_id=error_element.get('id'), diff --git a/setup.py b/setup.py index 67c0c28..29cb056 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='cppcheck-junit', - version='1.1.1', + version='1.1.2', description='Converts Cppcheck XML output to JUnit format.', long_description=open('README.rst').read(), diff --git a/test.py b/test.py index 193bde9..306e3e5 100644 --- a/test.py +++ b/test.py @@ -29,6 +29,26 @@ def test_bad(self): self.assertEqual(errors[file1][1].message, "Array 'a[10]' accessed at index 10, which is out of bounds.") + def test_no_location_element(self): + file = '' + errors = parse_cppcheck('tests/cppcheck-out-no-location-element.xml') + + self.assertEqual(len(errors), 1) + error = errors[file][0] + self.assertEqual(error.file, file) + self.assertEqual(error.line, 0) + self.assertEqual( + error.message, + 'Too many #ifdef configurations - cppcheck only checks 12 configurations. ' + 'Use --force to check all configurations. For more details, use ' + '--enable=information.') + self.assertEqual(error.severity, 'information') + + def test_bad_large(self): + errors = parse_cppcheck('tests/cppcheck-out-bad-large.xml') + self.assertEqual(len(errors), 43) + + def test_all(self): file1 = 'bad.cpp' file2 = 'bad2.cpp' diff --git a/tests/cppcheck-out-bad-large.xml b/tests/cppcheck-out-bad-large.xml new file mode 100644 index 0000000..b4b4ab7 --- /dev/null +++ b/tests/cppcheck-out-bad-large.xml @@ -0,0 +1,355 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/cppcheck-out-no-location-element.xml b/tests/cppcheck-out-no-location-element.xml new file mode 100644 index 0000000..6fd53d7 --- /dev/null +++ b/tests/cppcheck-out-no-location-element.xml @@ -0,0 +1,7 @@ + + + + + + +