[py-svn] r63550 - py/trunk/py/test/plugin

hpk at codespeak.net hpk at codespeak.net
Thu Apr 2 21:17:00 CEST 2009


Author: hpk
Date: Thu Apr  2 21:16:57 2009
New Revision: 63550

Added:
   py/trunk/py/test/plugin/api.py
      - copied, changed from r63547, py/trunk/py/test/plugin/pytest_plugintester.py
Modified:
   py/trunk/py/test/plugin/pytest_plugintester.py
   py/trunk/py/test/plugin/pytest_restdoc.py
Log:
* move hook and event API definitions to py/test/plugin/api.py 
* make "py.test" work on "doc" directory again. 



Copied: py/trunk/py/test/plugin/api.py (from r63547, py/trunk/py/test/plugin/pytest_plugintester.py)
==============================================================================
--- py/trunk/py/test/plugin/pytest_plugintester.py	(original)
+++ py/trunk/py/test/plugin/api.py	Thu Apr  2 21:16:57 2009
@@ -1,102 +1,11 @@
 """
-plugin with support classes and functions for testing pytest functionality 
+API definitions for pytest plugin hooks and events
 """
-import py
-
-class PlugintesterPlugin:
-    """ test support code for testing pytest plugins. """
-    def pytest_funcarg__plugintester(self, pyfuncitem):
-        pt = PluginTester(pyfuncitem) 
-        pyfuncitem.addfinalizer(pt.finalize)
-        return pt
-
-class Support(object):
-    def __init__(self, pyfuncitem):
-        """ instantiated per function that requests it. """
-        self.pyfuncitem = pyfuncitem
-
-    def getmoditem(self):
-        for colitem in self.pyfuncitem.listchain():
-            if isinstance(colitem, colitem.Module):
-                return colitem 
-
-    def finalize(self):
-        """ called after test function finished execution"""
-
-class PluginTester(Support):
-    def testdir(self):
-        # XXX import differently, eg. 
-        #     FSTester = self.pyfuncitem.config.pytestplugins.getpluginattr("pytester", "FSTester")
-        from pytest_pytester import TmpTestdir
-        crunner = TmpTestdir(self.pyfuncitem)
-        self.pyfuncitem.addfinalizer(crunner.finalize)
-        # 
-        for colitem in self.pyfuncitem.listchain():
-            if isinstance(colitem, py.test.collect.Module) and \
-               colitem.name.startswith("pytest_"):
-                    crunner.plugins.append(colitem.fspath.purebasename)
-                    break 
-        return crunner 
-
-    def apicheck(self, pluginclass):
-        print "loading and checking", pluginclass 
-        fail = False 
-        pm = py.test._PytestPlugins()
-        methods = collectattr(pluginclass)
-        hooks = collectattr(PytestPluginHooks)
-        getargs = py.std.inspect.getargs
-
-        def isgenerichook(name):
-            return name.startswith("pytest_funcarg__")
-
-        while methods:
-            name, method = methods.popitem()
-            if isgenerichook(name):
-                continue
-            if name not in hooks:
-                print "found unknown hook: %s" % name 
-                fail = True
-            else:
-                hook = hooks[name]
-                if not hasattr(hook, 'func_code'):
-                    continue # XXX do some checks on attributes as well? 
-                method_args = getargs(method.func_code) 
-                if '__call__' in method_args[0]:
-                    method_args[0].remove('__call__')
-                hookargs = getargs(hook.func_code)
-                for arg, hookarg in zip(method_args[0], hookargs[0]):
-                    if arg != hookarg: 
-                        print "argument mismatch:" 
-                        print "actual  : %s.%s" %(pluginclass.__name__, formatdef(method))
-                        print "required:", formatdef(hook)
-                        fail = True
-                        break 
-                if not fail:
-                    print "matching hook:", formatdef(method)
-        if fail:
-            py.test.fail("Plugin API error")
-
-def collectattr(obj, prefixes=("pytest_", "pyevent__")):
-    methods = {}
-    for apiname in vars(obj): 
-        for prefix in prefixes:
-            if apiname.startswith(prefix):
-                methods[apiname] = getattr(obj, apiname) 
-    return methods 
-
-def formatdef(func):
-    formatargspec = py.std.inspect.formatargspec
-    getargspec = py.std.inspect.formatargspec
-    return "%s%s" %(
-        func.func_name, 
-        py.std.inspect.formatargspec(*py.std.inspect.getargspec(func))
-    )
-
-
-class PytestPluginHooks:
-    def __init__(self):
-        """ usually called only once per test process. """ 
 
