Add TestListFilter definition.
[obnox/samba/samba-obnox.git] / selftest / testlist.py
1 # testlist.py -- Test list
2 # Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 3
7 # of the License or (at your option) any later version of
8 # the License.
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, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA  02110-1301, USA.
19
20 """Selftest test list management."""
21
22 __all__ = ['find_in_list', 'read_test_regexes', 'read_testlist']
23
24 import re
25 import sys
26
27 def find_in_list(list, fullname):
28     """Find test in list.
29
30     :param list: List with 2-tuples with regex and reason
31     """
32     for (regex, reason) in list:
33         if re.match(regex, fullname):
34             if reason is not None:
35                 return reason
36             else:
37                 return ""
38     return None
39
40
41 def read_test_regexes(f):
42     """Read tuples with regular expression and optional string from a file.
43
44     :param f: File-like object to read from
45     :return: Iterator over tuples with regular expression and test name
46     """
47     for l in f.readlines():
48         l = l.strip()
49         if l[0] == "#":
50             continue
51         try:
52             (test, reason) = l.split("#", 1)
53         except ValueError:
54             yield l, None
55         else:
56             yield test.strip(), reason.strip()
57
58
59 def should_run_test(tests, name):
60     if tests == []:
61         return True
62     for test in tests:
63         if re.match(test, name):
64             return True
65     return False
66
67
68 def read_testlist(inf, outf):
69     """Read a list of tests from a file.
70
71     :param inf: File-like object to read from.
72     :param outf: File-like object to write to.
73     :return: Iterator over tuples describing tests
74     """
75     while True:
76         l = inf.readline()
77         if l == '':
78             return
79         if l.startswith("-- TEST") and l.endswith(" --\n"):
80             supports_loadlist = l.startswith("-- TEST-LOADLIST")
81             supports_idlist = l.startswith("-- TEST-IDLIST")
82             name = inf.readline().rstrip("\n")
83             env = inf.readline().rstrip("\n")
84             cmdline = inf.readline().rstrip("\n")
85             yield (name, env, cmdline, supports_loadlist, supports_idlist)
86         else:
87             outf.write(l)
88
89
90 class TestListFilter(object):
91     """Interface for something that can filter a test list."""
92
93     def should_run_testsuite(self, name):
94         """Whether to run a specific testsuite.
95
96         :param name: Name of the testsuite
97         :return: List of tests to run.  None means run the whole testsuite.
98             Return an empty list to not run this testsuite
99         """
100         raise NotImplementedError(self.should_run_testsuite)