lib/param: Make and use lpcfg_equal_parameter in common
[Samba.git] / selftest / testlist.py
blob5102f4288bd84a534e60e4af679c4437cc360b7b
1 # testlist.py -- Test list
2 # Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org>
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.
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, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA 02110-1301, USA.
20 """Selftest test list management."""
22 __all__ = ['find_in_list', 'read_test_regexes', 'read_testlist']
24 import os
25 import re
26 import sys
28 def find_in_list(list, fullname):
29 """Find test in list.
31 :param list: List with 2-tuples with regex and reason
32 """
33 for (regex, reason) in list:
34 if re.match(regex, fullname):
35 if reason is not None:
36 return reason
37 else:
38 return ""
39 return None
42 def read_test_regexes(f):
43 """Read tuples with regular expression and optional string from a file.
45 :param f: File-like object to read from
46 :return: Iterator over tuples with regular expression and test name
47 """
48 for l in f.readlines():
49 l = l.strip()
50 if l[0] == "#":
51 continue
52 try:
53 (test, reason) = l.split("#", 1)
54 except ValueError:
55 yield l, None
56 else:
57 yield test.strip(), reason.strip()
60 def should_run_test(tests, name):
61 if tests == []:
62 return True
63 for test in tests:
64 if re.match(test, name):
65 return True
66 return False
69 def read_testlist(inf, outf):
70 """Read a list of tests from a file.
72 :param inf: File-like object to read from.
73 :param outf: File-like object to write to.
74 :return: Iterator over tuples describing tests
75 """
76 while True:
77 l = inf.readline()
78 if l == '':
79 return
80 if l.startswith("-- TEST") and l.endswith(" --\n"):
81 supports_loadlist = l.startswith("-- TEST-LOADLIST")
82 supports_idlist = l.startswith("-- TEST-IDLIST")
83 name = inf.readline().rstrip("\n")
84 env = inf.readline().rstrip("\n")
85 cmdline = inf.readline().rstrip("\n")
86 yield (name, env, cmdline, supports_loadlist, supports_idlist)
87 else:
88 outf.write(l)
91 def read_restricted_test_list(f):
92 for l in f.readlines():
93 yield l.strip()
96 class RestrictedTestManager(object):
97 """Test manager which can filter individual tests that should be run."""
99 def __init__(self, test_list):
100 self.test_list = test_list
101 self.unused = set(self.test_list)
103 @classmethod
104 def from_path(cls, path):
105 f = open(path, 'r')
106 try:
107 return cls(read_restricted_test_list(f))
108 finally:
109 f.close()
111 def should_run_testsuite(self, name):
112 """Determine whether a testsuite should be run.
114 :param name: Name of the testsuite
115 :return: None if full testsuite should be run,
116 a list of subtests to run or [] if it should
117 not be run.
119 match = []
120 for r in self.test_list:
121 if r == name:
122 match = None
123 if r in self.unused:
124 self.unused.remove(r)
125 elif r.startswith(name + "."):
126 if match is not None:
127 match.append(r[len(name+"."):])
128 if r in self.unused:
129 self.unused.remove(r)
130 return match
132 def iter_unused(self):
133 """Iterate over entry entries that were unused.
135 :return: Iterator over test list entries that were not used.
137 return iter(self.unused)
140 def open_file_or_pipe(path, mode):
141 """Open a file or pipe.
143 :param path: Path to open; if it ends with | it is assumed to be a
144 command to run
145 :param mode: Mode with which to open it
146 :return: File-like object
148 if path.endswith("|"):
149 return os.popen(path[:-1], mode)
150 return open(path, mode)
153 def read_testlist_file(fn, outf=None):
154 """Read testlist file.
156 :param fn: Path to read (assumed to be a command to run if it ends with |)
157 :param outf: File-like object to pass non-test data through to
158 (defaults to stdout)
159 :return: Iterator over test suites (see read_testlist)
161 if outf is None:
162 outf = sys.stdout
163 inf = open_file_or_pipe(fn, 'r')
164 try:
165 for testsuite in read_testlist(inf, outf):
166 yield testsuite
167 finally:
168 inf.close()