change pluginmanager.register API to raise ValueError if the plugin object or the name is already registered

This commit is contained in:
holger krekel 2012-06-16 21:29:04 +02:00
parent 2e163e4aae
commit b2e87ce027
5 changed files with 15 additions and 7 deletions

View File

@ -2,6 +2,8 @@ Changes between 2.2.4 and 2.2.5.dev
-----------------------------------
- fix issue128: show captured output when capsys/capfd are used
- pluginmanager.register(...) now raises ValueError if the
plugin has been already registered or the name is taken
Changes between 2.2.3 and 2.2.4
-----------------------------------

View File

@ -1,2 +1,2 @@
#
__version__ = '2.2.5.dev1'
__version__ = '2.2.5.dev2'

View File

@ -79,10 +79,11 @@ class PluginManager(object):
self.import_plugin(spec)
def register(self, plugin, name=None, prepend=False):
assert not self.isregistered(plugin), plugin
if self._name2plugin.get(name, None) == -1:
return
name = name or getattr(plugin, '__name__', str(id(plugin)))
if name in self._name2plugin:
return False
if self.isregistered(plugin, name):
raise ValueError("Plugin already registered: %s=%s" %(name, plugin))
#self.trace("registering", name, plugin)
self._name2plugin[name] = plugin
self.call_plugin(plugin, "pytest_addhooks", {'pluginmanager': self})

View File

@ -24,7 +24,7 @@ def main():
name='pytest',
description='py.test: simple powerful testing with Python',
long_description = long_description,
version='2.2.5.dev1',
version='2.2.5.dev2',
url='http://pytest.org',
license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

View File

@ -32,6 +32,11 @@ class TestBootstrapping:
l2 = pluginmanager.getplugins()
assert 42 not in l2
def test_plugin_double_register(self):
pm = PluginManager()
pm.register(42, name="abc")
pytest.raises(ValueError, lambda: pm.register(42, name="abc"))
def test_plugin_skip(self, testdir, monkeypatch):
p = testdir.makepyfile(skipping1="""
import pytest
@ -203,10 +208,10 @@ class TestBootstrapping:
assert pp.isregistered(mod)
l = pp.getplugins()
assert mod in l
pytest.raises(AssertionError, "pp.register(mod)")
pytest.raises(ValueError, "pp.register(mod)")
mod2 = py.std.types.ModuleType("pytest_hello")
#pp.register(mod2) # double pm
pytest.raises(AssertionError, "pp.register(mod)")
pytest.raises(ValueError, "pp.register(mod)")
#assert not pp.isregistered(mod2)
assert pp.getplugins() == l