Merge pull request #173 from myint/master
Handle "--xml-version=2" in cppcheck-htmlreport
This commit is contained in:
commit
f5593d5dd9
|
@ -6,6 +6,9 @@ script:
|
|||
- mkdir build
|
||||
- make test SRCDIR=build VERIFY=1
|
||||
- ./cppcheck --error-exitcode=1 -Ilib --enable=style --suppress=duplicateBranch -q cli gui lib -igui/test
|
||||
- sudo apt-get update
|
||||
- sudo apt-get install python-pygments
|
||||
- ./htmlreport/test_htmlreport.py
|
||||
notifications:
|
||||
irc:
|
||||
channels:
|
||||
|
|
|
@ -166,30 +166,46 @@ class AnnotateCodeFormatter(HtmlFormatter):
|
|||
|
||||
class CppCheckHandler(XmlContentHandler):
|
||||
"""Parses the cppcheck xml file and produces a list of all its errors."""
|
||||
errors = []
|
||||
|
||||
def __init__(self):
|
||||
XmlContentHandler.__init__(self)
|
||||
self.errors = []
|
||||
self.version = "1"
|
||||
|
||||
def startElement(self, name, attributes):
|
||||
if name == "results":
|
||||
self.version = attributes.get("version", self.version)
|
||||
|
||||
if self.version == '1':
|
||||
self.handleVersion1(name, attributes)
|
||||
else:
|
||||
self.handleVersion2(name, attributes)
|
||||
|
||||
def handleVersion1(self, name, attributes):
|
||||
if name != "error":
|
||||
return
|
||||
|
||||
try:
|
||||
file = attributes["file"]
|
||||
line = int(attributes["line"])
|
||||
except:
|
||||
file = ""
|
||||
line = 0
|
||||
if attributes["id"] != "missingInclude" and attributes["id"] != "toomanyconfigs":
|
||||
sys.stderr.write("ERROR: cppcheck error reported without a file name.\n")
|
||||
|
||||
self.errors.append(
|
||||
{
|
||||
"file" : file,
|
||||
"line" : line,
|
||||
self.errors.append({
|
||||
"file" : attributes.get("file", ""),
|
||||
"line" : int(attributes.get("line"), 0),
|
||||
"id" : attributes["id"],
|
||||
"severity" : attributes["severity"],
|
||||
"msg" : attributes["msg"]
|
||||
})
|
||||
|
||||
def handleVersion2(self, name, attributes):
|
||||
if name == "error":
|
||||
self.errors.append({
|
||||
"id" : attributes["id"],
|
||||
"severity" : attributes["severity"],
|
||||
"msg" : attributes["msg"]
|
||||
})
|
||||
elif name == "location":
|
||||
assert self.errors
|
||||
self.errors[-1]["file"] = attributes["file"]
|
||||
self.errors[-1]["line"] = int(attributes["line"])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Configure all the options this little utility is using.
|
||||
parser = optparse.OptionParser()
|
||||
|
@ -212,7 +228,7 @@ if __name__ == '__main__':
|
|||
# Get the stream that we read cppcheck errors from.
|
||||
stream = sys.stdin
|
||||
if options.file:
|
||||
if os.path.exists(options.file) == False:
|
||||
if not os.path.exists(options.file):
|
||||
parser.error("cppcheck xml file: %s not found." % options.file)
|
||||
stream = open(options.file, "r")
|
||||
|
||||
|
@ -234,7 +250,7 @@ if __name__ == '__main__':
|
|||
for error in contentHandler.errors:
|
||||
filename = error["file"]
|
||||
if filename not in files.keys():
|
||||
files[filename] = { "errors" : [], "htmlfile" : str(file_no) + ".html" }
|
||||
files[filename] = {"errors" : [], "htmlfile" : str(file_no) + ".html"}
|
||||
file_no = file_no + 1
|
||||
files[filename]["errors"].append(error)
|
||||
|
||||
|
|
|
@ -24,9 +24,14 @@ HTML_REPORT_BIN = os.path.join(os.path.abspath(os.path.dirname(__file__)),
|
|||
class TestHTMLReport(unittest.TestCase):
|
||||
|
||||
def testReportError(self):
|
||||
with runCheck(os.path.join(
|
||||
ROOT_DIR,
|
||||
'samples', 'memleak', 'bad.c')) as (report, output_directory):
|
||||
for xml_version in ['1', '2']:
|
||||
self.checkReportError(xml_version)
|
||||
|
||||
def checkReportError(self, xml_version):
|
||||
with runCheck(
|
||||
os.path.join(ROOT_DIR, 'samples', 'memleak', 'bad.c'),
|
||||
xml_version=xml_version
|
||||
) as (report, output_directory):
|
||||
self.assertIn('<html', report)
|
||||
|
||||
self.assertIn('Memory leak:', report)
|
||||
|
@ -42,9 +47,14 @@ class TestHTMLReport(unittest.TestCase):
|
|||
self.assertIn('Memory leak:', detail_contents)
|
||||
|
||||
def testReportNoError(self):
|
||||
with runCheck(os.path.join(
|
||||
ROOT_DIR,
|
||||
'samples', 'memleak', 'good.c')) as (report, output_directory):
|
||||
for xml_version in ['1', '2']:
|
||||
self.checkReportNoError(xml_version)
|
||||
|
||||
def checkReportNoError(self, xml_version):
|
||||
with runCheck(
|
||||
os.path.join(ROOT_DIR, 'samples', 'memleak', 'good.c'),
|
||||
xml_version=xml_version
|
||||
) as (report, output_directory):
|
||||
self.assertIn('<html', report)
|
||||
|
||||
self.assertNotIn('Memory leak:', report)
|
||||
|
@ -55,7 +65,7 @@ class TestHTMLReport(unittest.TestCase):
|
|||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def runCheck(source_file):
|
||||
def runCheck(source_file, xml_version):
|
||||
"""Run cppcheck and cppcheck-htmlreport.
|
||||
|
||||
Yield a tuple containing the resulting HTML report index and the directory
|
||||
|
@ -67,7 +77,8 @@ def runCheck(source_file):
|
|||
|
||||
with open(xml_filename, 'w') as output_file:
|
||||
subprocess.check_call(
|
||||
[CPPCHECK_BIN, '--xml', source_file],
|
||||
[CPPCHECK_BIN, '--xml', source_file,
|
||||
'--xml-version=' + xml_version],
|
||||
stderr=output_file)
|
||||
|
||||
assert os.path.exists(xml_filename)
|
||||
|
|
Loading…
Reference in New Issue