[py-svn] py-trunk commit 78bfab622759: streamline pluginmanager api and test/beautify printing of plugins with --trace

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Sat Oct 17 17:48:43 CEST 2009


# HG changeset patch -- Bitbucket.org
# Project py-trunk
# URL http://bitbucket.org/hpk42/py-trunk/overview/
# User holger krekel <holger at merlinux.eu>
# Date 1255777019 -7200
# Node ID 78bfab622759667c10a1ac7f1f158104ede57da7
# Parent 647c31b19f6f38f577552aac66276915511af192
streamline pluginmanager api and test/beautify printing of plugins with --trace

--- a/testing/pytest/plugin/test_pytest_terminal.py
+++ b/testing/pytest/plugin/test_pytest_terminal.py
@@ -613,3 +613,9 @@ class TestTerminalFunctional:
             ])
             assert result.ret == 1
 
+    def test_trace_reporting(self, testdir):
+        result = testdir.runpytest("--trace")
+        assert result.stdout.fnmatch_lines([
+            "*active plugins*"
+        ])
+        assert result.ret == 0

--- a/testing/pytest/plugin/test_pytest_pdb.py
+++ b/testing/pytest/plugin/test_pytest_pdb.py
@@ -6,7 +6,7 @@ class TestPDB:
         pdblist = []
         def mypdb(*args):
             pdblist.append(args)
-        plugin = request.config.pluginmanager.impname2plugin['pytest_pdb']
+        plugin = request.config.pluginmanager.getplugin('pdb')
         monkeypatch.setattr(plugin, 'post_mortem', mypdb)
         return pdblist 
          

--- a/testing/pytest/test_pluginmanager.py
+++ b/testing/pytest/test_pluginmanager.py
@@ -121,9 +121,10 @@ class TestBootstrapping:
         a1, a2 = A(), A()
         pp.register(a1)
         assert pp.isregistered(a1)
-        pp.register(a2)
+        pp.register(a2, "hello")
         assert pp.isregistered(a2)
         assert pp.getplugins() == [a1, a2]
+        assert pp.getplugin('hello') == a2
         pp.unregister(a1)
         assert not pp.isregistered(a1)
         pp.unregister(a2)
@@ -142,6 +143,15 @@ class TestBootstrapping:
         #assert not pp.isregistered(mod2)
         assert pp.getplugins() == [mod] # does not actually modify plugins 
 
+    def test_canonical_import(self, monkeypatch):
+        mod = py.std.types.ModuleType("pytest_xyz")
+        monkeypatch.setitem(py.std.sys.modules, 'pytest_xyz', mod)
+        pp = PluginManager()
+        pp.import_plugin('xyz')
+        assert pp.getplugin('xyz') == mod
+        assert pp.getplugin('pytest_xyz') == mod
+        assert pp.isregistered(mod)
+
     def test_register_mismatch_method(self):
         pp = PluginManager()
         class hello:

