Small improvements to the testing infra.

This commit is contained in:
Elmo 2010-08-26 16:05:04 +08:00 committed by Eero af Heurlin
parent f0960f9337
commit 30607913a3
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*")
func_re = (r"\bfunction\s+(", r")\s*\(")
def extract_mod_names(fpath, name_re=r"\w+"):
regex = name_re.join(mod_re)
def extract_definitions(fpath, name_re=r"\w+", def_re=""):
regex = name_re.join(def_re)
matcher = re.compile(regex)
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+"):
regex = name_re.join(func_re)
matcher = re.compile(regex)
return (m.group(1) for m in matcher.finditer(fpath.read()))
return extract_definitions(fpath, name_re=name_re, def_re=func_re)
def collect_test_modules():
dirpath = py.path.local("./")
def collect_test_modules(dirpath=None):
dirpath = dirpath or py.path.local("./")
print "Collecting openscad test module names"
test_files = {}
@ -26,21 +27,24 @@ def collect_test_modules():
test_files[fpath] = modules
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:
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)
calltime = time.time()
time.sleep(0.01)
#print calltime
while True:
if proc.poll() is not None:
break
time.sleep(0.1)
time.sleep(0.5)
#print time.time()
if time.time() > calltime + timeout:
raise Exception("Timeout")
raise Timeout()
finally:
try:
proc.terminate()

View file

@ -2,21 +2,22 @@ import py
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')
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):
tempname = "test_" + modpath.basename + modname
tempname = "test_" + modpath.basename + modname + '.scad'
fpath = temppath.join(tempname)
stlpath = temppath.join(tempname + ".stl")
f = fpath.open('w')
@ -24,10 +25,10 @@ def test_compile(modname, modpath):
//generated testfile
include <%s>
%s()
%s();
""" % (modpath, modname))
f.flush
output = call_openscad(path=fpath, stlpath=stlpath)
output = call_openscad(path=fpath, stlpath=stlpath, timeout=5)
print output
assert output[0] is 0
assert "warning" or "error" not in output[2].strip().lowercase()
@ -40,5 +41,6 @@ def test_compile_default(modpath):
print output
assert output[0] is 0
assert "warning" or "error" not in output[2].strip().lowercase()
assert len(stlpath.readlines()) == 2