Author: Ronny Pfannschmidt <[email protected]>
Branch: pytest
Changeset: r60264:5852b6a995fa
Date: 2013-01-21 02:19 +0100
http://bitbucket.org/pypy/pypy/changeset/5852b6a995fa/

Log:    move some common code into the pytest plugins in rpython

diff --git a/pypy/conftest.py b/pypy/conftest.py
--- a/pypy/conftest.py
+++ b/pypy/conftest.py
@@ -8,34 +8,23 @@
 # PyPy's command line extra options (these are added
 # to py.test's standard options)
 #
+
+pytest_plugins = [
+    'rpython.tool.pytest.leakfinder',
+    'rpython.tool.pytest.viewerplugin',
+    'rpython.tool.pytest.platform',
+]
+
 option = None
 
 pypydir = os.path.realpath(os.path.dirname(__file__))
 
-def braindead_deindent(self):
-    """monkeypatch that wont end up doing stupid in the python tokenizer"""
-    text = '\n'.join(self.lines)
-    short = py.std.textwrap.dedent(text)
-    newsource = py.code.Source()
-    newsource.lines[:] = short.splitlines()
-    return newsource
-
-py.code.Source.deindent = braindead_deindent
-
-def pytest_report_header():
-    return "pytest-%s from %s" %(pytest.__version__, pytest.__file__)
-
-def pytest_addhooks(pluginmanager):
-    from rpython.conftest import LeakFinder
-    pluginmanager.register(LeakFinder())
 
 def pytest_configure(config):
     global option
     option = config.option
 
 def pytest_addoption(parser):
