FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:dbad57390bd1 1 # -----------------------------------------------------------------------
ram54288 0:dbad57390bd1 2 # Copyright (c) 2016 ARM Limited. All rights reserved.
ram54288 0:dbad57390bd1 3 # SPDX-License-Identifier: Apache-2.0
ram54288 0:dbad57390bd1 4 # Licensed under the Apache License, Version 2.0 (the License); you may
ram54288 0:dbad57390bd1 5 # not use this file except in compliance with the License.
ram54288 0:dbad57390bd1 6 # You may obtain a copy of the License at
ram54288 0:dbad57390bd1 7 #
ram54288 0:dbad57390bd1 8 # http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:dbad57390bd1 9 #
ram54288 0:dbad57390bd1 10 # Unless required by applicable law or agreed to in writing, software
ram54288 0:dbad57390bd1 11 # distributed under the License is distributed on an AS IS BASIS, WITHOUT
ram54288 0:dbad57390bd1 12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:dbad57390bd1 13 # See the License for the specific language governing permissions and
ram54288 0:dbad57390bd1 14 # limitations under the License.
ram54288 0:dbad57390bd1 15 # -----------------------------------------------------------------------
ram54288 0:dbad57390bd1 16
ram54288 0:dbad57390bd1 17 import re
ram54288 0:dbad57390bd1 18 import sys
ram54288 0:dbad57390bd1 19 from tabulate import tabulate
ram54288 0:dbad57390bd1 20
ram54288 0:dbad57390bd1 21 def extract_tags(logLines):
ram54288 0:dbad57390bd1 22
ram54288 0:dbad57390bd1 23 # These are the Unity tag names we are going to look for.
ram54288 0:dbad57390bd1 24 tagNames = [
ram54288 0:dbad57390bd1 25 "UnityTest",
ram54288 0:dbad57390bd1 26 "UnityIgnoredTest",
ram54288 0:dbad57390bd1 27 "UnityResult",
ram54288 0:dbad57390bd1 28 ]
ram54288 0:dbad57390bd1 29
ram54288 0:dbad57390bd1 30 # All tags will end up here.
ram54288 0:dbad57390bd1 31 tags = []
ram54288 0:dbad57390bd1 32
ram54288 0:dbad57390bd1 33 for tagName in tagNames:
ram54288 0:dbad57390bd1 34
ram54288 0:dbad57390bd1 35 # Fetch start and end locations for the start and end markers.
ram54288 0:dbad57390bd1 36
ram54288 0:dbad57390bd1 37 startMatches = re.finditer(re.escape("<***" + tagName + "***>"), logLines)
ram54288 0:dbad57390bd1 38 endMatches = re.finditer(re.escape("</***" + tagName + "***>"), logLines)
ram54288 0:dbad57390bd1 39
ram54288 0:dbad57390bd1 40 startOffsets = [match.end() for match in startMatches]
ram54288 0:dbad57390bd1 41 endOffsets = [match.start() - 1 for match in endMatches]
ram54288 0:dbad57390bd1 42
ram54288 0:dbad57390bd1 43 # If the amount of start and end markers isn't identical, this is an error.
ram54288 0:dbad57390bd1 44 if len(startOffsets) != len(endOffsets):
ram54288 0:dbad57390bd1 45 raise Exception("For tag '" + tagName + "', start and end tags do not match!")
ram54288 0:dbad57390bd1 46
ram54288 0:dbad57390bd1 47 # Append all found tags to the tags list.
ram54288 0:dbad57390bd1 48 for tagOffsets in zip(startOffsets, endOffsets):
ram54288 0:dbad57390bd1 49 tagContent = logLines[tagOffsets[0]: tagOffsets[1] + 1]
ram54288 0:dbad57390bd1 50 tags.append((tagOffsets[0], tagName, tagContent))
ram54288 0:dbad57390bd1 51
ram54288 0:dbad57390bd1 52 # Sort the tags list by the offset.
ram54288 0:dbad57390bd1 53 tags.sort(key=lambda tag_: tag_[0])
ram54288 0:dbad57390bd1 54
ram54288 0:dbad57390bd1 55 # Throw away the offset (once sorted, it is no longer needed).
ram54288 0:dbad57390bd1 56 tags = [tag[1:] for tag in tags]
ram54288 0:dbad57390bd1 57
ram54288 0:dbad57390bd1 58 # At this point we are left with list of tags, each one a pair, consisting of
ram54288 0:dbad57390bd1 59 # (group name, test name, status).
ram54288 0:dbad57390bd1 60 return tags
ram54288 0:dbad57390bd1 61
ram54288 0:dbad57390bd1 62 def get_test_group_and_name(printableName):
ram54288 0:dbad57390bd1 63 match = re.match(r"TEST\((.*), (.*)\)", printableName)
ram54288 0:dbad57390bd1 64 if match is not None:
ram54288 0:dbad57390bd1 65 return match.group(1), match.group(2)
ram54288 0:dbad57390bd1 66 else:
ram54288 0:dbad57390bd1 67 raise Exception("Erorr parsing test group and name")
ram54288 0:dbad57390bd1 68
ram54288 0:dbad57390bd1 69 def get_ignored_test_group_and_name(printableName):
ram54288 0:dbad57390bd1 70 match = re.match(r"IGNORE_TEST\((.*), (.*)\)", printableName)
ram54288 0:dbad57390bd1 71 if match is not None:
ram54288 0:dbad57390bd1 72 return match.group(1), match.group(2)
ram54288 0:dbad57390bd1 73 else:
ram54288 0:dbad57390bd1 74 raise Exception("Erorr parsing test group and name")
ram54288 0:dbad57390bd1 75
ram54288 0:dbad57390bd1 76 def collect_tests(tags):
ram54288 0:dbad57390bd1 77
ram54288 0:dbad57390bd1 78 tests = []
ram54288 0:dbad57390bd1 79
ram54288 0:dbad57390bd1 80 # Build the list of tests, with status for each.
ram54288 0:dbad57390bd1 81 curTest = ""
ram54288 0:dbad57390bd1 82 resultHandled = False
ram54288 0:dbad57390bd1 83 for tag in tags:
ram54288 0:dbad57390bd1 84
ram54288 0:dbad57390bd1 85 tagName = tag[0]
ram54288 0:dbad57390bd1 86 tagValue = tag[1]
ram54288 0:dbad57390bd1 87
ram54288 0:dbad57390bd1 88 if tagName == "UnityTest":
ram54288 0:dbad57390bd1 89 curTest = get_test_group_and_name(tagValue)
ram54288 0:dbad57390bd1 90 resultHandled = False
ram54288 0:dbad57390bd1 91 elif tagName == "UnityResult":
ram54288 0:dbad57390bd1 92 if not resultHandled:
ram54288 0:dbad57390bd1 93 tests.append((curTest, tagValue))
ram54288 0:dbad57390bd1 94 resultHandled = True
ram54288 0:dbad57390bd1 95 elif tagName == "UnityIgnoredTest":
ram54288 0:dbad57390bd1 96 curTest = get_ignored_test_group_and_name(tagValue)
ram54288 0:dbad57390bd1 97 tests.append((curTest, "IGNORE"))
ram54288 0:dbad57390bd1 98 else:
ram54288 0:dbad57390bd1 99 raise Exception("Unknown tag '" + tagName + "' encountered")
ram54288 0:dbad57390bd1 100
ram54288 0:dbad57390bd1 101 return tests
ram54288 0:dbad57390bd1 102
ram54288 0:dbad57390bd1 103 def generate_junit_xml_output(packageName, tests, xmlOutFile):
ram54288 0:dbad57390bd1 104
ram54288 0:dbad57390bd1 105 testsCount = len(tests)
ram54288 0:dbad57390bd1 106
ram54288 0:dbad57390bd1 107 with open(xmlOutFile, "wt") as f:
ram54288 0:dbad57390bd1 108 print >> f, '<testsuite tests="' + str(testsCount) + '">'
ram54288 0:dbad57390bd1 109 for test in tests:
ram54288 0:dbad57390bd1 110 print >> f, ' <testcase classname="' + packageName + "." + test[0][0] + '" name="' + test[0][1] + '">'
ram54288 0:dbad57390bd1 111 if test[1] == "FAIL":
ram54288 0:dbad57390bd1 112 print >> f, ' <failure/>'
ram54288 0:dbad57390bd1 113 if test[1] == "IGNORE":
ram54288 0:dbad57390bd1 114 print >> f, ' <skipped/>'
ram54288 0:dbad57390bd1 115 print >> f, ' </testcase>'
ram54288 0:dbad57390bd1 116 print >> f, '</testsuite>'
ram54288 0:dbad57390bd1 117
ram54288 0:dbad57390bd1 118 def generate_text_output(packageName, tests, textOutFile):
ram54288 0:dbad57390bd1 119
ram54288 0:dbad57390bd1 120 testsCount = len(tests)
ram54288 0:dbad57390bd1 121
ram54288 0:dbad57390bd1 122 failingTests = 0
ram54288 0:dbad57390bd1 123 passedTests = 0
ram54288 0:dbad57390bd1 124 ignoredTests = 0
ram54288 0:dbad57390bd1 125
ram54288 0:dbad57390bd1 126 testsTable = []
ram54288 0:dbad57390bd1 127 for test in tests:
ram54288 0:dbad57390bd1 128 if test[1] == "FAIL":
ram54288 0:dbad57390bd1 129 failingTests += 1
ram54288 0:dbad57390bd1 130 if test[1] == "PASS":
ram54288 0:dbad57390bd1 131 passedTests += 1
ram54288 0:dbad57390bd1 132 if test[1] == "IGNORE":
ram54288 0:dbad57390bd1 133 ignoredTests += 1
ram54288 0:dbad57390bd1 134
ram54288 0:dbad57390bd1 135 testsTable.append([test[0][0], test[0][1], test[1]])
ram54288 0:dbad57390bd1 136
ram54288 0:dbad57390bd1 137 resultsTableHeader = ["TOTAL", "PASS", "FAIL", "IGNORE"]
ram54288 0:dbad57390bd1 138 resultsTable = [[str(testsCount), str(passedTests), str(failingTests), str(ignoredTests)]]
ram54288 0:dbad57390bd1 139
ram54288 0:dbad57390bd1 140 with open(textOutFile, "wt") as f:
ram54288 0:dbad57390bd1 141
ram54288 0:dbad57390bd1 142 print >> f, "==== " + packageName + " ===="
ram54288 0:dbad57390bd1 143 print >> f, tabulate(testsTable)
ram54288 0:dbad57390bd1 144 print >> f, tabulate(resultsTable, headers=resultsTableHeader)
ram54288 0:dbad57390bd1 145
ram54288 0:dbad57390bd1 146 if testsCount == 0 or failingTests > 0:
ram54288 0:dbad57390bd1 147 finalStatus = "FAIL"
ram54288 0:dbad57390bd1 148 else:
ram54288 0:dbad57390bd1 149 finalStatus = "PASS"
ram54288 0:dbad57390bd1 150 print >> f
ram54288 0:dbad57390bd1 151 print >> f, "Final status: " + finalStatus + "."
ram54288 0:dbad57390bd1 152
ram54288 0:dbad57390bd1 153 def unity_to_junit(packageName, inputFile, xmlOutFile, textOutFile):
ram54288 0:dbad57390bd1 154
ram54288 0:dbad57390bd1 155 with open(inputFile, "rt") as f:
ram54288 0:dbad57390bd1 156 logLines = f.read()
ram54288 0:dbad57390bd1 157
ram54288 0:dbad57390bd1 158 tags = extract_tags(logLines)
ram54288 0:dbad57390bd1 159 tests = collect_tests(tags)
ram54288 0:dbad57390bd1 160
ram54288 0:dbad57390bd1 161 generate_junit_xml_output(packageName, tests, xmlOutFile)
ram54288 0:dbad57390bd1 162 generate_text_output(packageName, tests, textOutFile)
ram54288 0:dbad57390bd1 163
ram54288 0:dbad57390bd1 164
ram54288 0:dbad57390bd1 165 if __name__ == "__main__":
ram54288 0:dbad57390bd1 166
ram54288 0:dbad57390bd1 167 if len(sys.argv) != 5:
ram54288 0:dbad57390bd1 168 print "Usage: <package-name> <input-file> <xml-out-file> <text-out-file>"
ram54288 0:dbad57390bd1 169 sys.exit(1)
ram54288 0:dbad57390bd1 170
ram54288 0:dbad57390bd1 171 unity_to_junit(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
ram54288 0:dbad57390bd1 172