-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathsplitUnitTestLog.py
executable file
·269 lines (226 loc) · 8.67 KB
/
splitUnitTestLog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python
from __future__ import print_function
import os
import re
import sys
import time
class LogSplitter(object):
def __init__(self, outFileIn=None, verbIn=False):
self.outFile = sys.stdout
if outFileIn:
print("Summary file:", outFileIn)
self.outFile = open(outFileIn, "w")
self.verbose = verbIn
return
def __del__(self):
self.outFile.close()
return
def setVerbose(self, verbIn=False):
self.verbose = verbIn
return
# --------------------------------------------------------------------------------
def split(self, logFile):
self.outFile.write("going to check " + logFile + "\n")
subsysRe = re.compile("^>> Tests for package ([A-Z].*/[A-Z].*) ran.")
pkgTestStartRe = re.compile('^===== Test "(.*)" ====')
pkgTestEndRe = re.compile(r"^\^\^\^\^ End Test (.*) \^\^\^\^")
pkgTestResultRe = re.compile(".*---> test ([^ ]+) (had ERRORS|succeeded)")
pkgStartRe = re.compile("^>> Entering Package (.*)")
# pkgEndRe = re.compile("^>> Leaving Package (.*)")
pkgEndRe = re.compile("^>> Tests for package (.*) ran.")
pkgSubsysMap = {}
subsysPkgMap = {}
baseDir = os.path.split(logFile)[0]
logDirs = os.path.join(baseDir, "unitTestLogs")
print("logDirs ", logDirs)
if not os.path.exists(logDirs):
os.makedirs(logDirs)
lf = open(logFile, "rb")
lines = lf
startTime = time.time()
nLines = 0
testNames = {}
testLines = {}
pkgLines = {}
results = {}
pkgTests = {}
actPkg = "None"
actTest = "None"
actTstLines = 0
actPkgLines = 0
actLogLines = []
startFound = False
for line in lines:
if sys.version_info[0] == 2:
line = line.decode("ascii", "ignore")
else:
line = line.decode("ascii", errors="ignore")
# write out log to individual log file ...
if startFound and ">> Leaving Package " not in line:
actLogLines.append(line)
nLines += 1
actTstLines += 1
actPkgLines += 1
subsysMatch = subsysRe.match(line)
if subsysMatch:
subsys, pkg = subsysMatch.group(1).split("/")
if pkg not in pkgSubsysMap:
pkgSubsysMap[pkg] = subsys
if subsys in subsysPkgMap:
subsysPkgMap[subsys].append(pkg)
else:
subsysPkgMap[subsys] = [pkg]
pkgStartMatch = pkgStartRe.match(line)
if pkgStartMatch:
pkg = pkgStartMatch.group(1)
actPkg = pkg
pkgTests[pkg] = 0
actPkgLines = 0
startFound = True
pkgEndMatch = pkgEndRe.match(line)
if pkgEndMatch:
pkg = pkgEndMatch.group(1)
if actPkg != pkg:
self.outFile.write(
"pkgEndMatch> package mismatch: pkg found "
+ pkg
+ " actPkg="
+ actPkg
+ "\n"
)
pkgLines[pkg] = actPkgLines
if len(actLogLines) > 2:
actLogDir = os.path.join(logDirs, pkg)
os.makedirs(actLogDir)
actLogFile = open(os.path.join(actLogDir, "unitTest.log"), "w")
actLogFile.write("".join(actLogLines))
actLogFile.close()
actLogLines = []
startFound = False
pkgTestResultMatch = pkgTestResultRe.match(line)
if pkgTestResultMatch: # this seems to only appear if there is an ERROR
tstName = pkgTestResultMatch.group(1)
results[tstName] = pkgTestResultMatch.group(2)
pkgTestStartMatch = pkgTestStartRe.match(line)
if pkgTestStartMatch:
tst = pkgTestStartMatch.group(1)
actTest = tst
actTstLines = 0
pkgTests[actPkg] += 1
if actPkg in testNames:
testNames[actPkg].append(actTest)
else:
testNames[actPkg] = [actTest]
if actTest not in results:
results[actTest] = "succeeded" # set the default, no error seen yet
pkgTestEndMatch = pkgTestEndRe.match(line)
if pkgTestEndMatch:
tst = pkgTestEndMatch.group(1)
if actTest != tst:
self.outFile.write(
"pkgTestEndMatch> test mismatch: tst found "
+ tst
+ " actTest="
+ actTest
+ "\n"
)
testLines[tst] = actTstLines
stopTime = time.time()
lf.close()
self.outFile.write("found a total of " + str(nLines) + " lines in logfile.\n")
self.outFile.write("analysis took " + str(stopTime - startTime) + " sec.\n")
self.outFile.write("total number of tests: " + str(len(list(results.keys()))) + "\n")
nMax = 1000
self.outFile.write("tests with more than " + str(nMax) + " lines of logs:\n")
for pkg, lines in list(testLines.items()):
if lines > nMax:
self.outFile.write(" " + pkg + " : " + str(lines) + "\n")
self.outFile.write("Number of tests for packages: \n")
noTests = 0
nrTests = 0
indent = " "
totalOK = 0
totalFail = 0
unitTestResults = {}
for pkg, nTst in list(pkgTests.items()):
if nTst == 0:
noTests += 1
else:
nrTests += 1
if self.verbose:
self.outFile.write("-" * 80 + "\n")
self.outFile.write(indent + pkg + " : ")
nOK = 0
if self.verbose:
self.outFile.write("\n")
for tNam in testNames[pkg]:
if results[tNam] == "succeeded":
nOK += 1
totalOK += 1
else:
totalFail += 1
if self.verbose:
self.outFile.write(indent * 2 + tNam + " " + results[tNam] + "\n")
if self.verbose:
self.outFile.write(indent + pkg + " : ")
self.outFile.write(
indent
+ str(len(testNames[pkg]))
+ " tests in total, OK:"
+ str(nOK)
+ " fail:"
+ str(len(testNames[pkg]) - nOK)
+ "\n"
)
unitTestResults[pkg] = [testNames[pkg], nOK, len(testNames[pkg]) - nOK]
self.outFile.write(
indent
+ str(nrTests)
+ " packages with tests ("
+ str(float(nrTests) / float(len(list(pkgTests.keys()))))
+ ")\n"
)
self.outFile.write(
indent
+ str(noTests)
+ " packages without tests ("
+ str(float(noTests) / float(len(list(pkgTests.keys()))))
+ ")\n"
)
self.outFile.write(
indent
+ "in total: tests OK : "
+ str(totalOK)
+ " tests FAIL : "
+ str(totalFail)
+ "\n"
)
try:
from pickle import Pickler
resFile = open(baseDir + "/unitTestResults.pkl", "wb")
pklr = Pickler(resFile, protocol=2)
pklr.dump(unitTestResults)
pklr.dump(results)
resFile.close()
print("Successfully pickled results for unit tests ! ")
except Exception as e:
print("ERROR during pickling results for unit tests:", str(e))
return
# ================================================================================
def main():
try:
import argparse
except ImportError:
import archived_argparse as argparse
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--logFile", dest="logFile", required=True)
parser.add_argument("-v", "--verbose", default=False, action="store_true")
parser.add_argument("-s", "--outFile", dest="outFile")
args = parser.parse_args()
logFile = args.logFile
verb = args.verbose
outFile = args.outFile
tls = LogSplitter(outFileIn=outFile, verbIn=verb)
tls.split(logFile)
if __name__ == "__main__":
main()