+class PluginHooks:
+    # ------------------------------------------------------------------------------
+    # Command line and configuration hooks 
+    # ------------------------------------------------------------------------------
     def pytest_addoption(self, parser):
         """ called before commandline parsing.  """
 
@@ -109,13 +18,10 @@
         """ called before test process is exited. 
         """
 
-    def pytest_pyfunc_call(self, pyfuncitem, args, kwargs):
-        """ return True if we consumed/did the call to the python function item. """
-
-    def pytest_item_makereport(self, item, excinfo, when, outerr):
-        """ return ItemTestReport event for the given test outcome. """
 
+    # ------------------------------------------------------------------------------
     # collection hooks
+    # ------------------------------------------------------------------------------
     def pytest_collect_file(self, path, parent):
         """ return Collection node or None. """
 
@@ -130,6 +36,16 @@
     def pytest_pymodule_makeitem(self, modcol, name, obj):
         """ return custom item/collector for a python object in a module, or None.  """
 
+    # ------------------------------------------------------------------------------
+    # runtest related hooks 
+    # ------------------------------------------------------------------------------
+
+    def pytest_pyfunc_call(self, pyfuncitem, args, kwargs):
+        """ return True if we consumed/did the call to the python function item. """
+
+    def pytest_item_makereport(self, item, excinfo, when, outerr):
+        """ return ItemTestReport event for the given test outcome. """
+
     # reporting hooks (invoked from pytest_terminal.py) 
     def pytest_report_teststatus(self, event):
         """ return shortletter and verbose word. """
@@ -137,9 +53,10 @@
     def pytest_terminal_summary(self, terminalreporter):
         """ add additional section in terminal summary reporting. """
 
+class Events:
     # Events 
     def pyevent(self, eventname, *args, **kwargs):
-        """ called for each testing event. """
+        """ generically called for each notification event. """
 
     def pyevent__gateway_init(self, gateway):
         """ called after a gateway has been initialized. """
@@ -186,13 +103,13 @@
             within the gateway manager context. 
         """
     def pyevent__testnodeready(self, node):
-        """ Node is ready to operate. """
+        """ Test Node is ready to operate. """
 
     def pyevent__testnodedown(self, node, error):
-        """ Node is down. """
+        """ Test Node is down. """
 
     def pyevent__rescheduleitems(self, event):
-        """ Items from a node that went down. """
+        """ reschedule Items from a node that went down. """
 
     def pyevent__looponfailinfo(self, event):
         """ info for repeating failing tests. """
@@ -201,9 +118,3 @@
         """ a new py lib plugin got registered. """
         
    
-# ===============================================================================
-# plugin tests 
-# ===============================================================================
-
-def test_generic(plugintester):
-    plugintester.apicheck(PlugintesterPlugin)

Modified: py/trunk/py/test/plugin/pytest_plugintester.py
==============================================================================
--- py/trunk/py/test/plugin/pytest_plugintester.py	(original)
+++ py/trunk/py/test/plugin/pytest_plugintester.py	Thu Apr  2 21:16:57 2009
@@ -2,6 +2,7 @@
 plugin with support classes and functions for testing pytest functionality 
 """
 import py
+from py.__.test.plugin import api
 
 class PlugintesterPlugin:
     """ test support code for testing pytest plugins. """
@@ -43,7 +44,8 @@
         fail = False 
         pm = py.test._PytestPlugins()
         methods = collectattr(pluginclass)
-        hooks = collectattr(PytestPluginHooks)
+        hooks = collectattr(api.PluginHooks)
+        hooks.update(collectattr(api.Events))
         getargs = py.std.inspect.getargs
 
         def isgenerichook(name):
@@ -92,115 +94,6 @@
         py.std.inspect.formatargspec(*py.std.inspect.getargspec(func))
     )
 
