format-subunit: Improve formatting, simplify code.
[amitay/samba.git] / selftest / subunithelper.py
1 # Python module for parsing and generating the Subunit protocol
2 # (Samba-specific)
3 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
4 #
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.
9
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.
14
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/>.
17
18 __all__ = ['parse_results']
19
20 import re
21 import time
22
23 VALID_RESULTS = ['success', 'successful', 'failure', 'fail', 'skip', 'knownfail', 'error', 'xfail', 'skip-testsuite', 'testsuite-failure', 'testsuite-xfail', 'testsuite-success', 'testsuite-error']
24
25 def parse_results(msg_ops, statistics, fh):
26     expected_fail = 0
27     open_tests = []
28
29     while fh:
30         l = fh.readline()
31         if l == "":
32             break
33         if l.startswith("test: "):
34             msg_ops.control_msg(l)
35             name = l.split(":", 1)[1].strip()
36             msg_ops.start_test(name)
37             open_tests.append(name)
38         elif l.startswith("time: "):
39             grp = re.match(
40                 "^time: (\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)\n", l)
41             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)))
42         elif re.match("^(" + "|".join(VALID_RESULTS) + "): (.*?)( \[)?([ \t]*)( multipart)?\n", l):
43             msg_ops.control_msg(l)
44             grp = re.match("^(" + "|".join(VALID_RESULTS) + "): (.*?)( \[)?([ \t]*)( multipart)?\n", l)
45             (result, testname, hasreason) = (grp.group(1), grp.group(2), grp.group(3))
46             if hasreason:
47                 reason = ""
48                 # reason may be specified in next lines
49                 terminated = False
50                 while fh:
51                     l = fh.readline()
52                     if l == "":
53                         break
54                     msg_ops.control_msg(l)
55                     if l == "]\n":
56                         terminated = True
57                         break
58                     else:
59                         reason += l
60                 
61                 if not terminated:
62                     statistics['TESTS_ERROR']+=1
63                     msg_ops.end_test(testname, "error", True, 
64                                        "reason (%s) interrupted" % result)
65                     return 1
66             else:
67                 reason = None
68             if result in ("success", "successful"):
69                 open_tests.pop() #FIXME: Check that popped value == $testname 
70                 statistics['TESTS_EXPECTED_OK']+=1
71                 msg_ops.end_test(testname, "success", False, reason)
72             elif result in ("xfail", "knownfail"):
73                 open_tests.pop() #FIXME: Check that popped value == $testname
74                 statistics['TESTS_EXPECTED_FAIL']+=1
75                 msg_ops.end_test(testname, "xfail", False, reason)
76                 expected_fail+=1
77             elif result in ("failure", "fail"):
78                 open_tests.pop() #FIXME: Check that popped value == $testname
79                 statistics['TESTS_UNEXPECTED_FAIL']+=1
80                 msg_ops.end_test(testname, "failure", True, reason)
81             elif result == "skip":
82                 statistics['TESTS_SKIP']+=1
83                 # Allow tests to be skipped without prior announcement of test
84                 last = open_tests.pop()
85                 if last is not None and last != testname:
86                     open_tests.append(testname)
87                 msg_ops.end_test(testname, "skip", False, reason)
88             elif result == "error":
89                 statistics['TESTS_ERROR']+=1
90                 open_tests.pop() #FIXME: Check that popped value == $testname
91                 msg_ops.end_test(testname, "error", True, reason)
92             elif result == "skip-testsuite":
93                 msg_ops.skip_testsuite(testname)
94             elif result == "testsuite-success":
95                 msg_ops.end_testsuite(testname, "success", reason)
96             elif result == "testsuite-failure":
97                 msg_ops.end_testsuite(testname, "failure", reason)
98             elif result == "testsuite-xfail":
99                 msg_ops.end_testsuite(testname, "xfail", reason)
100             elif result == "testsuite-error":
101                 msg_ops.end_testsuite(testname, "error", reason)
102         elif l.startswith("testsuite: "):
103             msg_ops.start_testsuite(l.split(":", 1)[1].strip())
104         elif l.startswith("testsuite-count: "):
105             msg_ops.testsuite_count(int(l.split(":", 1)[1].strip()))
106         else:
107             msg_ops.output_msg(l)
108
109     while open_tests:
110         msg_ops.end_test(open_tests.pop(), "error", True,
111                    "was started but never finished!")
112         statistics['TESTS_ERROR']+=1
113
114     if statistics['TESTS_ERROR'] > 0:
115         return 1
116     if statistics['TESTS_UNEXPECTED_FAIL'] > 0:
117         return 1 
118     return 0
119
120
121 class SubunitOps(object):
122
123     def start_test(self, testname):
124         print "test: %s" % testname
125
126     def end_test(self, name, result, reason=None):
127         if reason:
128             print "%s: %s [" % (result, name)
129             print "%s" % reason
130             print "]"
131         else:
132             print "%s: %s" % (result, name)
133
134     def skip_test(self, name, reason=None):
135         self.end_test(name, "skip", reason)
136
137     def fail_test(self, name, reason=None):
138         self.end_test(name, "fail", reason)
139
140     def success_test(self, name, reason=None):
141         self.end_test(name, "success", reason)
142
143     def xfail_test(self, name, reason=None):
144         self.end_test(name, "xfail", reason)
145
146     def report_time(self, 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)
149
150     # The following are Samba extensions:
151     def start_testsuite(self, name):
152         print "testsuite: %s" % name
153
154     def skip_testsuite(self, name, reason=None):
155         if reason:
156             print "skip-testsuite: %s [\n%s\n]" % (name, reason)
157         else:
158             print "skip-testsuite: %s" % name
159
160     def end_testsuite(self, name, result, reason=None):
161         if reason:
162             print "testsuite-$result: %s [" % name
163             print "%s" % reason
164             print "]"
165         else:
166             print "testsuite-$result: %s" % name
167
168     def testsuite_count(self, count):
169         print "testsuite-count: %d" % count