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']
25 VALID_RESULTS
= ['success', 'successful', 'failure', 'fail', 'skip', 'knownfail', 'error', 'xfail', 'skip-testsuite', 'testsuite-failure', 'testsuite-xfail', 'testsuite-success', 'testsuite-error']
27 def parse_results(msg_ops
, statistics
, fh
):
35 parts
= l
.split(None, 1)
36 if not len(parts
) == 2 or not l
.startswith(parts
[0]):
39 command
= parts
[0].rstrip(":")
41 if command
in ("test", "testing"):
42 msg_ops
.control_msg(l
)
43 msg_ops
.start_test(arg
.rstrip())
44 open_tests
.append(arg
.rstrip())
45 elif command
== "time":
46 msg_ops
.control_msg(l
)
48 '(\d+)-(\d+)-(\d+) (\d+):(\d+):([.0-9]+)\n', arg
)
51 '(\d+)-(\d+)-(\d+) (\d+):(\d+):([.0-9]+)Z\n', arg
)
53 print "Unable to parse time line: %s" % arg
55 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)))
56 elif command
in VALID_RESULTS
:
57 msg_ops
.control_msg(l
)
59 grp
= re
.match("(.*?)( \[)?([ \t]*)( multipart)?\n", arg
)
60 (testname
, hasreason
) = (grp
.group(1), grp
.group(2))
63 # reason may be specified in next lines
69 msg_ops
.control_msg(l
)
77 statistics
['TESTS_ERROR']+=1
78 msg_ops
.end_test(testname
, "error", True,
79 "reason (%s) interrupted" % result
)
83 if result
in ("success", "successful"):
85 open_tests
.remove(testname
)
87 statistics
['TESTS_ERROR']+=1
88 msg_ops
.end_test(testname
, "error", True,
89 "Test was never started")
91 statistics
['TESTS_EXPECTED_OK']+=1
92 msg_ops
.end_test(testname
, "success", False, reason
)
93 elif result
in ("xfail", "knownfail"):
95 open_tests
.remove(testname
)
97 statistics
['TESTS_ERROR']+=1
98 msg_ops
.end_test(testname
, "error", True,
99 "Test was never started")
101 statistics
['TESTS_EXPECTED_FAIL']+=1
102 msg_ops
.end_test(testname
, "xfail", False, reason
)
104 elif result
in ("failure", "fail"):
106 open_tests
.remove(testname
)
108 statistics
['TESTS_ERROR']+=1
109 msg_ops
.end_test(testname
, "error", True,
110 "Test was never started")
112 statistics
['TESTS_UNEXPECTED_FAIL']+=1
113 msg_ops
.end_test(testname
, "failure", True, reason
)
114 elif result
== "skip":
115 statistics
['TESTS_SKIP']+=1
116 # Allow tests to be skipped without prior announcement of test
117 last
= open_tests
.pop()
118 if last
is not None and last
!= testname
:
119 open_tests
.append(testname
)
120 msg_ops
.end_test(testname
, "skip", False, reason
)
121 elif result
== "error":
122 statistics
['TESTS_ERROR']+=1
124 open_tests
.remove(testname
)
127 msg_ops
.end_test(testname
, "error", True, reason
)
128 elif result
== "skip-testsuite":
129 msg_ops
.skip_testsuite(testname
)
130 elif result
== "testsuite-success":
131 msg_ops
.end_testsuite(testname
, "success", reason
)
132 elif result
== "testsuite-failure":
133 msg_ops
.end_testsuite(testname
, "failure", reason
)
134 elif result
== "testsuite-xfail":
135 msg_ops
.end_testsuite(testname
, "xfail", reason
)
136 elif result
== "testsuite-error":
137 msg_ops
.end_testsuite(testname
, "error", reason
)
139 raise AssertionError("Recognized but unhandled result %r" %
141 elif command
== "testsuite":
142 msg_ops
.start_testsuite(arg
.strip())
143 elif command
== "progress":
146 msg_ops
.progress(None, subunit
.PROGRESS_POP
)
148 msg_ops
.progress(None, subunit
.PROGRESS_PUSH
)
150 msg_ops
.progress(int(arg
), subunit
.PROGRESS_CUR
)
152 msg_ops
.progress(int(arg
), subunit
.PROGRESS_SET
)
154 msg_ops
.output_msg(l
)
157 msg_ops
.end_test(open_tests
.pop(), "error", True,
158 "was started but never finished!")
159 statistics
['TESTS_ERROR']+=1
161 if statistics
['TESTS_ERROR'] > 0:
163 if statistics
['TESTS_UNEXPECTED_FAIL'] > 0:
168 class SubunitOps(object):
170 def start_test(self
, testname
):
171 print "test: %s" % testname
173 def end_test(self
, name
, result
, reason
=None):
175 print "%s: %s [" % (result
, name
)
179 print "%s: %s" % (result
, name
)
181 def skip_test(self
, name
, reason
=None):
182 self
.end_test(name
, "skip", reason
)
184 def fail_test(self
, name
, reason
=None):
185 self
.end_test(name
, "fail", reason
)
187 def success_test(self
, name
, reason
=None):
188 self
.end_test(name
, "success", reason
)
190 def xfail_test(self
, name
, reason
=None):
191 self
.end_test(name
, "xfail", reason
)
193 def report_time(self
, t
):
194 (year
, mon
, mday
, hour
, min, sec
, wday
, yday
, isdst
) = time
.localtime(t
)
195 print "time: %04d-%02d-%02d %02d:%02d:%02d" % (year
, mon
, mday
, hour
, min, sec
)
197 def progress(self
, offset
, whence
):
198 if whence
== subunit
.PROGRESS_CUR
and offset
> -1:
200 elif whence
== subunit
.PROGRESS_PUSH
:
203 elif whence
== subunit
.PROGRESS_POP
:
208 print "progress: %s%s" % (prefix
, offset
)
210 # The following are Samba extensions:
211 def start_testsuite(self
, name
):
212 print "testsuite: %s" % name
214 def skip_testsuite(self
, name
, reason
=None):
216 print "skip-testsuite: %s [\n%s\n]" % (name
, reason
)
218 print "skip-testsuite: %s" % name
220 def end_testsuite(self
, name
, result
, reason
=None):
222 print "testsuite-%s: %s [" % (result
, name
)
226 print "testsuite-%s: %s" % (result
, name
)
229 def read_test_regexes(name
):
235 if l
== "" or l
[0] == "#":
238 (regex
, reason
) = l
.split("#", 1)
239 ret
[regex
.strip()] = reason
.strip()
247 def find_in_list(regexes
, fullname
):
248 for regex
, reason
in regexes
.iteritems():
249 if re
.match(regex
, fullname
):
256 class FilterOps(object):
258 def control_msg(self
, msg
):
259 pass # We regenerate control messages, so ignore this
261 def report_time(self
, time
):
262 self
._ops
.report_time(time
)
264 def progress(self
, delta
, whence
):
265 self
._ops
.progress(delta
, whence
)
267 def output_msg(self
, msg
):
268 if self
.output
is None:
269 sys
.stdout
.write(msg
)
273 def start_test(self
, testname
):
274 if self
.prefix
is not None:
275 testname
= self
.prefix
+ testname
277 if self
.strip_ok_output
:
280 self
._ops
.start_test(testname
)
282 def end_test(self
, testname
, result
, unexpected
, reason
):
283 if self
.prefix
is not None:
284 testname
= self
.prefix
+ testname
286 if result
in ("fail", "failure") and not unexpected
:
290 xfail_reason
= find_in_list(self
.expected_failures
, testname
)
291 if xfail_reason
is not None and result
in ("fail", "failure"):
295 reason
+= xfail_reason
297 if result
in ("fail", "failure"):
301 if result
== "error":
305 if self
.strip_ok_output
:
306 if result
not in ("success", "xfail", "skip"):
310 self
._ops
.end_test(testname
, result
, reason
)
312 def skip_testsuite(self
, name
, reason
=None):
313 self
._ops
.skip_testsuite(name
, reason
)
315 def start_testsuite(self
, name
):
316 self
._ops
.start_testsuite(name
)
322 def end_testsuite(self
, name
, result
, reason
=None):
325 if self
.xfail_added
> 0:
327 if self
.fail_added
> 0 or self
.error_added
> 0:
330 if xfail
and result
in ("fail", "failure"):
333 if self
.fail_added
> 0 and result
!= "failure":
336 reason
= "Subunit/Filter Reason"
337 reason
+= "\n failures[%d]" % self
.fail_added
339 if self
.error_added
> 0 and result
!= "error":
342 reason
= "Subunit/Filter Reason"
343 reason
+= "\n errors[%d]" % self
.error_added
345 self
._ops
.end_testsuite(name
, result
, reason
)
347 def __init__(self
, prefix
, expected_failures
, strip_ok_output
):
348 self
._ops
= SubunitOps()
351 self
.expected_failures
= expected_failures
352 self
.strip_ok_output
= strip_ok_output