-
-class PytestPluginHooks:
-    def __init__(self):
-        """ usually called only once per test process. """ 
-
-    def pytest_addoption(self, parser):
-        """ called before commandline parsing.  """
-
-    def pytest_configure(self, config):
-        """ called after command line options have been parsed. 
-            and all plugins and initial conftest files been loaded. 
-            ``config`` provides access to all such configuration values. 
-        """
-    def pytest_unconfigure(self, config):
-        """ called before test process is exited. 
-        """
-
-    def pytest_pyfunc_call(self, pyfuncitem, args, kwargs):
-        """ return True if we consumed/did the call to the python function item. """
-
-    def pytest_item_makereport(self, item, excinfo, when, outerr):
-        """ return ItemTestReport event for the given test outcome. """
-
-    # collection hooks
-    def pytest_collect_file(self, path, parent):
-        """ return Collection node or None. """
-
-    def pytest_collect_recurse(self, path, parent):
-        """ return True/False to cause/prevent recursion into given directory. 
-            return None if you do not want to make the decision. 
-        """ 
-
-    def pytest_collect_directory(self, path, parent):
-        """ return Collection node or None. """
-
-    def pytest_pymodule_makeitem(self, modcol, name, obj):
-        """ return custom item/collector for a python object in a module, or None.  """
-
-    # reporting hooks (invoked from pytest_terminal.py) 
-    def pytest_report_teststatus(self, event):
-        """ return shortletter and verbose word. """
-
-    def pytest_terminal_summary(self, terminalreporter):
-        """ add additional section in terminal summary reporting. """
-
-    # Events 
-    def pyevent(self, eventname, *args, **kwargs):
-        """ called for each testing event. """
-
-    def pyevent__gateway_init(self, gateway):
-        """ called after a gateway has been initialized. """
-
-    def pyevent__gateway_exit(self, gateway):
-        """ called when gateway is being exited. """
-
-    def pyevent__gwmanage_rsyncstart(self, source, gateways):
-        """ called before rsyncing a directory to remote gateways takes place. """
-
-    def pyevent__gwmanage_rsyncfinish(self, source, gateways):
-        """ called after rsyncing a directory to remote gateways takes place. """
-
-    def pyevent__trace(self, category, msg):
-        """ called for tracing events. """
-
-    def pyevent__internalerror(self, event):
-        """ called for internal errors. """
-
-    def pyevent__itemstart(self, item, node):
-        """ test item gets collected. """
-
-    def pyevent__itemtestreport(self, event):
-        """ test has been run. """
-
-    def pyevent__deselected(self, event):
-        """ item has been dselected. """
-
-    def pyevent__collectionstart(self, event):
-        """ collector starts collecting. """
-
-    def pyevent__collectionreport(self, event):
-        """ collector finished collecting. """
-
-    def pyevent__testrunstart(self, event):
-        """ whole test run starts. """
-
-    def pyevent__testrunfinish(self, event):
-        """ whole test run finishes. """
-
-    def pyevent__gwmanage_newgateway(self, gateway):
-        """ execnet gateway manager has instantiated a gateway. 
-            The gateway will have an 'id' attribute that is unique 
-            within the gateway manager context. 
-        """
-    def pyevent__testnodeready(self, node):
-        """ Node is ready to operate. """
-
-    def pyevent__testnodedown(self, node, error):
-        """ Node is down. """
-
-    def pyevent__rescheduleitems(self, event):
-        """ Items from a node that went down. """
-
-    def pyevent__looponfailinfo(self, event):
-        """ info for repeating failing tests. """
-
-    def pyevent__plugin_registered(self, plugin):
-        """ a new py lib plugin got registered. """
-        
-   
 # ===============================================================================
 # plugin tests 
 # ===============================================================================

Modified: py/trunk/py/test/plugin/pytest_restdoc.py
==============================================================================
--- py/trunk/py/test/plugin/pytest_restdoc.py	(original)
+++ py/trunk/py/test/plugin/pytest_restdoc.py	Thu Apr  2 21:16:57 2009
@@ -85,6 +85,18 @@
         directive.register_linkrole('api', self.resolve_linkrole)
         directive.register_linkrole('source', self.resolve_linkrole)
 
+        # XXX fake sphinx' "toctree" and refs
+        directive.register_linkrole('ref', self.resolve_linkrole)
+        
+        from docutils.parsers.rst import directives
+        def toctree_directive(name, arguments, options, content, lineno,
+                      content_offset, block_text, state, state_machine):
+            return []
+        toctree_directive.content = 1
+        toctree_directive.options = {'maxdepth': int, 'glob': directives.flag,
+                             'hidden': directives.flag}
+        directives.register_directive('toctree', toctree_directive)
+
     def resolve_linkrole(self, name, text, check=True):
         apigen_relpath = self.project.apigen_relpath
     
@@ -125,6 +137,8 @@
             else:
                 relpath += '.html'
             return (text, apigen_relpath + 'source/%s' % (relpath,))
+        elif name == 'ref':
+            return ("", "") 
 
     def _checkskip(self, lpath, htmlpath=None):
         if not self.config.getvalue("forcegen"):



More information about the pytest-commit mailing list