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]):
38 command
= parts
[0].rstrip(":")
40 if command
in ("test", "testing"):
41 msg_ops
.control_msg(l
)
42 msg_ops
.start_test(arg
.rstrip())
43 open_tests
.append(arg
.rstrip())
44 elif command
== "time":
45 msg_ops
.control_msg(l
)
47 "(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)\n", arg
)
48 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)))
49 elif command
in VALID_RESULTS
:
50 msg_ops
.control_msg(l
)
52 grp
= re
.match("(.*?)( \[)?([ \t]*)( multipart)?\n", arg
)
53 (testname
, hasreason
) = (grp
.group(1), grp
.group(2))
56 # reason may be specified in next lines
62 msg_ops
.control_msg(l
)
70 statistics
['TESTS_ERROR']+=1
71 msg_ops
.end_test(testname
, "error", True,
72 "reason (%s) interrupted" % result
)
76 if result
in ("success", "successful"):
78 open_tests
.remove(testname
)
80 statistics
['TESTS_ERROR']+=1
81 msg_ops
.end_test(testname
, "error", True,
82 "Test was never started")
84 statistics
['TESTS_EXPECTED_OK']+=1
85 msg_ops
.end_test(testname
, "success", False, reason
)
86 elif result
in ("xfail", "knownfail"):
88 open_tests
.remove(testname
)
90 statistics
['TESTS_ERROR']+=1
91 msg_ops
.end_test(testname
, "error", True,
92 "Test was never started")
94 statistics
['TESTS_EXPECTED_FAIL']+=1
95 msg_ops
.end_test(testname
, "xfail", False, reason
)
97 elif result
in ("failure", "fail"):
99 open_tests
.remove(testname
)
101 statistics
['TESTS_ERROR']+=1
102 msg_ops
.end_test(testname
, "error", True,
103 "Test was never started")
105 statistics
['TESTS_UNEXPECTED_FAIL']+=1
106 msg_ops
.end_test(testname
, "failure", True, reason
)
107 elif result
== "skip":
108 statistics
['TESTS_SKIP']+=1
109 # Allow tests to be skipped without prior announcement of test
110 last
= open_tests
.pop()
111 if last
is not None and last
!= testname
:
112 open_tests
.append(testname
)
113 msg_ops
.end_test(testname
, "skip", False, reason
)
114 elif result
== "error":
115 statistics
['TESTS_ERROR']+=1
117 open_tests
.remove(testname
)
120 msg_ops
.end_test(testname
, "error", True, reason
)
121 elif result
== "skip-testsuite":
122 msg_ops
.skip_testsuite(testname
)
123 elif result
== "testsuite-success":
124 msg_ops
.end_testsuite(testname
, "success", reason
)
125 elif result
== "testsuite-failure":
126 msg_ops
.end_testsuite(testname
, "failure", reason
)
127 elif result
== "testsuite-xfail":
128 msg_ops
.end_testsuite(testname
, "xfail", reason
)
129 elif result
== "testsuite-error":
130 msg_ops
.end_testsuite(testname
, "error", reason
)
131 elif command
== "testsuite":
132 msg_ops
.start_testsuite(arg
.strip())
133 elif command
== "progress":
136 msg_ops
.progress(None, subunit
.PROGRESS_POP
)
138 msg_ops
.progress(None, subunit
.PROGRESS_PUSH
)
140 msg_ops
.progress(int(arg
), subunit
.PROGRESS_CUR
)
142 msg_ops
.progress(int(arg
), subunit
.PROGRESS_SET
)
144 msg_ops
.output_msg(l
)
147 msg_ops
.end_test(open_tests
.pop(), "error", True,
148 "was started but never finished!")
149 statistics
['TESTS_ERROR']+=1
151 if statistics
['TESTS_ERROR'] > 0:
153 if statistics
['TESTS_UNEXPECTED_FAIL'] > 0:
158 class SubunitOps(object):
160 def start_test(self
, testname
):
161 print "test: %s" % testname
163 def end_test(self
, name
, result
, reason
=None):
165 print "%s: %s [" % (result
, name
)
169 print "%s: %s" % (result
, name
)
171 def skip_test(self
, name
, reason
=None):
172 self
.end_test(name
, "skip", reason
)
174 def fail_test(self
, name
, reason
=None):
175 self
.end_test(name
, "fail", reason
)
177 def success_test(self
, name
, reason
=None):
178 self
.end_test(name
, "success", reason
)
180 def xfail_test(self
, name
, reason
=None):
181 self
.end_test(name
, "xfail", reason
)
183 def report_time(self
, t
):
184 (year
, mon
, mday
, hour
, min, sec
, wday
, yday
, isdst
) = time
.localtime(t
)
185 print "time: %04d-%02d-%02d %02d:%02d:%02d" % (year
, mon
, mday
, hour
, min, sec
)
187 def progress(self
, offset
, whence
):
188 if whence
== subunit
.PROGRESS_CUR
and offset
> -1:
190 elif whence
== subunit
.PROGRESS_PUSH
:
193 elif whence
== subunit
.PROGRESS_POP
:
198 print "progress: %s%s" % (prefix
, offset
)
200 # The following are Samba extensions:
201 def start_testsuite(self
, name
):
202 print "testsuite: %s" % name
204 def skip_testsuite(self
, name
, reason
=None):
206 print "skip-testsuite: %s [\n%s\n]" % (name
, reason
)
208 print "skip-testsuite: %s" % name
210 def end_testsuite(self
, name
, result
, reason
=None):
212 print "testsuite-%s: %s [" % (result
, name
)
216 print "testsuite-%s: %s" % (result
, name
)
219 def read_test_regexes(name
):
225 if l
== "" or l
[0] == "#":
228 (regex
, reason
) = l
.split("#", 1)
229 ret
[regex
.strip()] = reason
.strip()
237 def find_in_list(regexes
, fullname
):
238 for regex
, reason
in regexes
.iteritems():
239 if re
.match(regex
, fullname
):
246 class FilterOps(object):
248 def control_msg(self
, msg
):
249 pass # We regenerate control messages, so ignore this
251 def report_time(self
, time
):
252 self
._ops
.report_time(time
)
254 def progress(self
, delta
, whence
):
255 self
._ops
.progress(delta
, whence
)
257 def output_msg(self
, msg
):
258 if self
.output
is None:
259 sys
.stdout
.write(msg
)
263 def start_test(self
, testname
):
264 if self
.prefix
is not None:
265 testname
= self
.prefix
+ testname
267 if self
.strip_ok_output
:
270 self
._ops
.start_test(testname
)
272 def end_test(self
, testname
, result
, unexpected
, reason
):
273 if self
.prefix
is not None:
274 testname
= self
.prefix
+ testname
276 if result
in ("fail", "failure") and not unexpected
:
280 xfail_reason
= find_in_list(self
.expected_failures
, testname
)
281 if xfail_reason
is not None and result
in ("fail", "failure"):
285 reason
+= xfail_reason
287 if result
in ("fail", "failure"):
291 if result
== "error":
295 if self
.strip_ok_output
:
296 if result
not in ("success", "xfail", "skip"):
300 self
._ops
.end_test(testname
, result
, reason
)
302 def skip_testsuite(self
, name
, reason
=None):
303 self
._ops
.skip_testsuite(name
, reason
)
305 def start_testsuite(self
, name
):
306 self
._ops
.start_testsuite(name
)
312 def end_testsuite(self
, name
, result
, reason
=None):
315 if self
.xfail_added
> 0:
317 if self
.fail_added
> 0 or self
.error_added
> 0:
320 if xfail
and result
in ("fail", "failure"):
323 if self
.fail_added
> 0 and result
!= "failure":
326 reason
= "Subunit/Filter Reason"
327 reason
+= "\n failures[%d]" % self
.fail_added
329 if self
.error_added
> 0 and result
!= "error":
332 reason
= "Subunit/Filter Reason"
333 reason
+= "\n errors[%d]" % self
.error_added
335 self
._ops
.end_testsuite(name
, result
, reason
)
337 def __init__(self
, prefix
, expected_failures
, strip_ok_output
):
338 self
._ops
= SubunitOps()
341 self
.expected_failures
= expected_failures
342 self
.strip_ok_output
= strip_ok_output