Small improvements to the testing infra.

This commit is contained in:
Elmo 2010-08-26 11:05:04 +03:00
parent 67a7d374e4
commit c0476e501f
2 changed files with 32 additions and 26 deletions

View file

@ -4,18 +4,19 @@ from subprocess import Popen, PIPE
mod_re = (r"\bmodule\s+(", r")\s*\(\s*") mod_re = (r"\bmodule\s+(", r")\s*\(\s*")
func_re = (r"\bfunction\s+(", r")\s*\(") func_re = (r"\bfunction\s+(", r")\s*\(")
def extract_mod_names(fpath, name_re=r"\w+"): def extract_definitions(fpath, name_re=r"\w+", def_re=""):
regex = name_re.join(mod_re) regex = name_re.join(def_re)
matcher = re.compile(regex) matcher = re.compile(regex)
return (m.group(1) for m in matcher.finditer(fpath.read())) return (m.group(1) for m in matcher.finditer(fpath.read()))
def extract_mod_names(fpath, name_re=r"\w+"):
return extract_definitions(fpath, name_re=name_re, def_re=mod_re)
def extract_func_names(fpath, name_re=r"\w+"): def extract_func_names(fpath, name_re=r"\w+"):
regex = name_re.join(func_re) return extract_definitions(fpath, name_re=name_re, def_re=func_re)
matcher = re.compile(regex)
return (m.group(1) for m in matcher.finditer(fpath.read()))
def collect_test_modules(): def collect_test_modules(dirpath=None):
dirpath = py.path.local("./") dirpath = dirpath or py.path.local("./")
print "Collecting openscad test module names" print "Collecting openscad test module names"
test_files = {} test_files = {}
@ -26,21 +27,24 @@ def collect_test_modules():
test_files[fpath] = modules test_files[fpath] = modules
return test_files return test_files
collect_test_modules() class Timeout(Exception): pass
def call_openscad(path, stlpath, timeout=20): def call_openscad(path, stlpath, timeout=1):
try: try:
proc = Popen(['openscad', '-s', str(stlpath), str(path)], command = ['openscad', '-s', str(stlpath), str(path)]
print command
proc = Popen(command,
stdout=PIPE, stderr=PIPE, close_fds=True) stdout=PIPE, stderr=PIPE, close_fds=True)
calltime = time.time() calltime = time.time()
time.sleep(0.01)
#print calltime #print calltime
while True: while True:
if proc.poll() is not None: if proc.poll() is not None:
break break
time.sleep(0.1) time.sleep(0.5)
#print time.time() #print time.time()
if time.time() > calltime + timeout: if time.time() > calltime + timeout:
raise Exception("Timeout") raise Timeout()
finally: finally:
try: try:
proc.terminate() proc.terminate()

View file

@ -2,21 +2,22 @@ import py
from openscad_utils import * from openscad_utils import *
def pytest_generate_tests(metafunc):
if "modpath" in metafunc.funcargnames:
if "modname" in metafunc.funcargnames:
for fpath, modnames in collect_test_modules().items():
for modname in modnames:
metafunc.addcall(funcargs=dict(modname=modname, modpath=fpath))
else:
dirpath = py.path.local("./")
for fpath in dirpath.visit('*.scad'):
metafunc.addcall(funcargs=dict(modpath=fpath))
temppath = py.test.ensuretemp('MCAD') temppath = py.test.ensuretemp('MCAD')
def pytest_generate_tests(metafunc):
if "modpath" in metafunc.funcargnames:
for fpath, modnames in collect_test_modules().items():
os.system("cp %s %s/" % (fpath, temppath))
if "modname" in metafunc.funcargnames:
for modname in modnames:
metafunc.addcall(funcargs=dict(modname=modname, modpath=fpath))
else:
metafunc.addcall(funcargs=dict(modpath=fpath))
def test_compile(modname, modpath): def test_compile(modname, modpath):
tempname = "test_" + modpath.basename + modname tempname = "test_" + modpath.basename + modname + '.scad'
fpath = temppath.join(tempname) fpath = temppath.join(tempname)
stlpath = temppath.join(tempname + ".stl") stlpath = temppath.join(tempname + ".stl")
f = fpath.open('w') f = fpath.open('w')
@ -24,10 +25,10 @@ def test_compile(modname, modpath):
//generated testfile //generated testfile
include <%s> include <%s>
%s() %s();
""" % (modpath, modname)) """ % (modpath, modname))
f.flush f.flush
output = call_openscad(path=fpath, stlpath=stlpath) output = call_openscad(path=fpath, stlpath=stlpath, timeout=5)
print output print output
assert output[0] is 0 assert output[0] is 0
assert "warning" or "error" not in output[2].strip().lowercase() assert "warning" or "error" not in output[2].strip().lowercase()
@ -40,5 +41,6 @@ def test_compile_default(modpath):
print output print output
assert output[0] is 0 assert output[0] is 0
assert "warning" or "error" not in output[2].strip().lowercase() assert "warning" or "error" not in output[2].strip().lowercase()
assert len(stlpath.readlines()) == 2