1 # Python module for parsing and generating the Subunit protocol
3 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 __all__ = ['parse_results']
26 VALID_RESULTS = ['success', 'successful', 'failure', 'fail', 'skip', 'knownfail', 'error', 'xfail', 'skip-testsuite', 'testsuite-failure', 'testsuite-xfail', 'testsuite-success', 'testsuite-error']
28 def parse_results(msg_ops, statistics, fh):
36 parts = l.split(None, 1)
37 if not len(parts) == 2 or not l.startswith(parts[0]):
40 command = parts[0].rstrip(":")
42 if command in ("test", "testing"):
43 msg_ops.control_msg(l)
44 msg_ops.start_test(arg.rstrip())
45 open_tests.append(arg.rstrip())
46 elif command == "time":
47 msg_ops.control_msg(l)
49 '(\d+)-(\d+)-(\d+) (\d+):(\d+):([.0-9]+)\n', arg)
52 '(\d+)-(\d+)-(\d+) (\d+):(\d+):([.0-9]+)Z\n', arg)
54 print "Unable to parse time line: %s" % arg
56 msg_ops.report_time(time.mktime((int(grp.group(1)), int(grp.group(2)), int(grp.group(3)), int(grp.group(4)), int(grp.group(5)), int(float(grp.group(6))), 0, 0, 0)))
57 elif command in VALID_RESULTS:
58 msg_ops.control_msg(l)
60 grp = re.match("(.*?)( \[)?([ \t]*)( multipart)?\n", arg)
61 (testname, hasreason) = (grp.group(1), grp.group(2))
64 # reason may be specified in next lines
70 msg_ops.control_msg(l)
78 statistics['TESTS_ERROR']+=1
79 msg_ops.end_test(testname, "error", True,
80 "reason (%s) interrupted" % result)
84 if result in ("success", "successful"):
86 open_tests.remove(testname)
88 statistics['TESTS_ERROR']+=1
89 msg_ops.end_test(testname, "error", True,
90 "Test was never started")
92 statistics['TESTS_EXPECTED_OK']+=1
93 msg_ops.end_test(testname, "success", False, reason)
94 elif result in ("xfail", "knownfail"):
96 open_tests.remove(testname)
98 statistics['TESTS_ERROR']+=1
99 msg_ops.end_test(testname, "error", True,
100 "Test was never started")
102 statistics['TESTS_EXPECTED_FAIL']+=1
103 msg_ops.end_test(testname, "xfail", False, reason)
105 elif result in ("failure", "fail"):
107 open_tests.remove(testname)
109 statistics['TESTS_ERROR']+=1
110 msg_ops.end_test(testname, "error", True,
111 "Test was never started")
113 statistics['TESTS_UNEXPECTED_FAIL']+=1
114 msg_ops.end_test(testname, "failure", True, reason)
115 elif result == "skip":
116 statistics['TESTS_SKIP']+=1
117 # Allow tests to be skipped without prior announcement of test
118 last = open_tests.pop()
119 if last is not None and last != testname:
120 open_tests.append(testname)
121 msg_ops.end_test(testname, "skip", False, reason)
122 elif result == "error":
123 statistics['TESTS_ERROR']+=1
125 open_tests.remove(testname)
128 msg_ops.end_test(testname, "error", True, reason)
129 elif result == "skip-testsuite":
130 msg_ops.skip_testsuite(testname)
131 elif result == "testsuite-success":
132 msg_ops.end_testsuite(testname, "success", reason)
133 elif result == "testsuite-failure":
134 msg_ops.end_testsuite(testname, "failure", reason)
135 elif result == "testsuite-xfail":
136 msg_ops.end_testsuite(testname, "xfail", reason)
137 elif result == "testsuite-error":
138 msg_ops.end_testsuite(testname, "error", reason)
140 raise AssertionError("Recognized but unhandled result %r" %
142 elif command == "testsuite":
143 msg_ops.start_testsuite(arg.strip())
144 elif command == "progress":
147 msg_ops.progress(None, subunit.PROGRESS_POP)
149 msg_ops.progress(None, subunit.PROGRESS_PUSH)
151 msg_ops.progress(int(arg), subunit.PROGRESS_CUR)
153 msg_ops.progress(int(arg), subunit.PROGRESS_SET)
155 msg_ops.output_msg(l)
158 msg_ops.end_test(open_tests.pop(), "error", True,
159 "was started but never finished!")
160 statistics['TESTS_ERROR']+=1
162 if statistics['TESTS_ERROR'] > 0:
164 if statistics['TESTS_UNEXPECTED_FAIL'] > 0:
169 class SubunitOps(object):
171 def start_test(self, testname):
172 print "test: %s" % testname
174 def end_test(self, name, result, reason=None):
176 print "%s: %s [" % (result, name)
180 print "%s: %s" % (result, name)
182 def skip_test(self, name, reason=None):
183 self.end_test(name, "skip", reason)
185 def fail_test(self, name, reason=None):
186 self.end_test(name, "fail", reason)
188 def success_test(self, name, reason=None):
189 self.end_test(name, "success", reason)
191 def xfail_test(self, name, reason=None):
192 self.end_test(name, "xfail", reason)
194 def report_time(self, t):
195 (year, mon, mday, hour, min, sec, wday, yday, isdst) = time.localtime(t)
196 print "time: %04d-%02d-%02d %02d:%02d:%02d" % (year, mon, mday, hour, min, sec)
198 def progress(self, offset, whence):
199 if whence == subunit.PROGRESS_CUR and offset > -1:
201 elif whence == subunit.PROGRESS_PUSH:
204 elif whence == subunit.PROGRESS_POP:
209 print "progress: %s%s" % (prefix, offset)
211 # The following are Samba extensions:
212 def start_testsuite(self, name):
213 print "testsuite: %s" % name
215 def skip_testsuite(self, name, reason=None):
217 print "skip-testsuite: %s [\n%s\n]" % (name, reason)
219 print "skip-testsuite: %s" % name
221 def end_testsuite(self, name, result, reason=None):
223 print "testsuite-%s: %s [" % (result, name)
227 print "testsuite-%s: %s" % (result, name)
230 def read_test_regexes(name):
236 if l == "" or l[0] == "#":
239 (regex, reason) = l.split("#", 1)
240 ret[regex.strip()] = reason.strip()
248 def find_in_list(regexes, fullname):
249 for regex, reason in regexes.iteritems():
250 if re.match(regex, fullname):
257 class FilterOps(testtools.testresult.TestResult):
259 def control_msg(self, msg):
260 pass # We regenerate control messages, so ignore this
262 def report_time(self, time):
263 self._ops.report_time(time)
265 def progress(self, delta, whence):
266 self._ops.progress(delta, whence)
268 def output_msg(self, msg):
269 if self.output is None:
270 sys.stdout.write(msg)
274 def start_test(self, testname):
275 if self.prefix is not None:
276 testname = self.prefix + testname
278 if self.strip_ok_output:
281 self._ops.start_test(testname)
283 def end_test(self, testname, result, unexpected, reason):
284 if self.prefix is not None:
285 testname = self.prefix + testname
287 if result in ("fail", "failure") and not unexpected:
291 xfail_reason = find_in_list(self.expected_failures, testname)
292 if xfail_reason is not None and result in ("fail", "failure"):
296 reason += xfail_reason
298 if result in ("fail", "failure"):
302 if result == "error":
306 if self.strip_ok_output:
307 if result not in ("success", "xfail", "skip"):
311 self._ops.end_test(testname, result, reason)
313 def skip_testsuite(self, name, reason=None):
314 self._ops.skip_testsuite(name, reason)
316 def start_testsuite(self, name):
317 self._ops.start_testsuite(name)
323 def end_testsuite(self, name, result, reason=None):
326 if self.xfail_added > 0:
328 if self.fail_added > 0 or self.error_added > 0:
331 if xfail and result in ("fail", "failure"):
334 if self.fail_added > 0 and result != "failure":
337 reason = "Subunit/Filter Reason"
338 reason += "\n failures[%d]" % self.fail_added
340 if self.error_added > 0 and result != "error":
343 reason = "Subunit/Filter Reason"
344 reason += "\n errors[%d]" % self.error_added
346 self._ops.end_testsuite(name, result, reason)
348 def __init__(self, prefix, expected_failures, strip_ok_output):
349 self._ops = SubunitOps()
352 self.expected_failures = expected_failures
353 self.strip_ok_output = strip_ok_output