|
26 | 26 | import codecs |
27 | 27 | import string |
28 | 28 | import copy |
| 29 | +import junit_xml |
29 | 30 |
|
30 | 31 | try: |
31 | 32 | from itertools import izip as zip |
@@ -1373,6 +1374,9 @@ def __init__(self, settings, stdversion="c89"): |
1373 | 1374 | self.ruleTexts = dict() |
1374 | 1375 | self.ruleText_filename = None |
1375 | 1376 |
|
| 1377 | + self.junit_testcases = [] |
| 1378 | + self.junit_filehandle = None |
| 1379 | + |
1376 | 1380 | # Dictionary of dictionaries for rules to suppress |
1377 | 1381 | # Dict1 is keyed by rule number in the hundreds format of |
1378 | 1382 | # Major * 100 + minor. ie Rule 5.2 = (5*100) + 2 |
@@ -4342,6 +4346,11 @@ def reportError(self, location, num1, num2): |
4342 | 4346 | if this_violation not in self.existing_violations: |
4343 | 4347 | self.existing_violations.add(this_violation) |
4344 | 4348 | 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) |
4345 | 4354 |
|
4346 | 4355 | if misra_severity not in self.violations: |
4347 | 4356 | self.violations[misra_severity] = [] |
@@ -4887,6 +4896,7 @@ def get_args_parser(): |
4887 | 4896 | parser.add_argument("-generate-table", help=argparse.SUPPRESS, action="store_true") |
4888 | 4897 | parser.add_argument("-verify", help=argparse.SUPPRESS, action="store_true") |
4889 | 4898 | 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") |
4890 | 4900 | return parser |
4891 | 4901 |
|
4892 | 4902 |
|
@@ -4916,6 +4926,16 @@ def main(): |
4916 | 4926 | print('Fatal error: file is not found: ' + filename) |
4917 | 4927 | sys.exit(1) |
4918 | 4928 |
|
| 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 | + |
4919 | 4939 |
|
4920 | 4940 | if args.verify_rule_texts and not args.rule_texts: |
4921 | 4941 | print("Error: Please specify rule texts file with --rule-texts=<file>") |
@@ -4993,6 +5013,10 @@ def main(): |
4993 | 5013 |
|
4994 | 5014 | if args.show_suppressed_rules: |
4995 | 5015 | 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() |
4996 | 5020 |
|
4997 | 5021 |
|
4998 | 5022 | if __name__ == '__main__': |
|
0 commit comments