From 30607913a3f51f9aec9a2c882d536513fa384209 Mon Sep 17 00:00:00 2001 From: Elmo Date: Thu, 26 Aug 2010 16:05:04 +0800 Subject: [PATCH] Small improvements to the testing infra. --- openscad_utils.py | 30 +++++++++++++++++------------- test_compile.py | 28 +++++++++++++++------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/openscad_utils.py b/openscad_utils.py index 51f383a..b7a48d5 100644 --- a/openscad_utils.py +++ b/openscad_utils.py @@ -4,20 +4,21 @@ 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 = {} for fpath in dirpath.visit('*.scad'): #print fpath @@ -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() diff --git a/test_compile.py b/test_compile.py index 2208186..a50e582 100644 --- a/test_compile.py +++ b/test_compile.py @@ -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