Author: Maciej Fijalkowski <[email protected]>
Branch: split-rpython
Changeset: r60224:02d7d2319c9e
Date: 2013-01-20 11:18 +0200
http://bitbucket.org/pypy/pypy/changeset/02d7d2319c9e/
Log: Another gross hack bites the dust.
diff --git a/pypy/conftest.py b/pypy/conftest.py
--- a/pypy/conftest.py
+++ b/pypy/conftest.py
@@ -101,11 +101,6 @@
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 makeitem(self, name, obj):
diff --git a/rpython/rtyper/module/test/test_ll_os.py
b/rpython/rtyper/module/test/test_ll_os.py
--- a/rpython/rtyper/module/test/test_ll_os.py
+++ b/rpython/rtyper/module/test/test_ll_os.py
@@ -1,9 +1,10 @@
import os
-from py.path import local
from rpython.tool.udir import udir
from rpython.translator.c.test.test_genc import compile
-from rpython.rtyper.module import ll_os #has side effect of registering
functions
+from rpython.rtyper.module import ll_os
+#has side effect of registering functions
+from rpython.tool.pytest.expecttest import ExpectTest
from rpython.rtyper import extregistry
import errno
@@ -11,7 +12,6 @@
import py
def getllimpl(fn):
- from rpython.rtyper.module import ll_os
return extregistry.lookup(fn).lltypeimpl
def test_access():
@@ -276,25 +276,27 @@
assert f(-1) == False
-class ExpectTestOs:
+class TestOsExpect(ExpectTest):
def setup_class(cls):
if not hasattr(os, 'ttyname'):
py.test.skip("no ttyname")
def test_ttyname(self):
- import os
- import py
- from rpython.rtyper.test.test_llinterp import interpret
+ def f():
+ import os
+ import py
+ from rpython.rtyper.test.test_llinterp import interpret
- def ll_to_string(s):
- return ''.join(s.chars)
-
- def f(num):
- try:
- return os.ttyname(num)
- except OSError:
- return ''
+ def ll_to_string(s):
+ return ''.join(s.chars)
- assert ll_to_string(interpret(f, [0])) == f(0)
- assert ll_to_string(interpret(f, [338])) == ''
+ def f(num):
+ try:
+ return os.ttyname(num)
+ except OSError:
+ return ''
+ assert ll_to_string(interpret(f, [0])) == f(0)
+ assert ll_to_string(interpret(f, [338])) == ''
+
+ self.run_test(f)
diff --git a/rpython/rtyper/module/test/test_ll_termios.py
b/rpython/rtyper/module/test/test_ll_termios.py
--- a/rpython/rtyper/module/test/test_ll_termios.py
+++ b/rpython/rtyper/module/test/test_ll_termios.py
@@ -1,5 +1,6 @@
import py, re, sys
from rpython.tool.udir import udir
+from rpython.tool.pytest.expecttest import ExpectTest
# tests here are run as snippets through a pexpected python subprocess
def setup_module(mod):
@@ -88,15 +89,17 @@
fn = compile(runs_tcall, [], backendopt=False)
self.run(fn, "ok")
-class ExpectTestTermios(object):
+class TestTermios(ExpectTest):
def test_tcsetattr_icanon(self):
- from rpython.rlib import rtermios
- import termios
- def check(fd, when, attributes):
- count = len([i for i in attributes[-1] if isinstance(i, int)])
- assert count == 2
- termios.tcsetattr = check
- attr = list(rtermios.tcgetattr(2))
- attr[3] |= termios.ICANON
- rtermios.tcsetattr(2, termios.TCSANOW, attr)
+ def f():
+ from rpython.rlib import rtermios
+ import termios
+ def check(fd, when, attributes):
+ count = len([i for i in attributes[-1] if isinstance(i, int)])
+ assert count == 2
+ termios.tcsetattr = check
+ attr = list(rtermios.tcgetattr(2))
+ attr[3] |= termios.ICANON
+ rtermios.tcsetattr(2, termios.TCSANOW, attr)
+ self.run_test(f)
diff --git a/rpython/tool/pytest/expecttest.py
b/rpython/tool/pytest/expecttest.py
--- a/rpython/tool/pytest/expecttest.py
+++ b/rpython/tool/pytest/expecttest.py
@@ -1,36 +1,11 @@
-# Collects and executes "Expect" tests.
-#
-# Classes which names start with "ExpectTest", are started in a
-# separate process, and monitored by the pexpect module. This allows
-# execution of dangerous code, which messes with the terminal for
-# example.
+# Executes "Expect" tests.
import py
-import os
import sys
-from rpython.tool.udir import udir
-from pypy.conftest import pypydir #XXX
+from tempfile import NamedTemporaryFile
-
-class ExpectTestMethod(py.test.collect.Function):
- @staticmethod
- def safe_name(target):
- s = "_".join(target)
- s = s.replace("()", "paren")
- s = s.replace(".py", "")
- s = s.replace(".", "_")
- s = s.replace(os.sep, "_")
- return s
-
- 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'
-
+class ExpectTest(object):
def _spawn(self, *args, **kwds):
import pexpect
kwds.setdefault('timeout', 600)
@@ -41,41 +16,22 @@
def spawn(self, argv):
return self._spawn(sys.executable, argv)
- def runtest(self):
- target = self.obj
- import pexpect
- source = py.code.Source(target)[1:].deindent()
- filename = self.safe_filename()
+ def run_test(self, func):
+ try:
+ import pexpect
+ except ImportError:
+ py.test.skip("pexpect not found")
+ source = py.code.Source(func)[1:].deindent()
+ tmpfile = NamedTemporaryFile(suffix='.py')
+ fname = tmpfile.name
+ dir_to_insert = py.path.local(__file__).join('..', '..', '..', '..')
source.lines = ['import sys',
- 'sys.path.insert(0, %s)' % repr(os.path.dirname(pypydir))
+ 'sys.path.insert(0, %s)' % repr(str(dir_to_insert))
] + source.lines
- source.lines.append('print "%s ok!"' % filename)
- f = udir.join(filename)
+ source.lines.append('print "%s ok!"' % fname)
+ f = py.path.local(fname)
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")
-
-
[email protected]
-def pytest_pycollect_makeitem(collector, name, obj):
- if py.std.inspect.isclass(obj) and name.startswith('ExpectTest'):
- #XXX: in conftest we had a rundirect option
- #XXX: kill expecttest for a more explicit way
- return ExpectClassCollector(name, parent=collector)
+ child.expect(re.escape(fname + " ok!"))
diff --git a/rpython/tool/pytest/test/test_expecttest.py
b/rpython/tool/pytest/test/test_expecttest.py
--- a/rpython/tool/pytest/test/test_expecttest.py
+++ b/rpython/tool/pytest/test/test_expecttest.py
@@ -1,53 +1,9 @@
-import py
-import rpython
+from rpython.tool.pytest.expecttest import ExpectTest
-pytest_plugins = "pytest_pytester"
-
-conftestpath = py.path.local(rpython.__file__).dirpath("conftest.py")
-
-
-def test_expectcollect(testdir):
- py.test.importorskip("pexpect")
- conftestpath.copy(testdir.tmpdir)
- sorter = testdir.inline_runsource("""
- class ExpectTestOne:
- def test_one(self):
- pass
- """)
- passed, skipped, failed = sorter.countoutcomes()
- assert passed == 1
-
-
-def test_safename():
- from rpython.tool.pytest.expecttest 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(testdir):
- py.test.importorskip("pexpect")
- conftestpath.copy(testdir.tmpdir)
- sorter = testdir.inline_runsource("""
- class ExpectTestOne:
- def test_one(self):
- pass
- """)
- evlist = sorter.getcalls("pytest_runtest_makereport")
- ev = [x for x in evlist if x.call.when == "call"][0]
- print ev
- sfn = ev.item.safe_filename()
- print sfn
- assert sfn ==
'test_safe_filename_test_safe_filename_ExpectTestOne_paren_test_one_1.py'
-
-
-class ExpectTest:
+class TestExpect(ExpectTest):
def test_one(self):
- import os
- import sys
- assert os.ttyname(sys.stdin.fileno())
-
- def test_two(self):
- import pypy
+ def func():
+ import os
+ import sys
+ assert os.ttyname(sys.stdin.fileno())
+ self.run_test(func)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit