/
runtests.py
910 lines (798 loc) · 34.9 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
#!/usr/bin/python
import os, sys, re, shutil, unittest, doctest
WITH_CYTHON = True
from distutils.dist import Distribution
from distutils.core import Extension
from distutils.command.build_ext import build_ext as _build_ext
distutils_distro = Distribution()
TEST_DIRS = ['compile', 'errors', 'run', 'pyregr']
TEST_RUN_DIRS = ['run', 'pyregr']
# Lists external modules, and a matcher matching tests
# which should be excluded if the module is not present.
EXT_DEP_MODULES = {
'numpy' : re.compile('.*\.numpy_.*').match
}
def get_numpy_include_dirs():
import numpy
return [numpy.get_include()]
EXT_DEP_INCLUDES = [
# test name matcher , callable returning list
(re.compile('numpy_.*').match, get_numpy_include_dirs),
]
VER_DEP_MODULES = {
# such as:
# (2,4) : lambda x: x in ['run.set']
}
INCLUDE_DIRS = [ d for d in os.getenv('INCLUDE', '').split(os.pathsep) if d ]
CFLAGS = os.getenv('CFLAGS', '').split()
class build_ext(_build_ext):
def build_extension(self, ext):
if ext.language == 'c++':
try:
self.compiler.compiler_so.remove('-Wstrict-prototypes')
except Exception:
pass
_build_ext.build_extension(self, ext)
class ErrorWriter(object):
match_error = re.compile('(warning:)?(?:.*:)?\s*([-0-9]+)\s*:\s*([-0-9]+)\s*:\s*(.*)').match
def __init__(self):
self.output = []
self.write = self.output.append
def _collect(self, collect_errors, collect_warnings):
s = ''.join(self.output)
result = []
for line in s.split('\n'):
match = self.match_error(line)
if match:
is_warning, line, column, message = match.groups()
if (is_warning and collect_warnings) or \
(not is_warning and collect_errors):
result.append( (int(line), int(column), message.strip()) )
result.sort()
return [ "%d:%d: %s" % values for values in result ]
def geterrors(self):
return self._collect(True, False)
def getwarnings(self):
return self._collect(False, True)
def getall(self):
return self._collect(True, True)
class TestBuilderBase(object):
def __init__(self, *args, **kwargs):
pass
def build_suite(self):
pass
class FwrapTestBuilder(object):
def __init__(self, rootdir, workdir, selectors, exclude_selectors,
cleanup_workdir, cleanup_sharedlibs):
self.rootdir = rootdir
self.workdir = workdir
self.selectors = selectors
self.exclude_selectors = exclude_selectors
self.cleanup_workdir = cleanup_workdir
self.cleanup_sharedlibs = cleanup_sharedlibs
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 = FwrapCompileTestCase
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)
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):
self.directory = directory
self.workdir = workdir
self.filename = filename
self.cleanup_workdir = cleanup_workdir
self.cleanup_sharedlibs = cleanup_sharedlibs
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):
# if not self.cleanup_workdir:
# if rmfile.lower().startswith("wrap") or rmfile.lower().startswith("autoconfig"):
# continue
# if not self.cleanup_sharedlibs and rmfile.endswith(".so") or rmfile.endswith(".dll"):
# continue
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):
self.projname = os.path.splitext(self.filename)[0] + '_fwrap'
self.projdir = os.path.join(self.workdir, self.projname)
self.wrapped_filename = self.projname+'_fortran.f95'
self.fwrap_config_source='genconfig.f95'
self.fwrap_config_module_source='config.f95'
self.fwrap_cython_source=self.projname+'.pyx'
fq_fname = os.path.join(os.path.abspath(self.directory), self.filename)
wrap([fq_fname], self.directory, self.workdir, self.projname)
# self.runCompileTest()
self.runCompileTest_distutils()
def runCompileTest(self):
from subprocess import Popen, PIPE, STDOUT, CalledProcessError
p = Popen(['make'], cwd=self.projdir, close_fds=True,
stdout=PIPE, stderr=STDOUT)
output = p.communicate()[0]
if p.returncode:
raise CalledProcessError(p.returncode, "make")
def runCompileTest_distutils(self):
import sys
CYTHON_DIR = os.path.abspath(os.path.join(os.path.pardir, os.path.pardir))
FILENAME = self.filename
WRAPPED_FILENAME = self.wrapped_filename
PROJNAME = self.projname
FWRAP_CONFIG_SOURCE = self.fwrap_config_source
FWRAP_CONFIG_MODULE_SOURCE = self.fwrap_config_module_source
FWRAP_CYTHON_SOURCE = self.fwrap_cython_source
setup_source = '''
import os,sys
sys.path.insert(0, '%(CYTHON_DIR)s')
from fwrap_setup import FwrapExtension, fwrap_build_ext, setup
sources = ['%(FILENAME)s', '%(WRAPPED_FILENAME)s']
ext = FwrapExtension(
'%(PROJNAME)s',
sources= ['%(FILENAME)s', '%(WRAPPED_FILENAME)s'],
fwrap_config_sources=[('%(FWRAP_CONFIG_SOURCE)s', '%(FWRAP_CONFIG_MODULE_SOURCE)s')],
fwrap_cython_sources=['%(FWRAP_CYTHON_SOURCE)s'],
)
setup(cmdclass={'build_ext' : fwrap_build_ext},
ext_modules = [ext])
''' % locals()
setup_fqpath = os.path.join(self.projdir, 'setup.py')
f = open(setup_fqpath,'w')
f.write(setup_source)
f.close()
shutil.copy('fwrap_setup.py', self.projdir)
shutil.copy(os.path.join(self.directory, self.filename), self.projdir)
from distutils.core import run_setup
thisdir = os.path.abspath(os.curdir)
try:
os.chdir(self.projdir)
orig_stdout, orig_stderr = sys.stdout, sys.stderr
sys.stdout = _devnull()
sys.stderr = _devnull()
run_setup(setup_fqpath, script_args=['build_ext', '--fcompiler=gnu95', '-lgfortran', '--inplace'])
finally:
os.chdir(thisdir)
sys.stdout,sys.stderr = orig_stdout, orig_stderr
def build_target_filenames(self, filename):
fortran_wrapper = "wrap_%s" % filename
c_header = "wrap_%s.h" % os.path.splitext(filename)[0]
return (fortran_wrapper, c_header)
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 TestBuilder(object):
def __init__(self, rootdir, workdir, selectors, exclude_selectors, annotate,
cleanup_workdir, cleanup_sharedlibs, with_pyregr, cython_only,
languages, test_bugs):
self.rootdir = rootdir
self.workdir = workdir
self.selectors = selectors
self.exclude_selectors = exclude_selectors
self.annotate = annotate
self.cleanup_workdir = cleanup_workdir
self.cleanup_sharedlibs = cleanup_sharedlibs
self.with_pyregr = with_pyregr
self.cython_only = cython_only
self.languages = languages
self.test_bugs = test_bugs
def build_suite(self):
suite = unittest.TestSuite()
test_dirs = TEST_DIRS
filenames = os.listdir(self.rootdir)
filenames.sort()
for filename in filenames:
if not WITH_CYTHON and filename == "errors":
# we won't get any errors without running Cython
continue
path = os.path.join(self.rootdir, filename)
if os.path.isdir(path) and filename in test_dirs:
if filename == 'pyregr' and not self.with_pyregr:
continue
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)
expect_errors = (context == 'errors')
suite = unittest.TestSuite()
filenames = os.listdir(path)
filenames.sort()
for filename in filenames:
if not (filename.endswith(".pyx") or filename.endswith(".py")):
continue
if filename.startswith('.'): continue # certain emacs backup files
if context == 'pyregr' and not filename.startswith('test_'):
continue
module = os.path.splitext(filename)[0]
fqmodule = "%s.%s" % (context, module)
if not [ 1 for match in self.selectors
if match(fqmodule) ]:
continue
if self.exclude_selectors:
if [1 for match in self.exclude_selectors if match(fqmodule)]:
continue
if context in TEST_RUN_DIRS:
if module.startswith("test_"):
test_class = CythonUnitTestCase
else:
test_class = CythonRunTestCase
else:
test_class = CythonCompileTestCase
for test in self.build_tests(test_class, path, workdir,
module, expect_errors):
suite.addTest(test)
return suite
def build_tests(self, test_class, path, workdir, module, expect_errors):
if expect_errors:
languages = self.languages[:1]
else:
languages = self.languages
if 'cpp' in module and 'c' in languages:
languages = list(languages)
languages.remove('c')
tests = [ self.build_test(test_class, path, workdir, module,
language, expect_errors)
for language in languages ]
return tests
def build_test(self, test_class, path, workdir, module,
language, expect_errors):
workdir = os.path.join(workdir, language)
if not os.path.exists(workdir):
os.makedirs(workdir)
return test_class(path, workdir, module,
language=language,
expect_errors=expect_errors,
annotate=self.annotate,
cleanup_workdir=self.cleanup_workdir,
cleanup_sharedlibs=self.cleanup_sharedlibs,
cython_only=self.cython_only)
class CythonCompileTestCase(unittest.TestCase):
def __init__(self, directory, workdir, module, language='c',
expect_errors=False, annotate=False, cleanup_workdir=True,
cleanup_sharedlibs=True, cython_only=False):
self.directory = directory
self.workdir = workdir
self.module = module
self.language = language
self.expect_errors = expect_errors
self.annotate = annotate
self.cleanup_workdir = cleanup_workdir
self.cleanup_sharedlibs = cleanup_sharedlibs
self.cython_only = cython_only
unittest.TestCase.__init__(self)
def shortDescription(self):
return "compiling (%s) %s" % (self.language, self.module)
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
try:
del sys.modules[self.module]
except KeyError:
pass
cleanup_c_files = WITH_CYTHON and self.cleanup_workdir
cleanup_lib_files = self.cleanup_sharedlibs
if os.path.exists(self.workdir):
for rmfile in os.listdir(self.workdir):
if not cleanup_c_files:
if rmfile[-2:] in (".c", ".h") or rmfile[-4:] == ".cpp":
continue
if not cleanup_lib_files and rmfile.endswith(".so") or rmfile.endswith(".dll"):
continue
if self.annotate and rmfile.endswith(".html"):
continue
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.workdir)
def runTest(self):
self.runCompileTest()
def runCompileTest(self):
self.compile(self.directory, self.module, self.workdir,
self.directory, self.expect_errors, self.annotate)
def find_module_source_file(self, source_file):
if not os.path.exists(source_file):
source_file = source_file[:-1]
return source_file
def build_target_filename(self, module_name):
target = '%s.%s' % (module_name, self.language)
return target
def split_source_and_output(self, directory, module, workdir):
source_file = os.path.join(directory, module) + '.pyx'
source_and_output = open(
self.find_module_source_file(source_file), 'rU')
out = open(os.path.join(workdir, module + '.pyx'), 'w')
for line in source_and_output:
last_line = line
if line.startswith("_ERRORS"):
out.close()
out = ErrorWriter()
else:
out.write(line)
try:
geterrors = out.geterrors
except AttributeError:
return []
else:
return geterrors()
def run_cython(self, directory, module, targetdir, incdir, annotate):
include_dirs = INCLUDE_DIRS[:]
if incdir:
include_dirs.append(incdir)
source = self.find_module_source_file(
os.path.join(directory, module + '.pyx'))
target = os.path.join(targetdir, self.build_target_filename(module))
options = CompilationOptions(
pyrex_default_options,
include_path = include_dirs,
output_file = target,
annotate = annotate,
use_listing_file = False,
cplus = self.language == 'cpp',
generate_pxi = False)
cython_compile(source, options=options,
full_module_name=module)
def run_distutils(self, module, workdir, incdir):
cwd = os.getcwd()
os.chdir(workdir)
try:
build_extension = build_ext(distutils_distro)
build_extension.include_dirs = INCLUDE_DIRS[:]
if incdir:
build_extension.include_dirs.append(incdir)
build_extension.finalize_options()
ext_include_dirs = []
for match, get_additional_include_dirs in EXT_DEP_INCLUDES:
if match(module):
ext_include_dirs += get_additional_include_dirs()
extension = Extension(
module,
sources = [self.build_target_filename(module)],
include_dirs = ext_include_dirs,
extra_compile_args = CFLAGS,
)
if self.language == 'cpp':
extension.language = 'c++'
build_extension.extensions = [extension]
build_extension.build_temp = workdir
build_extension.build_lib = workdir
build_extension.run()
finally:
os.chdir(cwd)
def compile(self, directory, module, workdir, incdir,
expect_errors, annotate):
expected_errors = errors = ()
if expect_errors:
expected_errors = self.split_source_and_output(
directory, module, workdir)
directory = workdir
if WITH_CYTHON:
old_stderr = sys.stderr
try:
sys.stderr = ErrorWriter()
self.run_cython(directory, module, workdir, incdir, annotate)
errors = sys.stderr.geterrors()
finally:
sys.stderr = old_stderr
if errors or expected_errors:
for expected, error in zip(expected_errors, errors):
self.assertEquals(expected, error)
if len(errors) < len(expected_errors):
expected_error = expected_errors[len(errors)]
self.assertEquals(expected_error, None)
elif len(errors) > len(expected_errors):
unexpected_error = errors[len(expected_errors)]
self.assertEquals(None, unexpected_error)
else:
if not self.cython_only:
self.run_distutils(module, workdir, incdir)
class CythonRunTestCase(CythonCompileTestCase):
def shortDescription(self):
return "compiling (%s) and running %s" % (self.language, self.module)
def run(self, result=None):
if result is None:
result = self.defaultTestResult()
result.startTest(self)
try:
self.setUp()
self.runCompileTest()
if not self.cython_only:
doctest.DocTestSuite(self.module).run(result)
except Exception:
result.addError(self, sys.exc_info())
result.stopTest(self)
try:
self.tearDown()
except Exception:
pass
class CythonUnitTestCase(CythonCompileTestCase):
def shortDescription(self):
return "compiling (%s) tests in %s" % (self.language, self.module)
def run(self, result=None):
if result is None:
result = self.defaultTestResult()
result.startTest(self)
try:
self.setUp()
self.runCompileTest()
unittest.defaultTestLoader.loadTestsFromName(self.module).run(result)
except Exception:
result.addError(self, sys.exc_info())
result.stopTest(self)
try:
self.tearDown()
except Exception:
pass
def collect_unittests(path, module_prefix, suite, selectors):
def file_matches(filename):
return filename.startswith("Test") and filename.endswith(".py")
def package_matches(dirname):
return dirname == "Tests"
loader = unittest.TestLoader()
skipped_dirs = []
for dirpath, dirnames, filenames in os.walk(path):
if dirpath != path and "__init__.py" not in filenames:
skipped_dirs.append(dirpath + os.path.sep)
continue
skip = False
for dir in skipped_dirs:
if dirpath.startswith(dir):
skip = True
if skip:
continue
parentname = os.path.split(dirpath)[-1]
if package_matches(parentname):
for f in filenames:
if file_matches(f):
filepath = os.path.join(dirpath, f)[:-len(".py")]
modulename = module_prefix + filepath[len(path)+1:].replace(os.path.sep, '.')
if not [ 1 for match in selectors if match(modulename) ]:
continue
module = __import__(modulename)
for x in modulename.split('.')[1:]:
module = getattr(module, x)
suite.addTests([loader.loadTestsFromModule(module)])
def collect_doctests(path, module_prefix, suite, selectors):
def package_matches(dirname):
return dirname not in ("Mac", "Distutils", "Plex")
def file_matches(filename):
return (filename.endswith(".py") and not ('~' in filename
or '#' in filename or filename.startswith('.')))
import doctest, types
for dirpath, dirnames, filenames in os.walk(path):
parentname = os.path.split(dirpath)[-1]
if package_matches(parentname):
for f in filenames:
if file_matches(f):
if not f.endswith('.py'): continue
filepath = os.path.join(dirpath, f)[:-len(".py")]
modulename = module_prefix + filepath[len(path)+1:].replace(os.path.sep, '.')
if not [ 1 for match in selectors if match(modulename) ]:
continue
module = __import__(modulename)
for x in modulename.split('.')[1:]:
module = getattr(module, x)
if hasattr(module, "__doc__") or hasattr(module, "__test__"):
try:
suite.addTest(doctest.DocTestSuite(module))
except ValueError: # no tests
pass
class MissingDependencyExcluder:
def __init__(self, deps):
# deps: { module name : matcher func }
self.exclude_matchers = []
for mod, matcher in deps.items():
try:
__import__(mod)
except ImportError:
self.exclude_matchers.append(matcher)
self.tests_missing_deps = []
def __call__(self, testname):
for matcher in self.exclude_matchers:
if matcher(testname):
self.tests_missing_deps.append(testname)
return True
return False
class VersionDependencyExcluder:
def __init__(self, deps):
# deps: { version : matcher func }
from sys import version_info
self.exclude_matchers = []
for ver, matcher in deps.items():
if version_info < ver:
self.exclude_matchers.append(matcher)
self.tests_missing_deps = []
def __call__(self, testname):
for matcher in self.exclude_matchers:
if matcher(testname):
self.tests_missing_deps.append(testname)
return True
return False
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("--no-cython", dest="with_cython",
# action="store_false", default=True,
# help="do not run the Cython compiler, only the C compiler")
# parser.add_option("--no-c", dest="use_c",
# action="store_false", default=True,
# help="do not test C compilation")
# parser.add_option("--no-cpp", dest="use_cpp",
# action="store_false", default=True,
# help="do not test C++ compilation")
# parser.add_option("--no-unit", dest="unittests",
# action="store_false", default=True,
# help="do not run the unit tests")
# parser.add_option("--no-doctest", dest="doctests",
# action="store_false", default=True,
# help="do not run the doctests")
# parser.add_option("--no-file", dest="filetests",
# action="store_false", default=True,
# help="do not run the file based tests")
# parser.add_option("--no-pyregr", dest="pyregr",
# action="store_false", default=True,
# help="do not run the regression tests of CPython in tests/pyregr/")
# parser.add_option("--cython-only", dest="cython_only",
# action="store_true", default=False,
# help="only compile pyx to c, do not run C compiler or run the tests")
# parser.add_option("--no-refnanny", dest="with_refnanny",
# action="store_false", default=True,
# help="do not regression test reference counting")
# parser.add_option("--sys-pyregr", dest="system_pyregr",
# action="store_true", default=False,
# help="run the regression tests of the CPython installation")
parser.add_option("-x", "--exclude", dest="exclude",
action="append", metavar="PATTERN",
help="exclude tests matching the PATTERN")
# parser.add_option("-C", "--coverage", dest="coverage",
# action="store_true", default=False,
# help="collect source coverage data for the Compiler")
# parser.add_option("-A", "--annotate", dest="annotate_source",
# action="store_true", default=True,
# help="generate annotated HTML versions of the test source files")
# parser.add_option("--no-annotate", dest="annotate_source",
# action="store_false",
# help="do not generate annotated HTML versions of the test source files")
parser.add_option("-v", "--verbose", dest="verbosity",
action="count", default=0,
help="display test progress, pass twice to print test names")
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()
if 0:
if sys.version_info[0] >= 3:
# make sure we do not import (or run) Cython itself
options.doctests = False
options.with_cython = False
options.unittests = False
options.pyregr = False
if options.coverage:
import coverage
coverage.erase()
coverage.start()
WITH_CYTHON = options.with_cython
if WITH_CYTHON:
from Cython.Compiler.Main import \
CompilationOptions, \
default_options as pyrex_default_options, \
compile as cython_compile
from Cython.Compiler import Errors
Errors.LEVEL = 0 # show all warnings
# if 0
# RUN ALL TESTS!
ROOTDIR = os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), 'tests')
WORKDIR = os.path.join(os.getcwd(), 'BUILD')
# UNITTEST_MODULE = "Cython"
# UNITTEST_ROOT = os.path.join(os.getcwd(), UNITTEST_MODULE)
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)
if 0:
if WITH_CYTHON:
from Cython.Compiler.Version import version
sys.stderr.write("Running tests against Cython %s\n" % version)
from Cython.Compiler import DebugFlags
DebugFlags.debug_temp_code_comments = 1
else:
sys.stderr.write("Running tests without Cython.\n")
#if 0
from Main import wrap
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)
if 0:
if options.with_refnanny:
from pyximport.pyxbuild import pyx_to_dll
libpath = pyx_to_dll(os.path.join("Cython", "Runtime", "refnanny.pyx"),
build_in_temp=True,
pyxbuild_dir=os.path.join(WORKDIR, "support"))
sys.path.insert(0, os.path.split(libpath)[0])
CFLAGS.append("-DCYTHON_REFNANNY")
#if 0
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
import re
selectors = [ re.compile(r, re.I|re.U).search for r in cmd_args ]
if not selectors:
selectors = [ lambda x:True ]
# Chech 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 0:
missing_dep_excluder = MissingDependencyExcluder(EXT_DEP_MODULES)
version_dep_excluder = VersionDependencyExcluder(VER_DEP_MODULES)
exclude_selectors = [missing_dep_excluder, version_dep_excluder] # want to pring msg at exit
if not test_bugs:
exclude_selectors += [ FileListExcluder("tests/bugs.txt") ]
if 0:
languages = []
if options.use_c:
languages.append('c')
if options.use_cpp:
languages.append('cpp')
# if 0
test_suite = unittest.TestSuite()
if 0:
if options.unittests:
collect_unittests(UNITTEST_ROOT, UNITTEST_MODULE + ".", test_suite, selectors)
if options.doctests:
collect_doctests(UNITTEST_ROOT, UNITTEST_MODULE + ".", test_suite, selectors)
# if 0
filetests = FwrapTestBuilder(ROOTDIR, WORKDIR, selectors, exclude_selectors,
options.cleanup_workdir, options.cleanup_sharedlibs)
test_suite.addTest(filetests.build_suite())
if 0:
if options.filetests and languages:
filetests = TestBuilder(ROOTDIR, WORKDIR, selectors, exclude_selectors,
options.annotate_source, options.cleanup_workdir,
options.cleanup_sharedlibs, options.pyregr,
options.cython_only, languages, test_bugs)
test_suite.addTest(filetests.build_suite())
if options.system_pyregr and languages:
filetests = TestBuilder(ROOTDIR, WORKDIR, selectors, exclude_selectors,
options.annotate_source, options.cleanup_workdir,
options.cleanup_sharedlibs, True,
options.cython_only, languages, test_bugs)
test_suite.addTest(
filetests.handle_directory(
os.path.join(sys.prefix, 'lib', 'python'+sys.version[:3], 'test'),
'pyregr'))
unittest.TextTestRunner(verbosity=options.verbosity).run(test_suite)
if 0:
if options.coverage:
coverage.stop()
ignored_modules = ('Options', 'Version', 'DebugFlags', 'CmdLine')
modules = [ module for name, module in sys.modules.items()
if module is not None and
name.startswith('Cython.Compiler.') and
name[len('Cython.Compiler.'):] not in ignored_modules ]
coverage.report(modules, show_missing=0)
if missing_dep_excluder.tests_missing_deps:
sys.stderr.write("Following tests excluded because of missing dependencies on your system:\n")
for test in missing_dep_excluder.tests_missing_deps:
sys.stderr.write(" %s\n" % test)
if options.with_refnanny:
import refnanny
sys.stderr.write("\n".join([repr(x) for x in refnanny.reflog]))