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+):(\d+)\n", arg
)
49 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)))
50 elif command
in VALID_RESULTS
:
51 msg_ops
.control_msg(l
)
53 grp
= re
.match("(.*?)( \[)?([ \t]*)( multipart)?\n", arg
)
54 (testname
, hasreason
) = (grp
.group(1), grp
.group(2))
57 # reason may be specified in next lines
63 msg_ops
.control_msg(l
)
71 statistics
['TESTS_ERROR']+=1
72 msg_ops
.end_test(testname
, "error", True,
73 "reason (%s) interrupted" % result
)
77 if result
in ("success", "successful"):
79 open_tests
.remove(testname
)
81 statistics
['TESTS_ERROR']+=1
82 msg_ops
.end_test(testname
, "error", True,
83 "Test was never started")
85 statistics
['TESTS_EXPECTED_OK']+=1
86 msg_ops
.end_test(testname
, "success", False, reason
)
87 elif result
in ("xfail", "knownfail"):
89 open_tests
.remove(testname
)
91 statistics
['TESTS_ERROR']+=1
92 msg_ops
.end_test(testname
, "error", True,
93 "Test was never started")
95 statistics
['TESTS_EXPECTED_FAIL']+=1
96 msg_ops
.end_test(testname
, "xfail", False, reason
)
98 elif result
in ("failure", "fail"):
100 open_tests
.remove(testname
)
102 statistics
['TESTS_ERROR']+=1
103 msg_ops
.end_test(testname
, "error", True,
104 "Test was never started")
106 statistics
['TESTS_UNEXPECTED_FAIL']+=1
107 msg_ops
.end_test(testname
, "failure", True, reason
)
108 elif result
== "skip":
109 statistics
['TESTS_SKIP']+=1
110 # Allow tests to be skipped without prior announcement of test
111 last
= open_tests
.pop()
112 if last
is not None and last
!= testname
:
113 open_tests
.append(testname
)
114 msg_ops
.end_test(testname
, "skip", False, reason
)
115 elif result
== "error":
116 statistics
['TESTS_ERROR']+=1
118 open_tests
.remove(testname
)
121 msg_ops
.end_test(testname
, "error", True, reason
)
122 elif result
== "skip-testsuite":
123 msg_ops
.skip_testsuite(testname
)
124 elif result
== "testsuite-success":
125 msg_ops
.end_testsuite(testname
, "success", reason
)
126 elif result
== "testsuite-failure":
127 msg_ops
.end_testsuite(testname
, "failure", reason
)
128 elif result
== "testsuite-xfail":
129 msg_ops
.end_testsuite(testname
, "xfail", reason
)
130 elif result
== "testsuite-error":
131 msg_ops
.end_testsuite(testname
, "error", reason
)
133 raise AssertionError("Recognized but unhandled result %r" %
135 elif command
== "testsuite":
136 msg_ops
.start_testsuite(arg
.strip())
137 elif command
== "progress":
140 msg_ops
.progress(None, subunit
.PROGRESS_POP
)
142 msg_ops
.progress(None, subunit
.PROGRESS_PUSH
)
144 msg_ops
.progress(int(arg
), subunit
.PROGRESS_CUR
)
146 msg_ops
.progress(int(arg
), subunit
.PROGRESS_SET
)
148 msg_ops
.output_msg(l
)
151 msg_ops
.end_test(open_tests
.pop(), "error", True,
152 "was started but never finished!")
153 statistics
['TESTS_ERROR']+=1
155 if statistics
['TESTS_ERROR'] > 0:
157 if statistics
['TESTS_UNEXPECTED_FAIL'] > 0:
162 class SubunitOps(object):
164 def start_test(self
, testname
):
165 print "test: %s" % testname
167 def end_test(self
, name
, result
, reason
=None):
169 print "%s: %s [" % (result
, name
)
173 print "%s: %s" % (result
, name
)
175 def skip_test(self
, name
, reason
=None):
176 self
.end_test(name
, "skip", reason
)
178 def fail_test(self
, name
, reason
=None):
179 self
.end_test(name
, "fail", reason
)
181 def success_test(self
, name
, reason
=None):
182 self
.end_test(name
, "success", reason
)
184 def xfail_test(self
, name
, reason
=None):
185 self
.end_test(name
, "xfail", reason
)
187 def report_time(self
, t
):
188 (year
, mon
, mday
, hour
, min, sec
, wday
, yday
, isdst
) = time
.localtime(t
)
189 print "time: %04d-%02d-%02d %02d:%02d:%02d" % (year
, mon
, mday
, hour
, min, sec
)
191 def progress(self
, offset
, whence
):
192 if whence
== subunit
.PROGRESS_CUR
and offset
> -1:
194 elif whence
== subunit
.PROGRESS_PUSH
:
197 elif whence
== subunit
.PROGRESS_POP
:
202 print "progress: %s%s" % (prefix
, offset
)
204 # The following are Samba extensions:
205 def start_testsuite(self
, name
):
206 print "testsuite: %s" % name
208 def skip_testsuite(self
, name
, reason
=None):
210 print "skip-testsuite: %s [\n%s\n]" % (name
, reason
)
212 print "skip-testsuite: %s" % name
214 def end_testsuite(self
, name
, result
, reason
=None):
216 print "testsuite-%s: %s [" % (result
, name
)
220 print "testsuite-%s: %s" % (result
, name
)
223 def read_test_regexes(name
):
229 if l
== "" or l
[0] == "#":
232 (regex
, reason
) = l
.split("#", 1)
233 ret
[regex
.strip()] = reason
.strip()
241 def find_in_list(regexes
, fullname
):
242 for regex
, reason
in regexes
.iteritems():
243 if re
.match(regex
, fullname
):
250 class FilterOps(object):
252 def control_msg(self
, msg
):
253 pass # We regenerate control messages, so ignore this
255 def report_time(self
, time
):
256 self
._ops
.report_time(time
)
258 def progress(self
, delta
, whence
):
259 self
._ops
.progress(delta
, whence
)
261 def output_msg(self
, msg
):
262 if self
.output
is None:
263 sys
.stdout
.write(msg
)
267 def start_test(self
, testname
):
268 if self
.prefix
is not None:
269 testname
= self
.prefix
+ testname
271 if self
.strip_ok_output
:
274 self
._ops
.start_test(testname
)
276 def end_test(self
, testname
, result
, unexpected
, reason
):
277 if self
.prefix
is not None:
278 testname
= self
.prefix
+ testname
280 if result
in ("fail", "failure") and not unexpected
:
284 xfail_reason
= find_in_list(self
.expected_failures
, testname
)
285 if xfail_reason
is not None and result
in ("fail", "failure"):
289 reason
+= xfail_reason
291 if result
in ("fail", "failure"):
295 if result
== "error":
299 if self
.strip_ok_output
:
300 if result
not in ("success", "xfail", "skip"):
304 self
._ops
.end_test(testname
, result
, reason
)
306 def skip_testsuite(self
, name
, reason
=None):
307 self
._ops
.skip_testsuite(name
, reason
)
309 def start_testsuite(self
, name
):
310 self
._ops
.start_testsuite(name
)
316 def end_testsuite(self
, name
, result
, reason
=None):
319 if self
.xfail_added
> 0:
321 if self
.fail_added
> 0 or self
.error_added
> 0:
324 if xfail
and result
in ("fail", "failure"):
327 if self
.fail_added
> 0 and result
!= "failure":
330 reason
= "Subunit/Filter Reason"
331 reason
+= "\n failures[%d]" % self
.fail_added
333 if self
.error_added
> 0 and result
!= "error":
336 reason
= "Subunit/Filter Reason"
337 reason
+= "\n errors[%d]" % self
.error_added
339 self
._ops
.end_testsuite(name
, result
, reason
)
341 def __init__(self
, prefix
, expected_failures
, strip_ok_output
):
342 self
._ops
= SubunitOps()
345 self
.expected_failures
= expected_failures
346 self
.strip_ok_output
= strip_ok_output