From 816aa7e2113cb0981933023e9765dd8776502e12 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 30 Oct 2019 18:01:39 +0100 Subject: [PATCH] Fix and re-enable htmlreport tests (#2310) * htmlreport/check.sh: Remove check with obsolete file gui/test/data/xmlfiles/xmlreport_v1.xml has been removed with commit d95efc44c7ece86bed9b9e6f4874a615bdaf107d * .travis.yml: Enable htmlreport test again * cppcheck-htmlreport: Fall back to guessing lexer from file content If the lexer can not be guessed from the file extension (for example for *.tpp) then guess the lexer that should be used from the content. This fixes "ERROR: *" output when running "htmlreport/check.sh" Also use specific exceptions instead of bare ones. --- .travis.yml | 11 +++++------ htmlreport/check.sh | 1 - htmlreport/cppcheck-htmlreport | 16 ++++++++++------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9aaa5a9d6..9662cefc7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -69,12 +69,11 @@ matrix: - make validateXML # Validate rule files - make validateRules -# note: trusty on travis has python pygments disabled so disable these tests on travis -## check htmlreport stuff -# - ./htmlreport/test_htmlreport.py -# - cd htmlreport -# - ./check.sh -# - cd ../ +# check htmlreport stuff + - ./htmlreport/test_htmlreport.py + - cd htmlreport + - ./check.sh + - cd ../ # check if DESTDIR works TODO: actually execute this - mkdir install_test - echo $CXXFLAGS diff --git a/htmlreport/check.sh b/htmlreport/check.sh index bff1e2ac8..828999fff 100755 --- a/htmlreport/check.sh +++ b/htmlreport/check.sh @@ -1,7 +1,6 @@ #!/bin/bash -ex -./cppcheck-htmlreport --file ../gui/test/data/xmlfiles/xmlreport_v1.xml --title "xml1 test" --report-dir . --source-dir ../test/ ./cppcheck-htmlreport --file ../gui/test/data/xmlfiles/xmlreport_v2.xml --title "xml2 test" --report-dir . --source-dir ../test/ echo -e "\n" diff --git a/htmlreport/cppcheck-htmlreport b/htmlreport/cppcheck-htmlreport index a48f643c4..e2cfef687 100755 --- a/htmlreport/cppcheck-htmlreport +++ b/htmlreport/cppcheck-htmlreport @@ -10,8 +10,9 @@ import operator from collections import Counter from pygments import highlight -from pygments.lexers import guess_lexer_for_filename +from pygments.lexers import guess_lexer, guess_lexer_for_filename from pygments.formatters import HtmlFormatter +from pygments.util import ClassNotFound from xml.sax import parse as xml_parse from xml.sax import SAXParseException as XmlParseException from xml.sax.handler import ContentHandler as XmlContentHandler @@ -523,11 +524,14 @@ if __name__ == '__main__': output_file.write(HTML_HEAD_END) try: lexer = guess_lexer_for_filename(source_filename, '') - except: - sys.stderr.write("ERROR: Couldn't determine lexer for the file' " + source_filename + " '. Won't be able to syntax highlight this file.") - output_file.write("\n Could not generated content because pygments failed to retrieve the determine code type.") - output_file.write("\n Sorry about this.") - continue + except ClassNotFound: + try: + lexer = guess_lexer(content) + except ClassNotFound: + sys.stderr.write("ERROR: Couldn't determine lexer for the file' " + source_filename + " '. Won't be able to syntax highlight this file.") + output_file.write("\n Could not generated content because pygments failed to retrieve the determine code type.") + output_file.write("\n Sorry about this.") + continue if options.source_encoding: lexer.encoding = options.source_encoding