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']
24 VALID_RESULTS = ['success', 'successful', 'failure', 'fail', 'skip', 'knownfail', 'error', 'xfail', 'skip-testsuite', 'testsuite-failure', 'testsuite-xfail', 'testsuite-success', 'testsuite-error']
26 def parse_results(msg_ops, statistics, fh):
34 if l.startswith("test: "):
35 msg_ops.control_msg(l)
36 name = l.split(":", 1)[1].strip()
37 msg_ops.start_test(name)
38 open_tests.append(name)
39 elif l.startswith("time: "):
41 "^time: (\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)\n", l)
42 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(grp.group(6)), 0, 0, 0)))
43 elif re.match("^(" + "|".join(VALID_RESULTS) + "): (.*?)( \[)?([ \t]*)( multipart)?\n", l):
44 msg_ops.control_msg(l)
45 grp = re.match("^(" + "|".join(VALID_RESULTS) + "): (.*?)( \[)?([ \t]*)( multipart)?\n", l)
46 (result, testname, hasreason) = (grp.group(1), grp.group(2), grp.group(3))
49 # reason may be specified in next lines
55 msg_ops.control_msg(l)
63 statistics['TESTS_ERROR']+=1
64 msg_ops.end_test(testname, "error", True,
65 "reason (%s) interrupted" % result)
69 if result in ("success", "successful"):
70 open_tests.pop() #FIXME: Check that popped value == $testname
71 statistics['TESTS_EXPECTED_OK']+=1
72 msg_ops.end_test(testname, "success", False, reason)
73 elif result in ("xfail", "knownfail"):
74 open_tests.pop() #FIXME: Check that popped value == $testname
75 statistics['TESTS_EXPECTED_FAIL']+=1
76 msg_ops.end_test(testname, "xfail", False, reason)
78 elif result in ("failure", "fail"):
79 open_tests.pop() #FIXME: Check that popped value == $testname
80 statistics['TESTS_UNEXPECTED_FAIL']+=1
81 msg_ops.end_test(testname, "failure", True, reason)
82 elif result == "skip":
83 statistics['TESTS_SKIP']+=1
84 # Allow tests to be skipped without prior announcement of test
85 last = open_tests.pop()
86 if last is not None and last != testname:
87 open_tests.append(testname)
88 msg_ops.end_test(testname, "skip", False, reason)
89 elif result == "error":
90 statistics['TESTS_ERROR']+=1
91 open_tests.pop() #FIXME: Check that popped value == $testname
92 msg_ops.end_test(testname, "error", True, reason)
93 elif result == "skip-testsuite":
94 msg_ops.skip_testsuite(testname)
95 elif result == "testsuite-success":
96 msg_ops.end_testsuite(testname, "success", reason)
97 elif result == "testsuite-failure":
98 msg_ops.end_testsuite(testname, "failure", reason)
99 elif result == "testsuite-xfail":
100 msg_ops.end_testsuite(testname, "xfail", reason)
101 elif result == "testsuite-error":
102 msg_ops.end_testsuite(testname, "error", reason)
103 elif l.startswith("testsuite: "):
104 msg_ops.start_testsuite(l.split(":", 1)[1].strip())
105 elif l.startswith("testsuite-count: "):
106 msg_ops.testsuite_count(int(l.split(":", 1)[1].strip()))
108 msg_ops.output_msg(l)
111 msg_ops.end_test(open_tests.pop(), "error", True,
112 "was started but never finished!")
113 statistics['TESTS_ERROR']+=1
115 if statistics['TESTS_ERROR'] > 0:
117 if statistics['TESTS_UNEXPECTED_FAIL'] > 0:
122 class SubunitOps(object):
124 def start_test(self, testname):
125 print "test: %s" % testname
127 def end_test(self, name, result, reason=None):
129 print "%s: %s [" % (result, name)
133 print "%s: %s" % (result, name)
135 def skip_test(self, name, reason=None):
136 self.end_test(name, "skip", reason)
138 def fail_test(self, name, reason=None):
139 self.end_test(name, "fail", reason)
141 def success_test(self, name, reason=None):
142 self.end_test(name, "success", reason)
144 def xfail_test(self, name, reason=None):
145 self.end_test(name, "xfail", reason)
147 def report_time(self, t):
148 (year, mon, mday, hour, min, sec, wday, yday, isdst) = time.localtime(t)
149 print "time: %04d-%02d-%02d %02d:%02d:%02d" % (year, mon, mday, hour, min, sec)
151 # The following are Samba extensions:
152 def start_testsuite(self, name):
153 print "testsuite: %s" % name
155 def skip_testsuite(self, name, reason=None):
157 print "skip-testsuite: %s [\n%s\n]" % (name, reason)
159 print "skip-testsuite: %s" % name
161 def end_testsuite(self, name, result, reason=None):
163 print "testsuite-%s: %s [" % (result, name)
167 print "testsuite-%s: %s" % (result, name)
169 def testsuite_count(self, count):
170 print "testsuite-count: %d" % count
173 def read_test_regexes(name):
178 if l == "" or l[0] == "#":
181 (regex, reason) = l.split("#", 1)
182 yield (regex.strip(), reason.strip())
189 def find_in_list(regexes, fullname):
190 for regex, reason in regexes:
191 if re.match(regex, fullname):
198 class FilterOps(object):
200 def control_msg(self, msg):
201 pass # We regenerate control messages, so ignore this
203 def report_time(self, time):
204 self._ops.report_time(time)
206 def output_msg(self, msg):
207 if self.output is None:
208 sys.stdout.write(msg)
212 def start_test(self, testname):
213 if self.prefix is not None:
214 testname = self.prefix + testname
216 if self.strip_ok_output:
219 self._ops.start_test(testname)
221 def end_test(self, testname, result, unexpected, reason):
222 if self.prefix is not None:
223 testname = self.prefix + testname
225 if result in ("fail", "failure") and not unexpected:
229 xfail_reason = find_in_list(self.expected_failures, testname)
230 if xfail_reason is not None and result in ("fail", "failure"):
234 reason += xfail_reason
236 if result in ("fail", "failure"):
240 if result == "error":
244 if self.strip_ok_output:
245 if result not in ("success", "xfail", "skip"):
249 self._ops.end_test(testname, result, reason)
251 def skip_testsuite(self, name, reason=None):
252 self._ops.skip_testsuite(name, reason)
254 def start_testsuite(self, name):
255 self._ops.start_testsuite(name)
261 def end_testsuite(self, name, result, reason=None):
264 if self.xfail_added > 0:
266 if self.fail_added > 0 or self.error_added > 0:
269 if xfail and result in ("fail", "failure"):
272 if self.fail_added > 0 and result != "failure":
275 reason = "Subunit/Filter Reason"
276 reason += "\n failures[%d]" % self.fail_added
278 if self.error_added > 0 and result != "error":
281 reason = "Subunit/Filter Reason"
282 reason += "\n errors[%d]" % self.error_added
284 self._ops.end_testsuite(name, result, reason)
286 def testsuite_count(self, count):
287 self._ops.testsuite_count(count)
289 def __init__(self, prefix, expected_failures, strip_ok_output):
290 self._ops = SubunitOps()
293 self.expected_failures = expected_failures
294 self.strip_ok_output = strip_ok_output