-
Notifications
You must be signed in to change notification settings - Fork 14
/
runtests.py
241 lines (199 loc) · 8.65 KB
/
runtests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/python
import os, sys, re, shutil, unittest, doctest
WITH_CYTHON = True
TEST_DIRS = ['compile', 'errors', 'run', 'pyregr']
TEST_RUN_DIRS = ['run', 'pyregr']
class FwrapTestBuilder(object):
def __init__(self, rootdir, workdir, selectors, exclude_selectors,
cleanup_workdir, cleanup_sharedlibs, verbosity=0):
self.rootdir = rootdir
self.workdir = workdir
self.selectors = selectors
self.exclude_selectors = exclude_selectors
self.cleanup_workdir = cleanup_workdir
self.cleanup_sharedlibs = cleanup_sharedlibs
self.verbosity = verbosity
def build_suite(self):
suite = unittest.TestSuite()
test_dirs = TEST_DIRS
filenames = os.listdir(self.rootdir)
filenames.sort()
for filename in filenames:
path = os.path.join(self.rootdir, filename)
if os.path.isdir(path) and filename in test_dirs:
suite.addTest(
self.handle_directory(path, filename))
return suite
def handle_directory(self, path, context):
workdir = os.path.join(self.workdir, context)
if not os.path.exists(workdir):
os.makedirs(workdir)
suite = unittest.TestSuite()
filenames = os.listdir(path)
filenames.sort()
for filename in filenames:
if os.path.splitext(filename)[1].lower() not in (".f", ".f77", ".f90", ".f95"):
continue
if filename.startswith('.'): continue # certain emacs backup files
basename = os.path.splitext(filename)[0]
fqbasename = "%s.%s" % (context, basename)
if not [1 for match in self.selectors if match(fqbasename)]:
continue
if self.exclude_selectors:
if [1 for match in self.exclude_selectors if match(fqbasename)]:
continue
if context in TEST_RUN_DIRS:
test_class = FwrapRunTestCase
else:
test_class = FwrapCompileTestCase
suite.addTest(self.build_test(test_class, path, workdir, filename))
return suite
def build_test(self, test_class, path, workdir, filename):
return test_class(path, workdir, filename,
cleanup_workdir=self.cleanup_workdir,
cleanup_sharedlibs=self.cleanup_sharedlibs,
verbosity=self.verbosity)
class _devnull(object):
def flush(self): pass
def write(self, s): pass
def read(self): return ''
class FwrapCompileTestCase(unittest.TestCase):
def __init__(self, directory, workdir, filename,
cleanup_workdir=True, cleanup_sharedlibs=True,
verbosity=0):
self.directory = directory
self.workdir = workdir
self.filename = filename
self.cleanup_workdir = cleanup_workdir
self.cleanup_sharedlibs = cleanup_sharedlibs
self.verbosity = verbosity
unittest.TestCase.__init__(self)
def shortDescription(self):
return "wrapping %s" % self.filename
def setUp(self):
if self.workdir not in sys.path:
sys.path.insert(0, self.workdir)
def tearDown(self):
try:
sys.path.remove(self.workdir)
except ValueError:
pass
if os.path.exists(self.workdir):
if self.cleanup_workdir:
for rmfile in os.listdir(self.workdir):
try:
rmfile = os.path.join(self.workdir, rmfile)
if os.path.isdir(rmfile):
shutil.rmtree(rmfile, ignore_errors=True)
else:
os.remove(rmfile)
except IOError:
pass
else:
os.makedirs(self.workdirs)
def runTest(self):
# fwrapc.py configure build fsrc...
self.projname = os.path.splitext(self.filename)[0] + '_fwrap'
self.projdir = os.path.join(self.workdir, self.projname)
fq_fname = os.path.join(os.path.abspath(self.directory), self.filename)
argv = ['configure', 'build',
'--name=%s' % self.projname,
'--outdir=%s' % self.projdir,
fq_fname,
'install']
fwrapc(argv=argv)
def compile(self, directory, filename, workdir, incdir):
self.run_wrapper(directory, filename, workdir, incdir)
def run_wrapper(self, directory, filename, workdir, incdir):
wrap(filename, directory, workdir)
class FwrapRunTestCase(FwrapCompileTestCase):
def shortDescription(self):
return "compiling and running %s" % self.filename
def run(self, result=None):
if result is None:
result = self.defaultTestResult()
result.startTest(self)
try:
self.setUp()
self.runTest()
if self.projdir not in sys.path:
sys.path.insert(0, self.projdir)
doctest_mod_base = self.projname+'_doctest'
doctest_mod_fqpath = os.path.join(self.directory, doctest_mod_base+'.py')
shutil.copy(doctest_mod_fqpath, self.projdir)
doctest.DocTestSuite(self.projname+'_doctest').run(result) #??
except Exception:
result.addError(self, sys.exc_info())
result.stopTest(self)
try:
self.tearDown()
except Exception:
pass
class FileListExcluder:
def __init__(self, list_file):
self.excludes = {}
for line in open(list_file).readlines():
line = line.strip()
if line and line[0] != '#':
self.excludes[line.split()[0]] = True
def __call__(self, testname):
return testname.split('.')[-1] in self.excludes
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option("--no-cleanup", dest="cleanup_workdir",
action="store_false", default=True,
help="do not delete the generated C files (allows passing --no-cython on next run)")
parser.add_option("--no-cleanup-sharedlibs", dest="cleanup_sharedlibs",
action="store_false", default=True,
help="do not delete the generated shared libary files (allows manual module experimentation)")
parser.add_option("-x", "--exclude", dest="exclude",
action="append", metavar="PATTERN",
help="exclude tests matching the PATTERN")
parser.add_option("-v", "--verbose", dest="verbosity",
action="count",
default=0,
help="display test progress, more v's for more output")
parser.add_option("-T", "--ticket", dest="tickets",
action="append",
help="a bug ticket number to run the respective test in 'tests/bugs'")
options, cmd_args = parser.parse_args()
# RUN ALL TESTS!
ROOTDIR = os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), 'tests')
WORKDIR = os.path.join(os.getcwd(), 'BUILD')
if os.path.exists(WORKDIR):
for path in os.listdir(WORKDIR):
if path in ("support",): continue
shutil.rmtree(os.path.join(WORKDIR, path), ignore_errors=True)
if not os.path.exists(WORKDIR):
os.makedirs(WORKDIR)
from fwrap.fwrapc import fwrapc
sys.stderr.write("Python %s\n" % sys.version)
sys.stderr.write("\n")
# insert cython.py/Cython source directory into sys.path
cython_dir = os.path.abspath(os.path.join(os.path.pardir, os.path.pardir))
sys.path.insert(0, cython_dir)
test_bugs = False
if options.tickets:
for ticket_number in options.tickets:
test_bugs = True
cmd_args.append('.*T%s$' % ticket_number)
if not test_bugs:
for selector in cmd_args:
if selector.startswith('bugs'):
test_bugs = True
selectors = [ re.compile(r, re.I|re.U).search for r in cmd_args ]
if not selectors:
selectors = [ lambda x:True ]
# Check which external modules are not present and exclude tests
# which depends on them (by prefix)
exclude_selectors = []
if options.exclude:
exclude_selectors += [ re.compile(r, re.I|re.U).search for r in options.exclude ]
if not test_bugs:
exclude_selectors += [ FileListExcluder("tests/bugs.txt") ]
test_suite = unittest.TestSuite()
filetests = FwrapTestBuilder(ROOTDIR, WORKDIR, selectors, exclude_selectors,
options.cleanup_workdir, options.cleanup_sharedlibs, options.verbosity)
test_suite.addTest(filetests.build_suite())
unittest.TextTestRunner(verbosity=options.verbosity).run(test_suite)