-    from rpython.conftest import pytest_addoption
-    pytest_addoption(parser)
     
     group = parser.getgroup("pypy options")
     group.addoption('-A', '--runappdirect', action="store_true",
diff --git a/rpython/conftest.py b/rpython/conftest.py
--- a/rpython/conftest.py
+++ b/rpython/conftest.py
@@ -1,43 +1,21 @@
 from os.path import *
-import py, pytest
-from rpython.tool import leakfinder
+import py
 
-pytest_plugins = 'rpython.tool.pytest.expecttest'
+pytest_plugins = [
+    'rpython.tool.pytest.expecttest',
+    'rpython.tool.pytest.leakfinder',
+    'rpython.tool.pytest.platform',
+    'rpython.tool.pytest.viewerplugin',
+]
 
 cdir = realpath(join(dirname(__file__), 'translator', 'c'))
 cache_dir = realpath(join(dirname(__file__), '_cache'))
 option = None
 
-def braindead_deindent(self):
-    """monkeypatch that wont end up doing stupid in the python tokenizer"""
-    text = '\n'.join(self.lines)
-    short = py.std.textwrap.dedent(text)
-    newsource = py.code.Source()
-    newsource.lines[:] = short.splitlines()
-    return newsource
-
-py.code.Source.deindent = braindead_deindent
-
-def pytest_report_header():
-    return "pytest-%s from %s" %(pytest.__version__, pytest.__file__)
-
 def pytest_configure(config):
     global option
     option = config.option
 
-def _set_platform(opt, opt_str, value, parser):
-    from rpython.config.translationoption import PLATFORMS
-    from rpython.translator.platform import set_platform
-    if value not in PLATFORMS:
-        raise ValueError("%s not in %s" % (value, PLATFORMS))
-    set_platform(value, None)
-
-def pytest_addoption(parser):
-    group = parser.getgroup("rpython options")
-    group.addoption('-P', '--platform', action="callback", type="string",
-           default="host", callback=_set_platform,
-           help="set up tests to use specified platform as compile/run target")
-
 
 def pytest_pycollect_makeitem(__multicall__,collector, name, obj):
     res = __multicall__.execute()
@@ -49,37 +27,3 @@
     return res
 
 
-def pytest_addhooks(pluginmanager):
-    pluginmanager.register(LeakFinder())
-
-class LeakFinder:
-    """Track memory allocations during test execution.
-    
-    So far, only used by the function lltype.malloc(flavor='raw').
-    """
-    def pytest_runtest_setup(self, __multicall__, item):
-        __multicall__.execute()
-        if not isinstance(item, py.test.collect.Function):
-            return
-        if not getattr(item.obj, 'dont_track_allocations', False):
-            leakfinder.start_tracking_allocations()
-
-    def pytest_runtest_call(self, __multicall__, item):
-        __multicall__.execute()
-        if not isinstance(item, py.test.collect.Function):
-            return
-        item._success = True
-
-    def pytest_runtest_teardown(self, __multicall__, item):
-        __multicall__.execute()
-        if not isinstance(item, py.test.collect.Function):
-            return
-        if (not getattr(item.obj, 'dont_track_allocations', False)
-            and leakfinder.TRACK_ALLOCATIONS):
-            item._pypytest_leaks = leakfinder.stop_tracking_allocations(False)
-        else:            # stop_tracking_allocations() already called
-            item._pypytest_leaks = None
-
-        # check for leaks, but only if the test passed so far
-        if getattr(item, '_success', False) and item._pypytest_leaks:
-            raise leakfinder.MallocMismatch(item._pypytest_leaks)
diff --git a/rpython/tool/pytest/leakfinder.py 
b/rpython/tool/pytest/leakfinder.py
new file mode 100644
--- /dev/null
+++ b/rpython/tool/pytest/leakfinder.py
@@ -0,0 +1,31 @@
+"""Track memory allocations during test execution.
+
+So far, only used by the function lltype.malloc(flavor='raw').
+"""
+
+def pytest_runtest_setup(__multicall__, item):
+    __multicall__.execute()
+    if not isinstance(item, py.test.collect.Function):
+        return
+    if not getattr(item.obj, 'dont_track_allocations', False):
+        leakfinder.start_tracking_allocations()
+
+def pytest_runtest_call(__multicall__, item):
+    __multicall__.execute()
+    if not isinstance(item, py.test.collect.Function):
+        return
+    item._success = True
+
+def pytest_runtest_teardown(__multicall__, item):
+    __multicall__.execute()
+    if not isinstance(item, py.test.collect.Function):
+        return
+    if (not getattr(item.obj, 'dont_track_allocations', False)
+        and leakfinder.TRACK_ALLOCATIONS):
+        item._pypytest_leaks = leakfinder.stop_tracking_allocations(False)
+    else:            # stop_tracking_allocations() already called
+        item._pypytest_leaks = None
+
+    # check for leaks, but only if the test passed so far
+    if getattr(item, '_success', False) and item._pypytest_leaks:
+        raise leakfinder.MallocMismatch(item._pypytest_leaks)
diff --git a/rpython/tool/pytest/platform.py b/rpython/tool/pytest/platform.py
new file mode 100644
--- /dev/null
+++ b/rpython/tool/pytest/platform.py
@@ -0,0 +1,30 @@
+import py
+import pytest
+
+def braindead_deindent(self):
+    """monkeypatch that wont end up doing stupid in the python tokenizer"""
+    text = '\n'.join(self.lines)
+    short = py.std.textwrap.dedent(text)
+    newsource = py.code.Source()
+    newsource.lines[:] = short.splitlines()
+    return newsource
+
+py.code.Source.deindent = braindead_deindent
+
+def pytest_report_header():
+    return "pytest-%s from %s" %(pytest.__version__, pytest.__file__)
+
+
+def _set_platform(opt, opt_str, value, parser):
+    from rpython.config.translationoption import PLATFORMS
+    from rpython.translator.platform import set_platform
+    if value not in PLATFORMS:
+        raise ValueError("%s not in %s" % (value, PLATFORMS))
+    set_platform(value, None)
+
+def pytest_addoption(parser):
+    group = parser.getgroup("rpython options")
+    group.addoption('-P', '--platform', action="callback", type="string",
+           default="host", callback=_set_platform,
+           help="set up tests to use specified platform as compile/run target")
+
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to