Skip to content

Commit 3363525

Browse files
authored
Fix #8089 (feature request: Show date/time of check in HTML report) (#7305)
This is to fix the bug: https://trac.cppcheck.net/ticket/8089 Title: feature request: Show date/time of check in HTML report Description: Cppcheck HTML report is a helpful tool to automate checking. It could be extended by the date/time information, when the checking was finished. This date/time information assists developers in fixing issues.
1 parent 2474fad commit 3363525

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

htmlreport/cppcheck-htmlreport

+17-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import os
1111
import re
1212
import sys
1313
import subprocess
14+
import time
1415

1516
from collections import Counter
1617
from pygments import highlight
@@ -470,7 +471,7 @@ def blame_lookup(blame_data, line):
470471
return next((data for start, end, data in blame_data if line >= start and line < end), {})
471472

472473

473-
def tr_str(td_th, line, id, cwe, severity, message, author, author_mail, date, add_author, tr_class=None, htmlfile=None, message_class=None):
474+
def tr_str(td_th, line, id, cwe, severity, message, timestamp, author, author_mail, date, add_author, tr_class=None, htmlfile=None, message_class=None):
474475
ret = ''
475476
if htmlfile:
476477
ret += '<%s><a href="%s#line-%d">%d</a></%s>' % (td_th, htmlfile, line, line, td_th)
@@ -485,6 +486,9 @@ def tr_str(td_th, line, id, cwe, severity, message, author, author_mail, date, a
485486
message_attribute = ''
486487
ret += '<%s%s>%s</%s>' % (td_th, message_attribute, html_escape(message), td_th)
487488

489+
if timestamp:
490+
ret += '<%s>%s</%s>' % (td_th, timestamp, td_th)
491+
488492
for field in add_author:
489493
if field == 'name':
490494
ret += '<%s>%s</%s>' % (td_th, html_escape(author), td_th)
@@ -556,6 +560,7 @@ class CppCheckHandler(XmlContentHandler):
556560
self.errors = []
557561
self.version = '1'
558562
self.versionCppcheck = ''
563+
self.timestamp = ''
559564

560565
def startElement(self, name, attributes):
561566
if name == 'results':
@@ -579,6 +584,7 @@ class CppCheckHandler(XmlContentHandler):
579584
}],
580585
'id': attributes['id'],
581586
'severity': attributes['severity'],
587+
'timestamp': self.timestamp,
582588
'msg': attributes['msg']
583589
})
584590

@@ -592,6 +598,7 @@ class CppCheckHandler(XmlContentHandler):
592598
'line': 0,
593599
'id': attributes['id'],
594600
'severity': attributes['severity'],
601+
'timestamp': self.timestamp,
595602
'msg': attributes['msg'],
596603
'verbose': attributes.get('verbose')
597604
}
@@ -694,6 +701,13 @@ def main() -> None:
694701
try:
695702
contentHandler = CppCheckHandler()
696703
for fname in options.file or [sys.stdin]:
704+
if options.file is not None:
705+
t = os.path.getmtime(fname)
706+
else:
707+
t = time.time()
708+
t_s = time.ctime(t)
709+
if t_s is not None:
710+
contentHandler.timestamp = t_s
697711
xml_parse(fname, contentHandler)
698712
except (XmlParseException, ValueError) as msg:
699713
print('Failed to parse cppcheck xml file: %s' % msg)
@@ -865,7 +879,7 @@ def main() -> None:
865879
output_file.write('\n <table class=\"summaryTable\">')
866880
output_file.write(
867881
'\n %s' %
868-
tr_str('th', 'Line', 'Id', 'CWE', 'Severity', 'Message', 'Author', 'Author mail', 'Date (DD/MM/YYYY)', add_author=add_author_information))
882+
tr_str('th', 'Line', 'Id', 'CWE', 'Severity', 'Message', 'Timestamp', 'Author', 'Author mail', 'Date (DD/MM/YYYY)', add_author=add_author_information))
869883

870884
for filename, data in sorted(files.items()):
871885
file_error = filename in decode_errors or filename.endswith('*')
@@ -905,7 +919,7 @@ def main() -> None:
905919

906920
output_file.write(
907921
'\n %s' %
908-
tr_str('td', line, error["id"], cwe_url, error["severity"], error["msg"],
922+
tr_str('td', line, error["id"], cwe_url, error["severity"], error["msg"], error["timestamp"],
909923
git_blame_dict.get('author', 'Unknown'), git_blame_dict.get('author-mail', '---'),
910924
git_blame_dict.get('author-time', '---'),
911925
tr_class=to_css_selector(error["id"]) + ' sev_' + error["severity"] + ' issue',

test/tools/htmlreport/test_htmlreport.py

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import tempfile
99

1010
import unittest
11+
import time
1112

1213
TEST_TOOLS_DIR = os.path.abspath(os.path.dirname(__file__))
1314
ROOT_DIR = os.path.split(os.path.dirname(os.path.dirname(TEST_TOOLS_DIR)))[0]
@@ -94,6 +95,18 @@ def testAddCheckersReport(self):
9495

9596
output_directory.cleanup()
9697

98+
def testAddTimestamp(self):
99+
with runCheck(
100+
xml_filename=os.path.join(TEST_TOOLS_DIR, 'example.xml'),
101+
) as (report, output_directory):
102+
xml_file = os.path.join(TEST_TOOLS_DIR, 'example.xml')
103+
t = os.path.getmtime(xml_file)
104+
t_s = time.ctime(t)
105+
106+
self.assertIn(t_s, report)
107+
108+
output_directory.cleanup()
109+
97110

98111
@contextlib.contextmanager
99112
def runCheck(source_filename=None, xml_version='1', xml_filename=None, checkers_filename=None):

0 commit comments

Comments
 (0)