Fix typo in getting line number

The closing parenthesis was in the wrong place resulting in the default
value not getting set. This adds a test case.
This commit is contained in:
Steven Myint 2013-10-07 06:35:12 -07:00
parent 6ad30a1c69
commit e183a826c5
4 changed files with 37 additions and 8 deletions

View File

@ -187,7 +187,7 @@ class CppCheckHandler(XmlContentHandler):
self.errors.append({
"file" : attributes.get("file", ""),
"line" : int(attributes.get("line"), 0),
"line" : int(attributes.get("line", 0)),
"id" : attributes["id"],
"severity" : attributes["severity"],
"msg" : attributes["msg"]

7
htmlreport/example.cc Normal file
View File

@ -0,0 +1,7 @@
#include "missing.h"
int main()
{
int x;
x++;
}

8
htmlreport/example.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<results>
Checking example.cc...
<error file="example.cc" line="5" id="unassignedVariable" severity="style" msg="Variable &apos;x&apos; is not assigned a value."/>
<error file="example.cc" line="6" id="uninitvar" severity="error" msg="Uninitialized variable: x"/>
Checking usage of global functions..
<error id="missingInclude" severity="style" msg="Cppcheck cannot find all the include files (use --check-config for details)"/>
</results>

View File

@ -63,9 +63,21 @@ class TestHTMLReport(unittest.TestCase):
self.assertFalse(
os.path.exists(os.path.join(output_directory, '0.html')))
def testMissingInclude(self):
with runCheck(
xml_filename=os.path.join(ROOT_DIR, 'htmlreport', 'example.xml'),
) as (report, output_directory):
self.assertIn('<html', report)
self.assertIn('Uninitialized variable:', report)
self.assertIn('example.cc', report)
self.assertTrue(
os.path.exists(os.path.join(output_directory, '0.html')))
@contextlib.contextmanager
def runCheck(source_file, xml_version):
def runCheck(source_filename=None, xml_version='1', xml_filename=None):
"""Run cppcheck and cppcheck-htmlreport.
Yield a tuple containing the resulting HTML report index and the directory
@ -73,11 +85,13 @@ def runCheck(source_file, xml_version):
"""
output_directory = tempfile.mkdtemp(dir='.')
if xml_filename is None:
assert source_filename
xml_filename = os.path.join(output_directory, 'output.xml')
with open(xml_filename, 'w') as output_file:
subprocess.check_call(
[CPPCHECK_BIN, '--xml', source_file,
[CPPCHECK_BIN, '--xml', source_filename,
'--xml-version=' + xml_version],
stderr=output_file)