selftest: Convert format-subunit to Python.
[Samba/eduardoll.git] / selftest / subunithelper.py
blob3cd0f013d03f6a2cea30881d6810c5a7f571c5b5
1 # Python module for parsing and generating the Subunit protocol
2 # (Samba-specific)
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']
20 import re
21 import time
23 VALID_RESULTS = ['success', 'successful', 'failure', 'fail', 'skip', 'knownfail', 'error', 'xfail', 'skip-testsuite', 'testsuite-failure', 'testsuite-xfail', 'testsuite-success', 'testsuite-error']
25 def parse_results(msg_ops, statistics, fh):
26 expected_fail = 0
27 open_tests = []
29 while fh:
30 l = fh.readline()
31 if l.startswith("test: "):
32 msg_ops.control_msg(l)
33 name = l.split(":", 1)[1].strip()
34 msg_ops.start_test(name)
35 open_tests.append(name)
36 elif l.startswith("time: "):
37 (year, month, day, hour, minute, second) = re.match(
38 "^time: (\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)\n/", l)
39 msg_ops.report_time(time.mktime(second, minute, hour, day, month-1, year-1900))
40 elif re.match("^(" + "|".join(VALID_RESULTS) + "): (.*?)( \[)?([ \t]*)( multipart)?\n", l):
41 msg_ops.control_msg(l)
42 (result, testname, hasreason) = re.match("^(" + "|".join(VALID_RESULTS) + "): (.*?)( \[)?([ \t]*)( multipart)?\n", l)
43 if hasreason:
44 reason = ""
45 # reason may be specified in next lines
46 terminated = False
47 while fh:
48 l = fh.readline()
49 msg_ops.control_msg(l)
50 if l == "]\n":
51 terminated = True
52 break
53 else:
54 reason += l
56 if not terminated:
57 statistics['TESTS_ERROR']+=1
58 msg_ops.end_test(testname, "error", 1,
59 "reason (%s) interrupted" % result)
60 return 1
61 if result in ("success", "successful"):
62 open_tests.pop() #FIXME: Check that popped value == $testname
63 statistics['TESTS_EXPECTED_OK']+=1
64 msg_ops.end_test(testname, "success", 0, reason)
65 elif result in ("xfail", "knownfail"):
66 open_tests.pop() #FIXME: Check that popped value == $testname
67 statistics['TESTS_EXPECTED_FAIL']+=1
68 msg_ops.end_test(testname, "xfail", 0, reason)
69 expected_fail+=1
70 elif result in ("failure", "fail"):
71 open_tests.pop() #FIXME: Check that popped value == $testname
72 statistics['TESTS_UNEXPECTED_FAIL']+=1
73 msg_ops.end_test(testname, "failure", 1, reason)
74 elif result == "skip":
75 statistics['TESTS_SKIP']+=1
76 # Allow tests to be skipped without prior announcement of test
77 last = open_tests.pop()
78 if last is not None and last != testname:
79 open_tests.append(testname)
80 msg_ops.end_test(testname, "skip", 0, reason)
81 elif result == "error":
82 statistics['TESTS_ERROR']+=1
83 open_tests.pop() #FIXME: Check that popped value == $testname
84 msg_ops.end_test(testname, "error", 1, reason)
85 elif result == "skip-testsuite":
86 msg_ops.skip_testsuite(testname)
87 elif result == "testsuite-success":
88 msg_ops.end_testsuite(testname, "success", reason)
89 elif result == "testsuite-failure":
90 msg_ops.end_testsuite(testname, "failure", reason)
91 elif result == "testsuite-xfail":
92 msg_ops.end_testsuite(testname, "xfail", reason)
93 elif result == "testsuite-error":
94 msg_ops.end_testsuite(testname, "error", reason)
95 elif l.startswith("testsuite: "):
96 msg_ops.start_testsuite(l.split(":", 1)[1].strip())
97 elif l.startswith("testsuite-count: "):
98 msg_ops.testsuite_count(int(l.split(":", 1)[1].strip()))
99 else:
100 msg_ops.output_msg(l)
102 while open_tests:
103 msg_ops.end_test(open_tests.pop(), "error", 1,
104 "was started but never finished!")
105 statistics['TESTS_ERROR']+=1
107 # if the Filter module is in use, it will have the right counts
108 if 'total_error' in msg_ops:
109 statistics['TESTS_ERROR'] = msg_ops['total_error']
110 statistics['TESTS_UNEXPECTED_FAIL'] = msg_ops['total_fail']
111 statistics['TESTS_EXPECTED_FAIL'] = msg_ops['total_xfail']
113 if statistics['TESTS_ERROR'] > 0:
114 return 1
115 if statistics['TESTS_UNEXPECTED_FAIL'] > 0:
116 return 1
117 return 0
120 def start_test(testname):
121 print "test: %s" % testname
123 def end_test(name, result, reason=None):
124 if reason:
125 print "%s: %s [" % (result, name)
126 print "%s" % reason
127 print "]"
128 else:
129 print "%s: %s" % (result, name)
132 def skip_test(name, reason=None):
133 end_test(name, "skip", reason)
136 def fail_test(name, reason=None):
137 end_test(name, "fail", reason)
140 def success_test(name, reason=None):
141 end_test(name, "success", reason)
143 def xfail_test(name, reason=None):
144 end_test(name, "xfail", reason)
146 def report_time(t):
147 (sec, min, hour, mday, mon, year, wday, yday, isdst) = time.localtimet(t)
148 print "time: %04d-%02d-%02d %02d:%02d:%02d" % (year+1900, mon+1, mday, hour, min, sec)
151 # The following are Samba extensions:
152 def start_testsuite(name):
153 print "testsuite: %s" % name
155 def skip_testsuite(name, reason=None):
156 if reason:
157 print "skip-testsuite: %s [\n%s\n]" % (name, reason)
158 else:
159 print "skip-testsuite: %s" % name
161 def end_testsuite(name, result, reason=None):
162 if reason:
163 print "testsuite-$result: %s [" % name
164 print "%s" % reason
165 print "]"
166 else:
167 print "testsuite-$result: %s" % name
169 def testsuite_count(count):
170 print "testsuite-count: %d" % count