Commit 386fa1d1 authored by Stefan Behnel's avatar Stefan Behnel

test runner: pass '-a' to get an annotated source version of the tests

parent bb72b52d
...@@ -37,14 +37,17 @@ class ErrorWriter(object): ...@@ -37,14 +37,17 @@ class ErrorWriter(object):
return errors return errors
class TestBuilder(object): class TestBuilder(object):
def __init__(self, rootdir, workdir, selectors): def __init__(self, rootdir, workdir, selectors, annotate):
self.rootdir = rootdir self.rootdir = rootdir
self.workdir = workdir self.workdir = workdir
self.selectors = selectors self.selectors = selectors
self.annotate = annotate
def build_suite(self): def build_suite(self):
suite = unittest.TestSuite() suite = unittest.TestSuite()
for filename in os.listdir(self.rootdir): filenames = os.listdir(self.rootdir)
filenames.sort()
for filename in filenames:
path = os.path.join(self.rootdir, filename) path = os.path.join(self.rootdir, filename)
if os.path.isdir(path) and filename in TEST_DIRS: if os.path.isdir(path) and filename in TEST_DIRS:
suite.addTest( suite.addTest(
...@@ -54,7 +57,9 @@ class TestBuilder(object): ...@@ -54,7 +57,9 @@ class TestBuilder(object):
def handle_directory(self, path, context): def handle_directory(self, path, context):
expect_errors = (context == 'errors') expect_errors = (context == 'errors')
suite = unittest.TestSuite() suite = unittest.TestSuite()
for filename in os.listdir(path): filenames = os.listdir(path)
filenames.sort()
for filename in filenames:
if not filename.endswith(".pyx"): if not filename.endswith(".pyx"):
continue continue
module = filename[:-4] module = filename[:-4]
...@@ -63,32 +68,46 @@ class TestBuilder(object): ...@@ -63,32 +68,46 @@ class TestBuilder(object):
if match(fqmodule) ]: if match(fqmodule) ]:
continue continue
if context in TEST_RUN_DIRS: if context in TEST_RUN_DIRS:
test = CythonRunTestCase(path, self.workdir, module) test = CythonRunTestCase(
path, self.workdir, module, self.annotate)
else: else:
test = CythonCompileTestCase( test = CythonCompileTestCase(
path, self.workdir, module, expect_errors) path, self.workdir, module, expect_errors, self.annotate)
suite.addTest(test) suite.addTest(test)
return suite return suite
class CythonCompileTestCase(unittest.TestCase): class CythonCompileTestCase(unittest.TestCase):
def __init__(self, directory, workdir, module, expect_errors=False): def __init__(self, directory, workdir, module,
expect_errors=False, annotate=False):
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
unittest.TestCase.__init__(self) unittest.TestCase.__init__(self)
def shortDescription(self): def shortDescription(self):
return "compiling " + self.module return "compiling " + self.module
def setUp(self): def tearDown(self):
if os.path.exists(self.workdir): if os.path.exists(self.workdir):
shutil.rmtree(self.workdir, ignore_errors=True) for rmfile in os.listdir(self.workdir):
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) os.makedirs(self.workdir)
def runTest(self): def runTest(self):
self.compile(self.directory, self.module, self.workdir, self.compile(self.directory, self.module, self.workdir,
self.directory, self.expect_errors) self.directory, self.expect_errors, self.annotate)
def split_source_and_output(self, directory, module, workdir): def split_source_and_output(self, directory, module, workdir):
source_and_output = open(os.path.join(directory, module + '.pyx'), 'rU') source_and_output = open(os.path.join(directory, module + '.pyx'), 'rU')
...@@ -107,7 +126,7 @@ class CythonCompileTestCase(unittest.TestCase): ...@@ -107,7 +126,7 @@ class CythonCompileTestCase(unittest.TestCase):
else: else:
return geterrors() return geterrors()
def run_cython(self, directory, module, targetdir, incdir): def run_cython(self, directory, module, targetdir, incdir, annotate):
include_dirs = INCLUDE_DIRS[:] include_dirs = INCLUDE_DIRS[:]
if incdir: if incdir:
include_dirs.append(incdir) include_dirs.append(incdir)
...@@ -117,6 +136,7 @@ class CythonCompileTestCase(unittest.TestCase): ...@@ -117,6 +136,7 @@ class CythonCompileTestCase(unittest.TestCase):
pyrex_default_options, pyrex_default_options,
include_path = include_dirs, include_path = include_dirs,
output_file = target, output_file = target,
annotate = annotate,
use_listing_file = False, cplus = False, generate_pxi = False) use_listing_file = False, cplus = False, generate_pxi = False)
cython_compile(source, options=options, cython_compile(source, options=options,
full_module_name=module) full_module_name=module)
...@@ -143,7 +163,8 @@ class CythonCompileTestCase(unittest.TestCase): ...@@ -143,7 +163,8 @@ class CythonCompileTestCase(unittest.TestCase):
finally: finally:
os.chdir(cwd) os.chdir(cwd)
def compile(self, directory, module, workdir, incdir, expect_errors): def compile(self, directory, module, workdir, incdir,
expect_errors, annotate):
expected_errors = errors = () expected_errors = errors = ()
if expect_errors: if expect_errors:
expected_errors = self.split_source_and_output( expected_errors = self.split_source_and_output(
...@@ -153,7 +174,7 @@ class CythonCompileTestCase(unittest.TestCase): ...@@ -153,7 +174,7 @@ class CythonCompileTestCase(unittest.TestCase):
old_stderr = sys.stderr old_stderr = sys.stderr
try: try:
sys.stderr = ErrorWriter() sys.stderr = ErrorWriter()
self.run_cython(directory, module, workdir, incdir) self.run_cython(directory, module, workdir, incdir, annotate)
errors = sys.stderr.geterrors() errors = sys.stderr.geterrors()
finally: finally:
sys.stderr = old_stderr sys.stderr = old_stderr
...@@ -184,12 +205,17 @@ class CythonRunTestCase(CythonCompileTestCase): ...@@ -184,12 +205,17 @@ class CythonRunTestCase(CythonCompileTestCase):
result.startTest(self) result.startTest(self)
result.addError(self, sys.exc_info()) result.addError(self, sys.exc_info())
result.stopTest(self) result.stopTest(self)
try:
self.tearDown()
except Exception:
pass
if __name__ == '__main__': if __name__ == '__main__':
# RUN ALL TESTS! # RUN ALL TESTS!
ROOTDIR = os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), 'tests') ROOTDIR = os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), 'tests')
WORKDIR = os.path.join(os.getcwd(), 'BUILD') WORKDIR = os.path.join(os.getcwd(), 'BUILD')
if not os.path.exists(WORKDIR): if os.path.exists(WORKDIR):
shutil.rmtree(WORKDIR, ignore_errors=True)
os.makedirs(WORKDIR) os.makedirs(WORKDIR)
if not sys.path or sys.path[0] != WORKDIR: if not sys.path or sys.path[0] != WORKDIR:
...@@ -207,12 +233,19 @@ if __name__ == '__main__': ...@@ -207,12 +233,19 @@ if __name__ == '__main__':
import coverage import coverage
coverage.erase() coverage.erase()
try:
sys.argv.remove("-a")
except ValueError:
annotate_source = False
else:
annotate_source = True
import re import re
selectors = [ re.compile(r, re.I).search for r in sys.argv[1:] ] selectors = [ re.compile(r, re.I).search for r in sys.argv[1:] ]
if not selectors: if not selectors:
selectors = [ lambda x:True ] selectors = [ lambda x:True ]
tests = TestBuilder(ROOTDIR, WORKDIR, selectors) tests = TestBuilder(ROOTDIR, WORKDIR, selectors, annotate_source)
test_suite = tests.build_suite() test_suite = tests.build_suite()
if coverage is not None: if coverage is not None:
......
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