[py-svn] r61154 - in py/branch/pytestplugin/py/test: . testing

hpk at codespeak.net hpk at codespeak.net
Tue Jan 20 12:34:44 CET 2009


Author: hpk
Date: Tue Jan 20 12:34:42 2009
New Revision: 61154

Added:
   py/branch/pytestplugin/py/test/pmanage.py   (contents, props changed)
   py/branch/pytestplugin/py/test/testing/test_pmanage.py   (contents, props changed)
Log:
adding draft pluginmanager


Added: py/branch/pytestplugin/py/test/pmanage.py
==============================================================================
--- (empty file)
+++ py/branch/pytestplugin/py/test/pmanage.py	Tue Jan 20 12:34:42 2009
@@ -0,0 +1,53 @@
+"""
+manage bootstrap, lifecycle and interaction with plugins 
+"""
+import py
+
+class PluginManager(object):
+    def __init__(self):
+        self._plugins = {}
+
+    def load(self, plugin):
+        for attrname in 'name', 'version':
+            assert hasattr(plugin, attrname)
+        name, version = plugin.name, plugin.version
+        if name in self._plugins:
+            raise ValueError("plugin with name %r aready loaded" % name)
+        self.trace("adding plugin %s-%s:" % (name, version))
+        self._plugins[name] = plugin
+
+    def _tryimport(self, modname):
+        try:
+            return __import__(modname, None, None, "__doc__")
+        except ImportError:
+            pass
+
+    def trace(self, msg): 
+        print >>py.std.sys.stderr, msg 
+
+    def loadbyname(self, pname):
+        pluginmod = self._tryimport(pname) 
+        if pluginmod is None:
+            self.trace("could not import plugin: %s" %(pname))
+        else:
+            for name, cls in vars(pluginmod).items():
+                if name.endswith("Plugin"):
+                    if getattr(cls, '__module__', None) == pluginmod.__name__:
+                        plugininstance = cls()
+                        self.load(plugininstance) 
+                        return plugininstance
+
+    def getplugin(self, pname):
+        return self._plugins[pname] 
+
+    #
+    # API for calling methods of the plugins 
+    #
+    def call_plugins(self, methname, **args):
+        for plugin in self._plugins.values():
+            method = getattr(plugin, methname, None)
+            if method is not None:
+                method(**args)
+
+    def forward_event(self, event):
+        self.call_plugins('pytest_event', event=event)

Added: py/branch/pytestplugin/py/test/testing/test_pmanage.py
==============================================================================
--- (empty file)
+++ py/branch/pytestplugin/py/test/testing/test_pmanage.py	Tue Jan 20 12:34:42 2009
@@ -0,0 +1,51 @@
+import sys
+import py
+from py.__.test.pmanage import PluginManager 
+
+class TestPluginManager:
+    def setup_method(self, method):
+        self.tmpdir = py.test.ensuretemp("%s.%s.%s" %
+            (__name__, self.__class__.__name__, method.__name__))
+
+    def test_loadbyname(self):
+        pm = PluginManager()
+        assert pm.loadbyname("x.y.z.a.b.c") is None
+
+        sys.path.insert(0, str(self.tmpdir))
+        pluginname = "testinitplugin"
+        self.tmpdir.join(pluginname + ".py").write(py.code.Source("""
+            class MyPlugin:
+                name = "myplugin" 
+                version = "0.1" 
+        """))
+        plugininstance = pm.loadbyname(pluginname) 
+        assert plugininstance.version == "0.1" 
+        assert plugininstance.name == "myplugin" 
+        py.test.raises(ValueError, "pm.loadbyname(pluginname)")
+
+    def test_callplugins(self):
+        pm = PluginManager()
+        class MyPlugin:
+            name, version = "myplugin 0.1".split()
+            def method(self, arg):
+                pass
+        pm.load(MyPlugin())
+        py.test.raises(ValueError, "pm.load(MyPlugin())")
+        py.test.raises(TypeError, 'pm.call_plugins("method")')
+        py.test.raises(TypeError, 'pm.call_plugins("method", 42)')
+        pm.call_plugins("method", arg=42)
+        py.test.raises(TypeError, 'pm.call_plugins("method", arg=42, s=13)')
+
+    def test_getplugin(self):
+        pm = PluginManager()
+        assert py.test.raises(LookupError, "pm.getplugin('xxx')")
+
+        class MyPlugin:
+            name, version = "myplugin 0.1".split()
+            def method(self, arg):
+                pass
+        pm.load(MyPlugin())
+        myplugin1 = pm.getplugin("myplugin") 
+        myplugin2 = pm.getplugin("myplugin") 
+        assert myplugin1 is myplugin2 
+



More information about the pytest-commit mailing list