--- a/_py/test/plugin/pytest_pastebin.py
+++ b/_py/test/plugin/pytest_pastebin.py
@@ -38,7 +38,7 @@ def pytest_configure(__multicall__, conf
     __multicall__.execute()
     if config.option.pastebin == "all":
         config._pastebinfile = tempfile.TemporaryFile('w+')
-        tr = config.pluginmanager.impname2plugin['terminalreporter']
+        tr = config.pluginmanager.getplugin('terminalreporter')
         oldwrite = tr._tw.write 
         def tee_write(s, **kwargs):
             oldwrite(s, **kwargs)
@@ -54,7 +54,7 @@ def pytest_unconfigure(config):
         proxyid = getproxy().newPaste("python", sessionlog)
         pastebinurl = "%s%s" % (url.show, proxyid)
         sys.stderr.write("pastebin session-log: %s\n" % pastebinurl)
-        tr = config.pluginmanager.impname2plugin['terminalreporter']
+        tr = config.pluginmanager.getplugin('terminalreporter')
         del tr._tw.__dict__['write']
         
 def getproxy():

--- a/_py/test/plugin/pytest_terminal.py
+++ b/_py/test/plugin/pytest_terminal.py
@@ -236,14 +236,15 @@ class TerminalReporter:
         if self.config.option.debug or self.config.option.traceconfig:
             self.write_line("using py lib: %s" % (py.path.local(py.__file__).dirpath()))
         if self.config.option.traceconfig:
+            self.write_line("active plugins:")
             plugins = []
-            for plugin in self.config.pluginmanager.comregistry:
-                name = getattr(plugin, '__name__', None)
-                if name is None:
-                    name = plugin.__class__.__name__
-                plugins.append(name)
-            plugins = ", ".join(plugins) 
-            self.write_line("active plugins: %s" %(plugins,))
+            items = self.config.pluginmanager._name2plugin.items()
+            for name, plugin in items:
+                repr_plugin = repr(plugin)
+                fullwidth = getattr(self._tw, 'fullwidth', sys.maxint)
+                if len(repr_plugin)+26 > fullwidth:
+                    repr_plugin = repr_plugin[:(fullwidth-30)] + '...'
+                self.write_line("    %-20s: %s" %(name, repr_plugin))
         for i, testarg in enumerate(self.config.args):
             self.write_line("test object %d: %s" %(i+1, testarg))
 

--- a/_py/test/plugin/pytest_pdb.py
+++ b/_py/test/plugin/pytest_pdb.py
@@ -28,8 +28,8 @@ def pytest_configure(config):
 class PdbInvoke:
     def pytest_runtest_makereport(self, item, call):
         if call.excinfo and not call.excinfo.errisinstance(Skipped): 
-            # XXX hack hack hack to play well with capturing
-            capman = item.config.pluginmanager.impname2plugin['capturemanager']
+            # play well with capturing, slightly hackish
+            capman = item.config.pluginmanager.getplugin('capturemanager')
             capman.suspendcapture() 
 
             tw = py.io.TerminalWriter()
@@ -37,7 +37,6 @@ class PdbInvoke:
             repr.toterminal(tw) 
             post_mortem(call.excinfo._excinfo[2])
 
-            # XXX hack end 
             capman.resumecapture_item(item)
 
 class Pdb(py.std.pdb.Pdb):

--- a/_py/test/plugin/pytest_pytester.py
+++ b/_py/test/plugin/pytest_pytester.py
@@ -189,7 +189,6 @@ class TmpTestdir:
                     plugin = PseudoPlugin(plugin) 
                 if not config.pluginmanager.isregistered(plugin):
                     config.pluginmanager.register(plugin)
-        #print "config.pluginmanager.impname2plugin", config.pluginmanager.impname2plugin
         return config
 
     def parseconfig(self, *args):

--- a/testing/pytest/plugin/test_pytest_pastebin.py
+++ b/testing/pytest/plugin/test_pytest_pastebin.py
@@ -6,7 +6,7 @@ class TestPasting:
         class MockProxy:
             def newPaste(self, language, code):
                 pastebinlist.append((language, code))
-        plugin = request.config.pluginmanager.impname2plugin['pytest_pastebin']
+        plugin = request.config.pluginmanager.getplugin('pastebin')
         mp.setattr(plugin, 'getproxy', MockProxy) 
         return pastebinlist 
 

--- a/_py/test/pluginmanager.py
+++ b/_py/test/pluginmanager.py
@@ -16,7 +16,7 @@ class PluginManager(object):
         if comregistry is None: 
             comregistry = py._com.Registry()
         self.comregistry = comregistry 
-        self.impname2plugin = {}
+        self._name2plugin = {}
 
         self.hook = py._com.HookRelay(
             hookspecs=hookspec, 
@@ -33,9 +33,9 @@ class PluginManager(object):
     def register(self, plugin, name=None):
         assert not self.isregistered(plugin)
         name = self._getpluginname(plugin, name)
-        if name in self.impname2plugin:
+        if name in self._name2plugin:
             return False
-        self.impname2plugin[name] = plugin
+        self._name2plugin[name] = plugin
         self.hook.pytest_plugin_registered(plugin=plugin)
         self._checkplugin(plugin)
         self.comregistry.register(plugin)
@@ -44,19 +44,26 @@ class PluginManager(object):
     def unregister(self, plugin):
         self.hook.pytest_plugin_unregistered(plugin=plugin)
         self.comregistry.unregister(plugin)
-        for name, value in list(self.impname2plugin.items()):
+        for name, value in list(self._name2plugin.items()):
             if value == plugin:
-                del self.impname2plugin[name]
+                del self._name2plugin[name]
 
     def isregistered(self, plugin, name=None):
-        return self._getpluginname(plugin, name) in self.impname2plugin
+        if self._getpluginname(plugin, name) in self._name2plugin:
+            return True
+        for val in self._name2plugin.values():
+            if plugin == val:
+                return True
 
     def getplugins(self):
         return list(self.comregistry)
 
-    def getplugin(self, importname):
-        impname = canonical_importname(importname)
-        return self.impname2plugin[impname]
+    def getplugin(self, name):
+        try:
+            return self._name2plugin[name]
+        except KeyError:
+            impname = canonical_importname(name)
+            return self._name2plugin[impname]
 
     # API for bootstrapping 
     #
@@ -94,7 +101,7 @@ class PluginManager(object):
     def import_plugin(self, spec):
         assert isinstance(spec, str)
         modname = canonical_importname(spec)
-        if modname in self.impname2plugin:
+        if modname in self._name2plugin:
             return
         try:
             mod = importplugin(modname)



More information about the pytest-commit mailing list