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 - 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 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) self.import_plugin(spec)
def register(self, plugin, name=None, prepend=False): 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))) name = name or getattr(plugin, '__name__', str(id(plugin)))
if name in self._name2plugin: if self.isregistered(plugin, name):
return False raise ValueError("Plugin already registered: %s=%s" %(name, plugin))
#self.trace("registering", name, plugin) #self.trace("registering", name, plugin)
self._name2plugin[name] = plugin self._name2plugin[name] = plugin
self.call_plugin(plugin, "pytest_addhooks", {'pluginmanager': self}) self.call_plugin(plugin, "pytest_addhooks", {'pluginmanager': self})

View File

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

View File

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