From 39290e42a4afdb00b3750af0fe68d08ba0531cf3 Mon Sep 17 00:00:00 2001 From: Romain Pelletant Date: Mon, 27 May 2024 16:03:54 +0200 Subject: [PATCH 1/2] addons: misra: add junit export support Add junit export support based on junit-xml Each error in a testcase Signed-off-by: Romain Pelletant --- addons/misra.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/addons/misra.py b/addons/misra.py index 74787f367b4..e6a65e582ef 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -26,6 +26,7 @@ import codecs import string import copy +import junit_xml try: from itertools import izip as zip @@ -1373,6 +1374,9 @@ def __init__(self, settings, stdversion="c89"): self.ruleTexts = dict() self.ruleText_filename = None + self.junit_testcases = [] + self.junit_filehandle = None + # Dictionary of dictionaries for rules to suppress # Dict1 is keyed by rule number in the hundreds format of # Major * 100 + minor. ie Rule 5.2 = (5*100) + 2 @@ -4342,6 +4346,11 @@ def reportError(self, location, num1, num2): if this_violation not in self.existing_violations: self.existing_violations.add(this_violation) cppcheckdata.reportError(location, cppcheck_severity, errmsg, 'misra', errorId, misra_severity) + if self.junit_filehandle: + loc = '[%s:%i]' % (location.file, location.linenr) + tc = junit_xml.TestCase(loc, 'misra', 1.0) + tc.add_error_info(errmsg, loc, errorId) + self.junit_testcases.append(tc) if misra_severity not in self.violations: self.violations[misra_severity] = [] @@ -4887,6 +4896,7 @@ def get_args_parser(): parser.add_argument("-generate-table", help=argparse.SUPPRESS, action="store_true") parser.add_argument("-verify", help=argparse.SUPPRESS, action="store_true") parser.add_argument("--severity", type=str, help="Set a custom severity string, for example 'error' or 'warning'. ") + parser.add_argument("--junit-file-export", type=str, help="Filename to export report as JUnit XML") return parser @@ -4916,6 +4926,16 @@ def main(): print('Fatal error: file is not found: ' + filename) sys.exit(1) + if args.junit_file_export: + filename = os.path.expanduser(args.junit_file_export) + filename = os.path.normpath(filename) + if os.path.isfile(filename): + print('JUnit file already exists and will be overwritten') + try: + checker.junit_filehandle = open(filename, 'w+') + except FileNotFoundError as e: + print(f'An error occurred: {e}') + if args.verify_rule_texts and not args.rule_texts: print("Error: Please specify rule texts file with --rule-texts=") @@ -4993,6 +5013,10 @@ def main(): if args.show_suppressed_rules: checker.showSuppressedRules() + if args.junit_file_export: + ts = junit_xml.TestSuite("MISRA2012_TS", checker.junit_testcases) + junit_xml.TestSuite.to_file(checker.junit_filehandle, [ts], prettyprint=True) + checker.junit_filehandle.close() if __name__ == '__main__': From e38358dada80c4502637040a3a7f7898a105c9ff Mon Sep 17 00:00:00 2001 From: Romain Pelletant Date: Mon, 27 May 2024 16:06:30 +0200 Subject: [PATCH 2/2] requirements: add junit-xml as requirement Add junit-xml as requirement Signed-off-by: Romain Pelletant --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 2ecb430828a..c0ae10ce189 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ pcre,pfultz2/pcre@8.45 -H sha256:d6f7182602a775a7d500a0cedca6449af0400c6493951513046d17615ed0bf11 +junit-xml \ No newline at end of file