276b51b812162d1c7ed704b506a46e339ed4de90
[ira/wip.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
26 def find_in_list(list, fullname):
27     """Find test in list.
28
29     :param list: List with 2-tuples with regex and reason
30     """
31     for (regex, reason) in list:
32         if re.match(regex, fullname):
33             if reason is not None:
34                 return reason
35             else:
36                 return ""
37     return None
38
39
40 def read_test_regexes(f):
41     """Read tuples with regular expression and optional string from a file.
42
43     :param f: File-like object to read from
44     :return: Iterator over tuples with regular expression and test name
45     """
46     for l in f.readlines():
47         l = l.strip()
48         if l[0] == "#":
49             continue
50         try:
51             (test, reason) = l.split("#", 1)
52         except ValueError:
53             yield l, None
54         else:
55             yield test.strip(), reason.strip()
56
57
58 def should_run_test(tests, name):
59     if tests == []:
60         return True
61     for test in tests:
62         if re.match(test, name):
63             return True
64     return False
65
66
67 def read_testlist(inf, outf):
68     """Read a list of tests from a file.
69
70     :param inf: File-like object to read from.
71     :param outf: File-like object to write to.
72     :return: Iterator over tuples describing tests
73     """
74     while True:
75         l = inf.readline()
76         if l == '':
77             return
78         if l.startswith("-- TEST") and l.endswith(" --\n"):
79             supports_loadlist = l.startswith("-- TEST-LOADLIST")
80             supports_idlist = l.startswith("-- TEST-IDLIST")
81             name = inf.readline().rstrip("\n")
82             env = inf.readline().rstrip("\n")
83             cmdline = inf.readline().rstrip("\n")
84             yield (name, env, cmdline, supports_loadlist, supports_idlist)
85         else:
86             outf.write(l)
87
88
89 def read_restricted_test_list(f):
90     for l in f.readlines():
91         yield l.strip()
92
93
94 class RestrictedTestManager(object):
95
96     def __init__(self, test_list):
97         self.test_list = test_list
98         self.unused = set(self.test_list)
99
100     def should_run_testsuite(self, name):
101         match = []
102         for r in self.test_list:
103             if r == name:
104                 match = None
105                 if r in self.unused:
106                     self.unused.remove(r)
107             elif r.startswith(name + "."):
108                 if match is not None:
109                     match.append(r[len(name+"."):])
110                 if r in self.unused:
111                     self.unused.remove(r)
112         return match
113
114     def iter_unused(self):
115         return iter(self.unused)