Skip to content

Commit 39290e4

Browse files
addons: misra: add junit export support
Add junit export support based on junit-xml Each error in a testcase Signed-off-by: Romain Pelletant <romain.pelletant@gmail.com>
1 parent 51fb56c commit 39290e4

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎addons/misra.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import codecs
2727
import string
2828
import copy
29+
import junit_xml
2930

3031
try:
3132
from itertools import izip as zip
@@ -1373,6 +1374,9 @@ def __init__(self, settings, stdversion="c89"):
13731374
self.ruleTexts = dict()
13741375
self.ruleText_filename = None
13751376

1377+
self.junit_testcases = []
1378+
self.junit_filehandle = None
1379+
13761380
# Dictionary of dictionaries for rules to suppress
13771381
# Dict1 is keyed by rule number in the hundreds format of
13781382
# Major * 100 + minor. ie Rule 5.2 = (5*100) + 2
@@ -4342,6 +4346,11 @@ def reportError(self, location, num1, num2):
43424346
if this_violation not in self.existing_violations:
43434347
self.existing_violations.add(this_violation)
43444348
cppcheckdata.reportError(location, cppcheck_severity, errmsg, 'misra', errorId, misra_severity)
4349+
if self.junit_filehandle:
4350+
loc = '[%s:%i]' % (location.file, location.linenr)
4351+
tc = junit_xml.TestCase(loc, 'misra', 1.0)
4352+
tc.add_error_info(errmsg, loc, errorId)
4353+
self.junit_testcases.append(tc)
43454354

43464355
if misra_severity not in self.violations:
43474356
self.violations[misra_severity] = []
@@ -4887,6 +4896,7 @@ def get_args_parser():
48874896
parser.add_argument("-generate-table", help=argparse.SUPPRESS, action="store_true")
48884897
parser.add_argument("-verify", help=argparse.SUPPRESS, action="store_true")
48894898
parser.add_argument("--severity", type=str, help="Set a custom severity string, for example 'error' or 'warning'. ")
4899+
parser.add_argument("--junit-file-export", type=str, help="Filename to export report as JUnit XML")
48904900
return parser
48914901

48924902

@@ -4916,6 +4926,16 @@ def main():
49164926
print('Fatal error: file is not found: ' + filename)
49174927
sys.exit(1)
49184928

4929+
if args.junit_file_export:
4930+
filename = os.path.expanduser(args.junit_file_export)
4931+
filename = os.path.normpath(filename)
4932+
if os.path.isfile(filename):
4933+
print('JUnit file already exists and will be overwritten')
4934+
try:
4935+
checker.junit_filehandle = open(filename, 'w+')
4936+
except FileNotFoundError as e:
4937+
print(f'An error occurred: {e}')
4938+
49194939

49204940
if args.verify_rule_texts and not args.rule_texts:
49214941
print("Error: Please specify rule texts file with --rule-texts=<file>")
@@ -4993,6 +5013,10 @@ def main():
49935013

49945014
if args.show_suppressed_rules:
49955015
checker.showSuppressedRules()
5016+
if args.junit_file_export:
5017+
ts = junit_xml.TestSuite("MISRA2012_TS", checker.junit_testcases)
5018+
junit_xml.TestSuite.to_file(checker.junit_filehandle, [ts], prettyprint=True)
5019+
checker.junit_filehandle.close()
49965020

49975021

49985022
if __name__ == '__main__':

0 commit comments

Comments
 (0)