Skip to content

Commit 816aa7e

Browse files
authored
Fix and re-enable htmlreport tests (danmar#2310)
* htmlreport/check.sh: Remove check with obsolete file gui/test/data/xmlfiles/xmlreport_v1.xml has been removed with commit d95efc4 * .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.
1 parent 694d147 commit 816aa7e

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

.travis.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ matrix:
6969
- make validateXML
7070
# Validate rule files
7171
- make validateRules
72-
# note: trusty on travis has python pygments disabled so disable these tests on travis
73-
## check htmlreport stuff
74-
# - ./htmlreport/test_htmlreport.py
75-
# - cd htmlreport
76-
# - ./check.sh
77-
# - cd ../
72+
# check htmlreport stuff
73+
- ./htmlreport/test_htmlreport.py
74+
- cd htmlreport
75+
- ./check.sh
76+
- cd ../
7877
# check if DESTDIR works TODO: actually execute this
7978
- mkdir install_test
8079
- echo $CXXFLAGS

htmlreport/check.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/bash -ex
22

33

4-
./cppcheck-htmlreport --file ../gui/test/data/xmlfiles/xmlreport_v1.xml --title "xml1 test" --report-dir . --source-dir ../test/
54
./cppcheck-htmlreport --file ../gui/test/data/xmlfiles/xmlreport_v2.xml --title "xml2 test" --report-dir . --source-dir ../test/
65
echo -e "\n"
76

htmlreport/cppcheck-htmlreport

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import operator
1010

1111
from collections import Counter
1212
from pygments import highlight
13-
from pygments.lexers import guess_lexer_for_filename
13+
from pygments.lexers import guess_lexer, guess_lexer_for_filename
1414
from pygments.formatters import HtmlFormatter
15+
from pygments.util import ClassNotFound
1516
from xml.sax import parse as xml_parse
1617
from xml.sax import SAXParseException as XmlParseException
1718
from xml.sax.handler import ContentHandler as XmlContentHandler
@@ -523,11 +524,14 @@ if __name__ == '__main__':
523524
output_file.write(HTML_HEAD_END)
524525
try:
525526
lexer = guess_lexer_for_filename(source_filename, '')
526-
except:
527-
sys.stderr.write("ERROR: Couldn't determine lexer for the file' " + source_filename + " '. Won't be able to syntax highlight this file.")
528-
output_file.write("\n <tr><td colspan='5'> Could not generated content because pygments failed to retrieve the determine code type.</td></tr>")
529-
output_file.write("\n <tr><td colspan='5'> Sorry about this.</td></tr>")
530-
continue
527+
except ClassNotFound:
528+
try:
529+
lexer = guess_lexer(content)
530+
except ClassNotFound:
531+
sys.stderr.write("ERROR: Couldn't determine lexer for the file' " + source_filename + " '. Won't be able to syntax highlight this file.")
532+
output_file.write("\n <tr><td colspan='5'> Could not generated content because pygments failed to retrieve the determine code type.</td></tr>")
533+
output_file.write("\n <tr><td colspan='5'> Sorry about this.</td></tr>")
534+
continue
531535

532536
if options.source_encoding:
533537
lexer.encoding = options.source_encoding

0 commit comments

Comments
 (0)