a first go at testing schmir's generate_standalone_pytest script and

making it work on python2.4-3.1 + jython.

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-12-30 17:08:45 +01:00
parent f5ea19858c
commit 77667c11d2
4 changed files with 83 additions and 14 deletions

View File

@ -6,13 +6,10 @@ import zlib
import base64 import base64
import sys import sys
def main(): def main(pydir, outfile, infile):
here = os.path.dirname(os.path.abspath(__file__)) os.chdir(os.path.dirname(str(pydir)))
outfile = os.path.join(here, "py.test") outfile = str(outfile)
infile = outfile+"-in" infile = str(infile)
os.chdir(os.path.dirname(here))
files = [] files = []
for dirpath, dirnames, filenames in os.walk("py"): for dirpath, dirnames, filenames in os.walk("py"):
for f in filenames: for f in filenames:
@ -39,4 +36,8 @@ def main():
sys.stdout.write("generated %s\n" % outfile) sys.stdout.write("generated %s\n" % outfile)
if __name__=="__main__": if __name__=="__main__":
main() dn = os.path.dirname
pydir = os.path.join(dn(dn(os.path.abspath(__file__))), 'py')
outfile = os.path.join(dn(__file__), "py.test")
infile = outfile+"-in"
main(pydir, outfile, infile)

View File

@ -4,12 +4,19 @@ sources = """
@SOURCES@""" @SOURCES@"""
import sys import sys
import cPickle
import base64 import base64
import zlib import zlib
import imp import imp
sources = cPickle.loads(zlib.decompress(base64.decodestring(sources))) if sys.version_info >= (3,0):
exec("def do_exec(co, loc): exec(co, loc)\n")
import pickle
sources = sources.encode("ascii") # ensure bytes
else:
import cPickle as pickle
exec("def do_exec(co, loc): exec co in loc\n")
sources = pickle.loads(zlib.decompress(base64.decodestring(sources)))
class DictImporter(object): class DictImporter(object):
sources = sources sources = sources
@ -22,8 +29,7 @@ class DictImporter(object):
def load_module(self, fullname): def load_module(self, fullname):
# print "load_module:", fullname # print "load_module:", fullname
import new from types import ModuleType
try: try:
s = self.sources[fullname] s = self.sources[fullname]
is_pkg = False is_pkg = False
@ -32,13 +38,13 @@ class DictImporter(object):
is_pkg = True is_pkg = True
co = compile(s, fullname, 'exec') co = compile(s, fullname, 'exec')
module = sys.modules.setdefault(fullname, new.module(fullname)) module = sys.modules.setdefault(fullname, ModuleType(fullname))
module.__file__ = "%s/%s" % (__file__, fullname) module.__file__ = "%s/%s" % (__file__, fullname)
module.__loader__ = self module.__loader__ = self
if is_pkg: if is_pkg:
module.__path__ = [fullname] module.__path__ = [fullname]
exec co in module.__dict__ do_exec(co, module.__dict__)
return sys.modules[fullname] return sys.modules[fullname]
importer = DictImporter() importer = DictImporter()

View File

@ -0,0 +1,18 @@
import py, os
import generate_standalone_pytest
import subprocess
mydir = py.path.local(__file__).dirpath()
def test_gen(testdir, anypython):
testdir.chdir()
infile = mydir.join("py.test-in")
outfile = testdir.tmpdir.join("mypytest")
generate_standalone_pytest.main(pydir=os.path.dirname(py.__file__),
infile=infile, outfile=outfile)
result = testdir._run(anypython, outfile, '-h')
assert result.ret == 0
result = testdir._run(anypython, outfile, '--version')
assert result.ret == 0
result.stderr.fnmatch_lines([
"*imported from*mypytest"
])

View File

@ -63,3 +63,47 @@ def pytest_generate_tests(metafunc):
for name, l in multi.kwargs.items(): for name, l in multi.kwargs.items():
for val in l: for val in l:
metafunc.addcall(funcargs={name: val}) metafunc.addcall(funcargs={name: val})
# XXX copied from execnet's conftest.py - needs to be merged
winpymap = {
'python2.7': r'C:\Python27\python.exe',
'python2.6': r'C:\Python26\python.exe',
'python2.5': r'C:\Python25\python.exe',
'python2.4': r'C:\Python24\python.exe',
'python3.1': r'C:\Python31\python.exe',
}
def pytest_generate_tests(metafunc):
if 'anypython' in metafunc.funcargnames:
for name in ('python2.4', 'python2.5', 'python2.6',
'python2.7', 'python3.1', 'pypy-c', 'jython'):
metafunc.addcall(id=name, param=name)
def getexecutable(name, cache={}):
try:
return cache[name]
except KeyError:
executable = py.path.local.sysfind(name)
if executable:
if name == "jython":
import subprocess
popen = subprocess.Popen([str(executable), "--version"],
universal_newlines=True, stderr=subprocess.PIPE)
out, err = popen.communicate()
if not err or "2.5" not in err:
executable = None
cache[name] = executable
return executable
def pytest_funcarg__anypython(request):
name = request.param
executable = getexecutable(name)
if executable is None:
if sys.platform == "win32":
executable = winpymap.get(name, None)
if executable:
executable = py.path.local(executable)
if executable.check():
return executable
py.test.skip("no %s found" % (name,))
return executable