From 77667c11d27b7313a4f8d581739c9148bc61a96b Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 30 Dec 2009 17:08:45 +0100 Subject: [PATCH] a first go at testing schmir's generate_standalone_pytest script and making it work on python2.4-3.1 + jython. --HG-- branch : trunk --- bin-for-dist/generate_standalone_pytest.py | 17 +++++---- bin-for-dist/py.test-in | 18 ++++++--- bin-for-dist/test_generate_standalone.py | 18 +++++++++ conftest.py | 44 ++++++++++++++++++++++ 4 files changed, 83 insertions(+), 14 deletions(-) create mode 100644 bin-for-dist/test_generate_standalone.py diff --git a/bin-for-dist/generate_standalone_pytest.py b/bin-for-dist/generate_standalone_pytest.py index 32a48e9af..c46fa505b 100755 --- a/bin-for-dist/generate_standalone_pytest.py +++ b/bin-for-dist/generate_standalone_pytest.py @@ -6,13 +6,10 @@ import zlib import base64 import sys -def main(): - here = os.path.dirname(os.path.abspath(__file__)) - outfile = os.path.join(here, "py.test") - infile = outfile+"-in" - - os.chdir(os.path.dirname(here)) - +def main(pydir, outfile, infile): + os.chdir(os.path.dirname(str(pydir))) + outfile = str(outfile) + infile = str(infile) files = [] for dirpath, dirnames, filenames in os.walk("py"): for f in filenames: @@ -39,4 +36,8 @@ def main(): sys.stdout.write("generated %s\n" % outfile) 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) diff --git a/bin-for-dist/py.test-in b/bin-for-dist/py.test-in index 8388f7bd1..263b50f19 100755 --- a/bin-for-dist/py.test-in +++ b/bin-for-dist/py.test-in @@ -4,12 +4,19 @@ sources = """ @SOURCES@""" import sys -import cPickle import base64 import zlib 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): sources = sources @@ -22,8 +29,7 @@ class DictImporter(object): def load_module(self, fullname): # print "load_module:", fullname - import new - + from types import ModuleType try: s = self.sources[fullname] is_pkg = False @@ -32,13 +38,13 @@ class DictImporter(object): is_pkg = True 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.__loader__ = self if is_pkg: module.__path__ = [fullname] - exec co in module.__dict__ + do_exec(co, module.__dict__) return sys.modules[fullname] importer = DictImporter() diff --git a/bin-for-dist/test_generate_standalone.py b/bin-for-dist/test_generate_standalone.py new file mode 100644 index 000000000..c19fc021a --- /dev/null +++ b/bin-for-dist/test_generate_standalone.py @@ -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" + ]) diff --git a/conftest.py b/conftest.py index 9c6f1ea77..798512190 100644 --- a/conftest.py +++ b/conftest.py @@ -63,3 +63,47 @@ def pytest_generate_tests(metafunc): for name, l in multi.kwargs.items(): for val in l: 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