From eab7e039ebba408163448e31057e67fc84b2b505 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sat, 17 Oct 2009 12:56:59 +0200 Subject: [PATCH] streamline pluginmanager api and test/beautify printing of plugins with --trace --HG-- branch : trunk --- _py/test/plugin/pytest_pastebin.py | 4 +-- _py/test/plugin/pytest_pdb.py | 5 ++-- _py/test/plugin/pytest_pytester.py | 1 - _py/test/plugin/pytest_terminal.py | 15 ++++++----- _py/test/pluginmanager.py | 27 ++++++++++++------- testing/pytest/plugin/test_pytest_pastebin.py | 2 +- testing/pytest/plugin/test_pytest_pdb.py | 2 +- testing/pytest/plugin/test_pytest_terminal.py | 6 +++++ testing/pytest/test_pluginmanager.py | 12 ++++++++- 9 files changed, 48 insertions(+), 26 deletions(-) diff --git a/_py/test/plugin/pytest_pastebin.py b/_py/test/plugin/pytest_pastebin.py index 0fa5a573b..4d3f65359 100644 --- a/_py/test/plugin/pytest_pastebin.py +++ b/_py/test/plugin/pytest_pastebin.py @@ -38,7 +38,7 @@ def pytest_configure(__multicall__, config): __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(): diff --git a/_py/test/plugin/pytest_pdb.py b/_py/test/plugin/pytest_pdb.py index 301b56e93..7e6eccb42 100644 --- 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): diff --git a/_py/test/plugin/pytest_pytester.py b/_py/test/plugin/pytest_pytester.py index 5bb473209..99ac5377d 100644 --- 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): diff --git a/_py/test/plugin/pytest_terminal.py b/_py/test/plugin/pytest_terminal.py index e6775de1f..555385d7b 100644 --- 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)) diff --git a/_py/test/pluginmanager.py b/_py/test/pluginmanager.py index 8e122ca97..d322e506c 100644 --- 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) diff --git a/testing/pytest/plugin/test_pytest_pastebin.py b/testing/pytest/plugin/test_pytest_pastebin.py index 6e3ff4db9..032f683ca 100644 --- 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 diff --git a/testing/pytest/plugin/test_pytest_pdb.py b/testing/pytest/plugin/test_pytest_pdb.py index 638a98922..4dd57f015 100644 --- 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 diff --git a/testing/pytest/plugin/test_pytest_terminal.py b/testing/pytest/plugin/test_pytest_terminal.py index 747b3d0aa..5e54f5c4e 100644 --- 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 diff --git a/testing/pytest/test_pluginmanager.py b/testing/pytest/test_pluginmanager.py index 957a4e455..a7085bbe3 100644 --- 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: