GApplication: Make ::startup run-first
[glib.git] / glib / gtester-report
blob0fcdd9ac3ac8ae900981017a45c240ffc0e4df45
1 #! /usr/bin/env python
2 # GLib Testing Framework Utility -*- Mode: python; -*-
3 # Copyright (C) 2007 Imendio AB
4 # Authors: Tim Janik
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA.
20 import datetime
21 import optparse
22 import sys, re, xml.dom.minidom
24 try:
25 import subunit
26 from subunit import iso8601
27 from testtools.content import Content, ContentType
28 mime_utf8 = ContentType('text', 'plain', {'charset': 'utf8'})
29 except ImportError:
30 subunit = None
33 pkginstall_configvars = {
34 #@PKGINSTALL_CONFIGVARS_IN24LINES@ # configvars are substituted upon script installation
37 # xml utilities
38 def find_child (node, child_name):
39 for child in node.childNodes:
40 if child.nodeName == child_name:
41 return child
42 return None
43 def list_children (node, child_name):
44 rlist = []
45 for child in node.childNodes:
46 if child.nodeName == child_name:
47 rlist += [ child ]
48 return rlist
49 def find_node (node, name = None):
50 if not node or node.nodeName == name or not name:
51 return node
52 for child in node.childNodes:
53 c = find_node (child, name)
54 if c:
55 return c
56 return None
57 def node_as_text (node, name = None):
58 if name:
59 node = find_node (node, name)
60 txt = ''
61 if node:
62 if node.nodeValue:
63 txt += node.nodeValue
64 for child in node.childNodes:
65 txt += node_as_text (child)
66 return txt
67 def attribute_as_text (node, aname, node_name = None):
68 node = find_node (node, node_name)
69 if not node:
70 return ''
71 attr = node.attributes.get (aname, '')
72 if hasattr (attr, 'value'):
73 return attr.value
74 return ''
76 # HTML utilities
77 def html_indent_string (n):
78 uncollapsible_space = '  ' # HTML won't compress alternating sequences of ' ' and ' '
79 string = ''
80 for i in range (0, (n + 1) / 2):
81 string += uncollapsible_space
82 return string
84 # TestBinary object, instantiated per test binary in the log file
85 class TestBinary:
86 def __init__ (self, name):
87 self.name = name
88 self.testcases = []
89 self.duration = 0
90 self.success_cases = 0
91 self.skipped_cases = 0
92 self.file = '???'
93 self.random_seed = ''
95 # base class to handle processing/traversion of XML nodes
96 class TreeProcess:
97 def __init__ (self):
98 self.nest_level = 0
99 def trampoline (self, node):
100 name = node.nodeName
101 if name == '#text':
102 self.handle_text (node)
103 else:
104 try: method = getattr (self, 'handle_' + re.sub ('[^a-zA-Z0-9]', '_', name))
105 except: method = None
106 if method:
107 return method (node)
108 else:
109 return self.process_recursive (name, node)
110 def process_recursive (self, node_name, node):
111 self.process_children (node)
112 def process_children (self, node):
113 self.nest_level += 1
114 for child in node.childNodes:
115 self.trampoline (child)
116 self.nest_level += 1
118 # test report reader, this class collects some statistics and merges duplicate test binary runs
119 class ReportReader (TreeProcess):
120 def __init__ (self):
121 TreeProcess.__init__ (self)
122 self.binary_names = []
123 self.binaries = {}
124 self.last_binary = None
125 self.info = {}
126 def binary_list (self):
127 lst = []
128 for name in self.binary_names:
129 lst += [ self.binaries[name] ]
130 return lst
131 def get_info (self):
132 return self.info
133 def handle_info (self, node):
134 dn = find_child (node, 'package')
135 self.info['package'] = node_as_text (dn)
136 dn = find_child (node, 'version')
137 self.info['version'] = node_as_text (dn)
138 dn = find_child (node, 'revision')
139 if dn is not None:
140 self.info['revision'] = node_as_text (dn)
141 def handle_testcase (self, node):
142 self.last_binary.testcases += [ node ]
143 result = attribute_as_text (node, 'result', 'status')
144 if result == 'success':
145 self.last_binary.success_cases += 1
146 if bool (int (attribute_as_text (node, 'skipped') + '0')):
147 self.last_binary.skipped_cases += 1
148 def handle_text (self, node):
149 pass
150 def handle_testbinary (self, node):
151 path = node.attributes.get ('path', None).value
152 if self.binaries.get (path, -1) == -1:
153 self.binaries[path] = TestBinary (path)
154 self.binary_names += [ path ]
155 self.last_binary = self.binaries[path]
156 dn = find_child (node, 'duration')
157 dur = node_as_text (dn)
158 try: dur = float (dur)
159 except: dur = 0
160 if dur:
161 self.last_binary.duration += dur
162 bin = find_child (node, 'binary')
163 if bin:
164 self.last_binary.file = attribute_as_text (bin, 'file')
165 rseed = find_child (node, 'random-seed')
166 if rseed:
167 self.last_binary.random_seed = node_as_text (rseed)
168 self.process_children (node)
171 class ReportWriter(object):
172 """Base class for reporting."""
174 def __init__(self, binary_list):
175 self.binaries = binary_list
177 def _error_text(self, node):
178 """Get a string representing the error children of node."""
179 rlist = list_children(node, 'error')
180 txt = ''
181 for enode in rlist:
182 txt += node_as_text (enode)
183 if txt and txt[-1] != '\n':
184 txt += '\n'
185 return txt
188 class HTMLReportWriter(ReportWriter):
189 # Javascript/CSS snippet to toggle element visibility
190 cssjs = r'''
191 <style type="text/css" media="screen">
192 .VisibleSection { }
193 .HiddenSection { display: none; }
194 </style>
195 <script language="javascript" type="text/javascript"><!--
196 function toggle_display (parentid, tagtype, idmatch, keymatch) {
197 ptag = document.getElementById (parentid);
198 tags = ptag.getElementsByTagName (tagtype);
199 for (var i = 0; i < tags.length; i++) {
200 tag = tags[i];
201 var key = tag.getAttribute ("keywords");
202 if (tag.id.indexOf (idmatch) == 0 && key && key.match (keymatch)) {
203 if (tag.className.indexOf ("HiddenSection") >= 0)
204 tag.className = "VisibleSection";
205 else
206 tag.className = "HiddenSection";
210 message_array = Array();
211 function view_testlog (wname, file, random_seed, tcase, msgtitle, msgid) {
212 txt = message_array[msgid];
213 var w = window.open ("", // URI
214 wname,
215 "resizable,scrollbars,status,width=790,height=400");
216 var doc = w.document;
217 doc.write ("<h2>File: " + file + "</h2>\n");
218 doc.write ("<h3>Case: " + tcase + "</h3>\n");
219 doc.write ("<strong>Random Seed:</strong> <code>" + random_seed + "</code> <br /><br />\n");
220 doc.write ("<strong>" + msgtitle + "</strong><br />\n");
221 doc.write ("<pre>");
222 doc.write (txt);
223 doc.write ("</pre>\n");
224 doc.write ("<a href=\'javascript:window.close()\'>Close Window</a>\n");
225 doc.close();
227 --></script>
229 def __init__ (self, info, binary_list):
230 ReportWriter.__init__(self, binary_list)
231 self.info = info
232 self.bcounter = 0
233 self.tcounter = 0
234 self.total_tcounter = 0
235 self.total_fcounter = 0
236 self.total_duration = 0
237 self.indent_depth = 0
238 self.lastchar = ''
239 def oprint (self, message):
240 sys.stdout.write (message)
241 if message:
242 self.lastchar = message[-1]
243 def handle_info (self):
244 self.oprint ('<h3>Package: %(package)s, version: %(version)s</h3>\n' % self.info)
245 if self.info['revision']:
246 self.oprint ('<h5>Report generated from: %(revision)s</h5>\n' % self.info)
247 def handle_text (self, node):
248 self.oprint (node.nodeValue)
249 def handle_testcase (self, node, binary):
250 skipped = bool (int (attribute_as_text (node, 'skipped') + '0'))
251 if skipped:
252 return # skipped tests are uninteresting for HTML reports
253 path = attribute_as_text (node, 'path')
254 duration = node_as_text (node, 'duration')
255 result = attribute_as_text (node, 'result', 'status')
256 rcolor = {
257 'success': 'bgcolor="lightgreen"',
258 'failed': 'bgcolor="red"',
259 }.get (result, '')
260 if result != 'success':
261 duration = '-' # ignore bogus durations
262 self.oprint ('<tr id="b%u_t%u_" keywords="%s all" class="HiddenSection">\n' % (self.bcounter, self.tcounter, result))
263 self.oprint ('<td>%s %s</td> <td align="right">%s</td> \n' % (html_indent_string (4), path, duration))
264 perflist = list_children (node, 'performance')
265 if result != 'success':
266 txt = self._error_text(node)
267 txt = re.sub (r'"', r'\\"', txt)
268 txt = re.sub (r'\n', r'\\n', txt)
269 txt = re.sub (r'&', r'&amp;', txt)
270 txt = re.sub (r'<', r'&lt;', txt)
271 self.oprint ('<script language="javascript" type="text/javascript">message_array["b%u_t%u_"] = "%s";</script>\n' % (self.bcounter, self.tcounter, txt))
272 self.oprint ('<td align="center"><a href="javascript:view_testlog (\'%s\', \'%s\', \'%s\', \'%s\', \'Output:\', \'b%u_t%u_\')">Details</a></td>\n' %
273 ('TestResultWindow', binary.file, binary.random_seed, path, self.bcounter, self.tcounter))
274 elif perflist:
275 presults = []
276 for perf in perflist:
277 pmin = bool (int (attribute_as_text (perf, 'minimize')))
278 pmax = bool (int (attribute_as_text (perf, 'maximize')))
279 pval = float (attribute_as_text (perf, 'value'))
280 txt = node_as_text (perf)
281 txt = re.sub (r'&', r'&amp;', txt)
282 txt = re.sub (r'<', r'&gt;', txt)
283 txt = '<strong>Performance(' + (pmin and '<em>minimized</em>' or '<em>maximized</em>') + '):</strong> ' + txt.strip() + '<br />\n'
284 txt = re.sub (r'"', r'\\"', txt)
285 txt = re.sub (r'\n', r'\\n', txt)
286 presults += [ (pval, txt) ]
287 presults.sort()
288 ptxt = ''.join ([e[1] for e in presults])
289 self.oprint ('<script language="javascript" type="text/javascript">message_array["b%u_t%u_"] = "%s";</script>\n' % (self.bcounter, self.tcounter, ptxt))
290 self.oprint ('<td align="center"><a href="javascript:view_testlog (\'%s\', \'%s\', \'%s\', \'%s\', \'Test Results:\', \'b%u_t%u_\')">Details</a></td>\n' %
291 ('TestResultWindow', binary.file, binary.random_seed, path, self.bcounter, self.tcounter))
292 else:
293 self.oprint ('<td align="center">-</td>\n')
294 self.oprint ('<td align="right" %s>%s</td>\n' % (rcolor, result))
295 self.oprint ('</tr>\n')
296 self.tcounter += 1
297 self.total_tcounter += 1
298 self.total_fcounter += result != 'success'
299 def handle_binary (self, binary):
300 self.tcounter = 1
301 self.bcounter += 1
302 self.total_duration += binary.duration
303 self.oprint ('<tr><td><strong>%s</strong></td><td align="right">%f</td> <td align="center">\n' % (binary.name, binary.duration))
304 erlink, oklink = ('', '')
305 real_cases = len (binary.testcases) - binary.skipped_cases
306 if binary.success_cases < real_cases:
307 erlink = 'href="javascript:toggle_display (\'ResultTable\', \'tr\', \'b%u_\', \'failed\')"' % self.bcounter
308 if binary.success_cases:
309 oklink = 'href="javascript:toggle_display (\'ResultTable\', \'tr\', \'b%u_\', \'success\')"' % self.bcounter
310 if real_cases != 0:
311 self.oprint ('<a %s>ER</a>\n' % erlink)
312 self.oprint ('<a %s>OK</a>\n' % oklink)
313 self.oprint ('</td>\n')
314 perc = binary.success_cases * 100.0 / real_cases
315 pcolor = {
316 100 : 'bgcolor="lightgreen"',
317 0 : 'bgcolor="red"',
318 }.get (int (perc), 'bgcolor="yellow"')
319 self.oprint ('<td align="right" %s>%.2f%%</td>\n' % (pcolor, perc))
320 self.oprint ('</tr>\n')
321 else:
322 self.oprint ('Empty\n')
323 self.oprint ('</td>\n')
324 self.oprint ('</tr>\n')
325 for tc in binary.testcases:
326 self.handle_testcase (tc, binary)
327 def handle_totals (self):
328 self.oprint ('<tr>')
329 self.oprint ('<td><strong>Totals:</strong> %u Binaries, %u Tests, %u Failed, %u Succeeded</td>' %
330 (self.bcounter, self.total_tcounter, self.total_fcounter, self.total_tcounter - self.total_fcounter))
331 self.oprint ('<td align="right">%f</td>\n' % self.total_duration)
332 self.oprint ('<td align="center">-</td>\n')
333 if self.total_tcounter != 0:
334 perc = (self.total_tcounter - self.total_fcounter) * 100.0 / self.total_tcounter
335 else:
336 perc = 0.0
337 pcolor = {
338 100 : 'bgcolor="lightgreen"',
339 0 : 'bgcolor="red"',
340 }.get (int (perc), 'bgcolor="yellow"')
341 self.oprint ('<td align="right" %s>%.2f%%</td>\n' % (pcolor, perc))
342 self.oprint ('</tr>\n')
343 def printout (self):
344 self.oprint ('<html><head>\n')
345 self.oprint ('<title>GTester Unit Test Report</title>\n')
346 self.oprint (self.cssjs)
347 self.oprint ('</head>\n')
348 self.oprint ('<body>\n')
349 self.oprint ('<h2>GTester Unit Test Report</h2>\n')
350 self.handle_info ()
351 self.oprint ('<table id="ResultTable" width="100%" border="1">\n<tr>\n')
352 self.oprint ('<th>Program / Testcase </th>\n')
353 self.oprint ('<th style="width:8em">Duration (sec)</th>\n')
354 self.oprint ('<th style="width:5em">View</th>\n')
355 self.oprint ('<th style="width:5em">Result</th>\n')
356 self.oprint ('</tr>\n')
357 for tb in self.binaries:
358 self.handle_binary (tb)
359 self.handle_totals()
360 self.oprint ('</table>\n')
361 self.oprint ('</body>\n')
362 self.oprint ('</html>\n')
365 class SubunitWriter(ReportWriter):
366 """Reporter to output a subunit stream."""
368 def printout(self):
369 reporter = subunit.TestProtocolClient(sys.stdout)
370 for binary in self.binaries:
371 for tc in binary.testcases:
372 test = GTestCase(tc, binary)
373 test.run(reporter)
376 class GTestCase(object):
377 """A representation of a gtester test result as a pyunit TestCase."""
379 def __init__(self, case, binary):
380 """Create a GTestCase for case `case` from binary program `binary`."""
381 self._case = case
382 self._binary = binary
383 # the name of the case - e.g. /dbusmenu/glib/objects/menuitem/props_boolstr
384 self._path = attribute_as_text(self._case, 'path')
386 def id(self):
387 """What test is this? Returns the gtester path for the testcase."""
388 return self._path
390 def _get_details(self):
391 """Calculate a details dict for the test - attachments etc."""
392 details = {}
393 result = attribute_as_text(self._case, 'result', 'status')
394 details['filename'] = Content(mime_utf8, lambda:[self._binary.file])
395 details['random_seed'] = Content(mime_utf8,
396 lambda:[self._binary.random_seed])
397 if self._get_outcome() == 'addFailure':
398 # Extract the error details. Skips have no details because its not
399 # skip like unittest does, instead the runner just bypasses N test.
400 txt = self._error_text(self._case)
401 details['error'] = Content(mime_utf8, lambda:[txt])
402 if self._get_outcome() == 'addSuccess':
403 # Sucessful tests may have performance metrics.
404 perflist = list_children(self._case, 'performance')
405 if perflist:
406 presults = []
407 for perf in perflist:
408 pmin = bool (int (attribute_as_text (perf, 'minimize')))
409 pmax = bool (int (attribute_as_text (perf, 'maximize')))
410 pval = float (attribute_as_text (perf, 'value'))
411 txt = node_as_text (perf)
412 txt = 'Performance(' + (pmin and 'minimized' or 'maximized'
413 ) + '): ' + txt.strip() + '\n'
414 presults += [(pval, txt)]
415 presults.sort()
416 perf_details = [e[1] for e in presults]
417 details['performance'] = Content(mime_utf8, lambda:perf_details)
418 return details
420 def _get_outcome(self):
421 if int(attribute_as_text(self._case, 'skipped') + '0'):
422 return 'addSkip'
423 outcome = attribute_as_text(self._case, 'result', 'status')
424 if outcome == 'success':
425 return 'addSuccess'
426 else:
427 return 'addFailure'
429 def run(self, result):
430 time = datetime.datetime.utcnow().replace(tzinfo=iso8601.Utc())
431 result.time(time)
432 result.startTest(self)
433 try:
434 outcome = self._get_outcome()
435 details = self._get_details()
436 # Only provide a duration IFF outcome == 'addSuccess' - the main
437 # parser claims bogus results otherwise: in that case emit time as
438 # zero perhaps.
439 if outcome == 'addSuccess':
440 duration = float(node_as_text(self._case, 'duration'))
441 duration = duration * 1000000
442 timedelta = datetime.timedelta(0, 0, duration)
443 time = time + timedelta
444 result.time(time)
445 getattr(result, outcome)(self, details=details)
446 finally:
447 result.stopTest(self)
451 # main program handling
452 def parse_opts():
453 """Parse program options.
455 :return: An options object and the program arguments.
457 parser = optparse.OptionParser()
458 parser.version = pkginstall_configvars.get ('glib-version', '0.0-uninstalled')
459 parser.usage = "%prog [OPTIONS] <gtester-log.xml>"
460 parser.description = "Generate HTML reports from the XML log files generated by gtester."
461 parser.epilog = "gtester-report (GLib utils) version %s."% (parser.version,)
462 parser.add_option("-v", "--version", action="store_true", dest="version", default=False,
463 help="Show program version.")
464 parser.add_option("-s", "--subunit", action="store_true", dest="subunit", default=False,
465 help="Output subunit [See https://launchpad.net/subunit/"
466 " Needs python-subunit]")
467 options, files = parser.parse_args()
468 if options.version:
469 print parser.epilog
470 return None, None
471 if len(files) != 1:
472 parser.error("Must supply a log file to parse.")
473 if options.subunit and subunit is None:
474 parser.error("python-subunit is not installed.")
475 return options, files
478 def main():
479 options, files = parse_opts()
480 if options is None:
481 return 0
482 xd = xml.dom.minidom.parse (files[0])
483 rr = ReportReader()
484 rr.trampoline (xd)
485 if not options.subunit:
486 HTMLReportWriter(rr.get_info(), rr.binary_list()).printout()
487 else:
488 SubunitWriter(rr.get_info(), rr.binary_list()).printout()
491 if __name__ == '__main__':
492 main()