[pypy-svn] r44580 - in pypy/dist/pypy: . tool/test
fijal at codespeak.net
fijal at codespeak.net
Thu Jun 28 10:45:03 CEST 2007
Author: fijal
Date: Thu Jun 28 10:45:03 2007
New Revision: 44580
Modified:
pypy/dist/pypy/conftest.py
pypy/dist/pypy/tool/test/test_pytestsupport.py
Log:
The thing I always want to do - add a ExpectTest test case for running
inside pexpect. usefull for testing terminal stuff.
Modified: pypy/dist/pypy/conftest.py
==============================================================================
--- pypy/dist/pypy/conftest.py (original)
+++ pypy/dist/pypy/conftest.py Thu Jun 28 10:45:03 2007
@@ -1,10 +1,12 @@
-import py, sys
+import py, sys, os
from py.__.test.outcome import Failed
from pypy.interpreter.gateway import app2interp_temp
from pypy.interpreter.error import OperationError
from pypy.tool.pytest import appsupport
from pypy.tool.option import make_config, make_objspace
from inspect import isclass, getmro
+from pypy.tool.udir import udir
+from pypy.tool.autopath import pypydir
rootdir = py.magic.autopath().dirpath()
@@ -25,6 +27,9 @@
Option('-A', '--runappdirect', action="store_true",
default=False, dest="runappdirect",
help="run applevel tests directly on python interpreter (not through PyPy)"),
+ Option('--direct', action="store_true",
+ default=False, dest="rundirect",
+ help="run pexpect tests directly")
)
_SPACECACHE={}
@@ -183,6 +188,11 @@
return self.accept_regular_test()
if name.startswith('AppTest'):
return True
+ if name.startswith('ExpectTest'):
+ return True
+ #XXX todo
+ #if name.startswith('AppExpectTest'):
+ # return True
return False
def setup(self):
@@ -196,9 +206,19 @@
obj = getattr(self.obj, name)
if isclass(obj):
if name.startswith('AppTest'):
- return AppClassCollector(name, parent=self)
- else:
- return IntClassCollector(name, parent=self)
+ return AppClassCollector(name, parent=self)
+ elif name.startswith('ExpectTest'):
+ if option.rundirect:
+ return py.test.collect.Class(name, parent=self)
+ return ExpectClassCollector(name, parent=self)
+ # XXX todo
+ #elif name.startswith('AppExpectTest'):
+ # if option.rundirect:
+ # return AppClassCollector(name, parent=self)
+ # return AppExpectClassCollector(name, parent=self)
+ else:
+ return IntClassCollector(name, parent=self)
+
elif hasattr(obj, 'func_code'):
if name.startswith('app_test_'):
assert not obj.func_code.co_flags & 32, \
@@ -335,7 +355,7 @@
self.execute_appex(space, func, space, w_instance)
class PyPyClassCollector(py.test.collect.Class):
- def setup(self):
+ def setup(self):
cls = self.obj
cls.space = LazyObjSpaceGetter()
super(PyPyClassCollector, self).setup()
@@ -380,3 +400,59 @@
space.newtuple([]),
space.newdict())
self.w_class = w_class
+
+class ExpectTestMethod(py.test.collect.Function):
+ def safe_name(target):
+ s = "_".join(target)
+ s = s.replace("()", "paren")
+ s = s.replace(".py", "")
+ s = s.replace(".", "_")
+ return s
+
+ safe_name = staticmethod(safe_name)
+
+ def safe_filename(self):
+ name = self.safe_name(self.listnames())
+ num = 0
+ while udir.join(name + '.py').check():
+ num += 1
+ name = self.safe_name(self.listnames()) + "_" + str(num)
+ return name + '.py'
+
+ def _spawn(self, *args, **kwds):
+ import pexpect
+ child = pexpect.spawn(*args, **kwds)
+ child.logfile = sys.stdout
+ return child
+
+ def spawn(self, argv):
+ return self._spawn(sys.executable, argv)
+
+ def execute(self, target, *args):
+ assert not args
+ import pexpect
+ source = py.code.Source(target)[1:].deindent()
+ filename = self.safe_filename()
+ source.lines = ['import sys',
+ 'sys.path.insert(0, %s)' % repr(os.path.dirname(pypydir))
+ ] + source.lines
+ source.lines.append('print "%s ok!"' % filename)
+ f = udir.join(filename)
+ f.write(source)
+ # run target in the guarded environment
+ child = self.spawn([str(f)])
+ import re
+ child.expect(re.escape(filename + " ok!"))
+
+class ExpectClassInstance(py.test.collect.Instance):
+ Function = ExpectTestMethod
+
+class ExpectClassCollector(py.test.collect.Class):
+ Instance = ExpectClassInstance
+
+ def setup(self):
+ super(ExpectClassCollector, self).setup()
+ try:
+ import pexpect
+ except ImportError:
+ py.test.skip("pexpect not found")
Modified: pypy/dist/pypy/tool/test/test_pytestsupport.py
==============================================================================
--- pypy/dist/pypy/tool/test/test_pytestsupport.py (original)
+++ pypy/dist/pypy/tool/test/test_pytestsupport.py Thu Jun 28 10:45:03 2007
@@ -6,7 +6,10 @@
from pypy.interpreter.pycode import PyCode
from pypy.interpreter.pyframe import PyFrame
from pypy.tool.pytest.appsupport import AppFrame, build_pytest_assertion, AppExceptionInfo
-
+import py
+from pypy.tool.udir import udir
+import os
+import sys
def somefunc(x):
print x
@@ -99,3 +102,54 @@
def test_values_arrive2(self):
assert self.some1 == 42
+
+def test_expectcollect():
+ try:
+ import pexpect
+ except ImportError:
+ py.test.skip("pexpect not found")
+ source = py.code.Source("""
+ class ExpectTestOne:
+ def test_one(self):
+ pass
+ """)
+ from pypy import conftest
+ tdir = udir.ensure("t", dir=True)
+ dest = tdir.join("test_expect.py")
+ dest.write(source)
+ col = conftest.Module(dest)
+ result = col.run()
+ assert len(result) == 1
+
+def test_safename():
+ from pypy.conftest import ExpectTestMethod
+
+ safe_name = ExpectTestMethod.safe_name
+ assert safe_name(['pypy', 'tool', 'test', 'test_pytestsupport.py',
+ 'ExpectTest', '()', 'test_one']) == \
+ 'pypy_tool_test_test_pytestsupport_ExpectTest_paren_test_one'
+
+def test_safe_filename():
+ source = py.code.Source("""
+ class ExpectTestOne:
+ def test_one(self):
+ pass
+ """)
+ dest = udir.join("test_expect_safefilename.py")
+ dest.write(source)
+ from pypy import conftest
+ col = conftest.Module(dest)
+ methcol = col.join('ExpectTestOne').join('()').join('test_one')
+ name = 'test_expect_safefilename_ExpectTestOne_paren_test_one.py'
+ assert methcol.safe_filename() == name
+ udir.ensure(name)
+ assert methcol.safe_filename() == name[:-3] + '_1.py'
+
+class ExpectTest:
+ def test_one(self):
+ import os
+ import sys
+ assert os.ttyname(sys.stdin.fileno())
+
+ def test_two(self):
+ import pypy
More information about the Pypy-commit
mailing list