2010-03-30 00:25:25 +04:00
#!/usr/bin/env python
2010-03-30 02:30:52 +04:00
# vim: expandtab
2009-06-03 19:16:56 +04:00
# Pretty-format subunit output
2010-03-30 00:25:25 +04:00
# Copyright (C) 2008-2010 Jelmer Vernooij <jelmer@samba.org>
2009-06-03 19:16:56 +04:00
# Published under the GNU GPL, v3 or later
2010-03-30 00:25:25 +04:00
import optparse
import os
2010-03-30 04:39:50 +04:00
import signal
2010-03-30 00:25:25 +04:00
import sys
2009-06-04 15:49:11 +04:00
2010-03-31 06:51:05 +04:00
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../lib/subunit/python"))
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../lib/testtools"))
2010-03-31 06:52:13 +04:00
import subunithelper
import subunit
2010-03-30 02:30:52 +04:00
class PlainFormatter(object):
2010-03-30 02:59:04 +04:00
def __init__(self, summaryfile, verbose, immediate, statistics,
totaltests=None):
2010-03-30 02:30:52 +04:00
self.verbose = verbose
self.immediate = immediate
self.statistics = statistics
self.start_time = None
self.test_output = {}
self.suitesfailed = []
self.suites_ok = 0
self.skips = {}
self.summaryfile = summaryfile
self.index = 0
2010-03-30 02:59:04 +04:00
self.name = None
2010-03-30 16:42:23 +04:00
self._progress_level = 0
2010-03-30 02:30:52 +04:00
self.totalsuites = totaltests
2010-03-30 16:36:25 +04:00
def progress(self, offset, whence):
2010-03-30 16:42:23 +04:00
if whence == subunit.PROGRESS_POP:
self._progress_level -= 1
elif whence == subunit.PROGRESS_PUSH:
self._progress_level += 1
elif whence == subunit.PROGRESS_SET:
if self._progress_level == 0:
self.totalsuites = offset
elif whence == subunit.PROGRESS_CUR:
raise NotImplementedError
2010-03-30 16:36:25 +04:00
2010-03-30 02:30:52 +04:00
def report_time(self, time):
if self.start_time is None:
self.start_time = time
self.last_time = time
def start_testsuite(self, name):
self.index += 1
2010-03-30 02:59:04 +04:00
self.name = name
testsuite_start_time = self.last_time
2010-03-30 02:30:52 +04:00
2010-03-30 02:59:04 +04:00
duration = testsuite_start_time - self.start_time
2010-03-30 02:30:52 +04:00
if not self.verbose:
self.test_output[name] = ""
2010-03-30 02:59:04 +04:00
out = "[%d" % self.index
2010-03-30 02:30:52 +04:00
if self.totalsuites is not None:
out += "/%d" % self.totalsuites
out += " in %ds" % duration
if self.suitesfailed:
out += ", %d errors" % (len(self.suitesfailed),)
out += "] %s" % name
if self.immediate:
sys.stdout.write(out + "\n")
else:
sys.stdout.write(out + ": ")
def output_msg(self, output):
if self.verbose:
sys.stdout.write(output)
2010-03-30 02:59:04 +04:00
elif self.name is not None:
self.test_output[self.name] += output
2010-03-30 02:30:52 +04:00
else:
sys.stdout.write(output)
def control_msg(self, output):
#$self->output_msg($output)
pass
def end_testsuite(self, name, result, reason):
out = ""
2010-03-30 16:44:37 +04:00
unexpected = False
2010-03-30 02:30:52 +04:00
if not name in self.test_output:
print "no output for name[%s]" % name
if result in ("success", "xfail"):
self.suites_ok+=1
else:
2010-03-31 05:34:50 +04:00
self.output_msg("ERROR: Testsuite[%s]\n" % name)
2010-03-31 05:32:23 +04:00
if reason is not None:
2010-03-31 05:34:50 +04:00
self.output_msg("REASON: %s\n" % (reason,))
2010-03-30 02:30:52 +04:00
self.suitesfailed.append(name)
if self.immediate and not self.verbose:
out += self.test_output[name]
2010-03-30 16:44:37 +04:00
unexpected = True
2010-03-30 02:30:52 +04:00
if not self.immediate:
if not unexpected:
out += " ok\n"
else:
out += " " + result.upper() + "\n"
sys.stdout.write(out)
def start_test(self, testname):
pass
2010-03-31 05:34:50 +04:00
def end_test(self, testname, result, unexpected, reason=None):
2010-03-30 02:30:52 +04:00
if not unexpected:
2010-03-30 02:59:04 +04:00
self.test_output[self.name] = ""
2010-03-30 02:30:52 +04:00
if not self.immediate:
sys.stdout.write({
'failure': 'f',
'xfail': 'X',
'skip': 's',
'success': '.'}.get(result, "?(%s)" % result))
return
2010-03-31 05:34:50 +04:00
self.test_output[self.name] += "UNEXPECTED(%s): %s\n" % (result, testname)
if reason is not None:
self.test_output[self.name] += "REASON: %s\n" % (reason.strip(),)
2010-03-30 02:30:52 +04:00
if self.immediate and not self.verbose:
2010-03-30 02:59:04 +04:00
print self.test_output[self.name]
self.test_output[self.name] = ""
2010-03-30 02:30:52 +04:00
if not self.immediate:
sys.stdout.write({
'error': 'E',
'failure': 'F',
'success': 'S'}.get(result, "?"))
def summary(self):
f = open(self.summaryfile, 'w+')
if self.suitesfailed:
f.write("= Failed tests =\n")
for suite in self.suitesfailed:
f.write("== %s ==\n" % suite)
f.write(self.test_output[suite]+"\n\n")
f.write("\n")
if not self.immediate and not self.verbose:
for suite in self.suitesfailed:
2010-03-30 02:59:04 +04:00
print "=" * 78
2010-03-30 02:30:52 +04:00
print "FAIL: %s" % suite
print self.test_output[suite]
print ""
f.write("= Skipped tests =\n")
for reason in self.skips.keys():
f.write(reason + "\n")
for name in self.skips[reason]:
f.write("\t%s\n" % name)
f.write("\n")
f.close()
2010-03-30 02:59:04 +04:00
print "\nA summary with detailed information can be found in:"
print " %s" % self.summaryfile
2010-03-30 02:30:52 +04:00
if not self.suitesfailed:
2010-03-30 02:59:04 +04:00
ok = (self.statistics['TESTS_EXPECTED_OK'] +
self.statistics['TESTS_EXPECTED_FAIL'])
2010-03-30 02:30:52 +04:00
print "\nALL OK (%d tests in %d testsuites)" % (ok, self.suites_ok)
else:
2010-03-30 02:59:04 +04:00
print "\nFAILED (%d failures and %d errors in %d testsuites)" % (
self.statistics['TESTS_UNEXPECTED_FAIL'],
self.statistics['TESTS_ERROR'],
len(self.suitesfailed))
2010-03-30 02:30:52 +04:00
def skip_testsuite(self, name, reason="UNKNOWN"):
self.skips.setdefault(reason, []).append(name)
if self.totalsuites:
self.totalsuites-=1
2010-03-30 00:25:25 +04:00
parser = optparse.OptionParser("format-subunit [options]")
parser.add_option("--verbose", action="store_true",
2010-03-30 02:30:52 +04:00
help="Be verbose")
2010-03-30 00:25:25 +04:00
parser.add_option("--immediate", action="store_true",
2010-03-30 02:30:52 +04:00
help="Show failures immediately, don't wait until test run has finished")
2010-03-30 00:25:25 +04:00
parser.add_option("--prefix", type="string", default=".",
2010-03-30 02:30:52 +04:00
help="Prefix to write summary to")
2009-06-04 15:49:11 +04:00
2010-03-30 00:25:25 +04:00
opts, args = parser.parse_args()
2009-06-04 15:49:11 +04:00
2010-03-30 00:25:25 +04:00
statistics = {
2010-03-30 02:30:52 +04:00
'SUITES_FAIL': 0,
'TESTS_UNEXPECTED_OK': 0,
'TESTS_EXPECTED_OK': 0,
'TESTS_UNEXPECTED_FAIL': 0,
'TESTS_EXPECTED_FAIL': 0,
'TESTS_ERROR': 0,
'TESTS_SKIP': 0,
2010-03-30 00:25:25 +04:00
}
2009-06-04 15:49:11 +04:00
2010-03-30 04:39:50 +04:00
def handle_sigint(sig, stack):
sys.exit(0)
signal.signal(signal.SIGINT, handle_sigint)
2010-03-30 02:59:04 +04:00
msg_ops = PlainFormatter(os.path.join(opts.prefix, "summary"), opts.verbose,
opts.immediate, statistics)
2009-06-04 15:49:11 +04:00
2010-03-30 00:25:25 +04:00
expected_ret = subunithelper.parse_results(msg_ops, statistics, sys.stdin)
2009-06-04 15:49:11 +04:00
2010-03-30 00:25:25 +04:00
msg_ops.summary()
2009-06-04 15:49:11 +04:00
2010-03-30 00:25:25 +04:00
sys.exit(expected_ret)