Commit f9baac12 authored by Stefan Behnel's avatar Stefan Behnel

cleanup for test runner: use optparse to read cmd line options

parent fc3249dc
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
import os, sys, re, shutil, unittest, doctest import os, sys, re, shutil, unittest, doctest
WITH_CYTHON = True WITH_CYTHON = True
CLEANUP_WORKDIR = True
from distutils.dist import Distribution from distutils.dist import Distribution
from distutils.core import Extension from distutils.core import Extension
...@@ -34,11 +33,12 @@ class ErrorWriter(object): ...@@ -34,11 +33,12 @@ class ErrorWriter(object):
return errors return errors
class TestBuilder(object): class TestBuilder(object):
def __init__(self, rootdir, workdir, selectors, annotate): def __init__(self, rootdir, workdir, selectors, annotate, cleanup_workdir):
self.rootdir = rootdir self.rootdir = rootdir
self.workdir = workdir self.workdir = workdir
self.selectors = selectors self.selectors = selectors
self.annotate = annotate self.annotate = annotate
self.cleanup_workdir = cleanup_workdir
def build_suite(self): def build_suite(self):
suite = unittest.TestSuite() suite = unittest.TestSuite()
...@@ -75,28 +75,34 @@ class TestBuilder(object): ...@@ -75,28 +75,34 @@ class TestBuilder(object):
continue continue
if context in TEST_RUN_DIRS: if context in TEST_RUN_DIRS:
test = CythonRunTestCase( test = CythonRunTestCase(
path, workdir, module, self.annotate) path, workdir, module,
annotate=self.annotate,
cleanup_workdir=self.cleanup_workdir)
else: else:
test = CythonCompileTestCase( test = CythonCompileTestCase(
path, workdir, module, expect_errors, self.annotate) path, workdir, module,
expect_errors=expect_errors,
annotate=self.annotate,
cleanup_workdir=self.cleanup_workdir)
suite.addTest(test) suite.addTest(test)
return suite return suite
class CythonCompileTestCase(unittest.TestCase): class CythonCompileTestCase(unittest.TestCase):
def __init__(self, directory, workdir, module, def __init__(self, directory, workdir, module,
expect_errors=False, annotate=False): expect_errors=False, annotate=False, cleanup_workdir=True):
self.directory = directory self.directory = directory
self.workdir = workdir self.workdir = workdir
self.module = module self.module = module
self.expect_errors = expect_errors self.expect_errors = expect_errors
self.annotate = annotate self.annotate = annotate
self.cleanup_workdir = cleanup_workdir
unittest.TestCase.__init__(self) unittest.TestCase.__init__(self)
def shortDescription(self): def shortDescription(self):
return "compiling " + self.module return "compiling " + self.module
def tearDown(self): def tearDown(self):
cleanup_c_files = WITH_CYTHON and CLEANUP_WORKDIR cleanup_c_files = WITH_CYTHON and self.cleanup_workdir
if os.path.exists(self.workdir): if os.path.exists(self.workdir):
for rmfile in os.listdir(self.workdir): for rmfile in os.listdir(self.workdir):
if not cleanup_c_files and rmfile[-2:] in (".c", ".h"): if not cleanup_c_files and rmfile[-2:] in (".c", ".h"):
...@@ -221,12 +227,22 @@ class CythonRunTestCase(CythonCompileTestCase): ...@@ -221,12 +227,22 @@ class CythonRunTestCase(CythonCompileTestCase):
pass pass
if __name__ == '__main__': if __name__ == '__main__':
try: from optparse import OptionParser
sys.argv.remove("--no-cython") parser = OptionParser()
except ValueError: parser.add_option("--no-cython", dest="with_cython",
WITH_CYTHON = True action="store_false", default=True)
else: parser.add_option("--no-cleanup", dest="cleanup_workdir",
WITH_CYTHON = False action="store_false", default=True)
parser.add_option("-C", "--coverage", dest="coverage",
action="store_true", default=False)
parser.add_option("-A", "--annotate-source", dest="annotate_source",
action="store_true", default=False)
parser.add_option("-v", "--verbose", dest="verbosity",
action="count", default=0)
options, cmd_args = parser.parse_args()
WITH_CYTHON = options.with_cython
if WITH_CYTHON: if WITH_CYTHON:
from Cython.Compiler.Main import \ from Cython.Compiler.Main import \
...@@ -250,64 +266,31 @@ if __name__ == '__main__': ...@@ -250,64 +266,31 @@ if __name__ == '__main__':
if WITH_CYTHON: if WITH_CYTHON:
from Cython.Compiler.Version import version from Cython.Compiler.Version import version
from Cython.Compiler.Main import \
CompilationOptions, \
default_options as pyrex_default_options, \
compile as cython_compile
print("Running tests against Cython %s" % version) print("Running tests against Cython %s" % version)
else: else:
print("Running tests without Cython.") print("Running tests without Cython.")
print("Python %s" % sys.version) print("Python %s" % sys.version)
print("") print("")
try:
sys.argv.remove("-C")
except ValueError:
coverage = None
else:
import coverage
coverage.erase()
try:
sys.argv.remove("--no-cleanup")
except ValueError:
CLEANUP_WORKDIR = True
else:
CLEANUP_WORKDIR = False
try:
sys.argv.remove("-a")
except ValueError:
annotate_source = False
else:
annotate_source = True
try:
sys.argv.remove("-vv")
except ValueError:
try:
sys.argv.remove("-v")
except ValueError:
verbosity = 0
else:
verbosity = 1
else:
verbosity = 2
import re import re
selectors = [ re.compile(r, re.I|re.U).search for r in sys.argv[1:] ] selectors = [ re.compile(r, re.I|re.U).search for r in cmd_args ]
if not selectors: if not selectors:
selectors = [ lambda x:True ] selectors = [ lambda x:True ]
tests = TestBuilder(ROOTDIR, WORKDIR, selectors, annotate_source) if options.coverage:
import coverage
coverage.erase()
tests = TestBuilder(ROOTDIR, WORKDIR, selectors,
options.annotate_source, options.cleanup_workdir)
test_suite = tests.build_suite() test_suite = tests.build_suite()
if coverage is not None: if options.coverage:
coverage.start() coverage.start()
unittest.TextTestRunner(verbosity=verbosity).run(test_suite) unittest.TextTestRunner(verbosity=options.verbosity).run(test_suite)
if coverage is not None: if options.coverage:
coverage.stop() coverage.stop()
ignored_modules = ('Options', 'Version', 'DebugFlags') ignored_modules = ('Options', 'Version', 'DebugFlags')
modules = [ module for name, module in sys.modules.items() modules = [ module for name, module in sys.modules.items()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment