From b2e87ce027bbf77f1b216241cf6e4261648c3fb7 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sat, 16 Jun 2012 21:29:04 +0200 Subject: [PATCH] change pluginmanager.register API to raise ValueError if the plugin object or the name is already registered --- CHANGELOG | 2 ++ _pytest/__init__.py | 2 +- _pytest/core.py | 7 ++++--- setup.py | 2 +- testing/test_core.py | 9 +++++++-- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 97cf30dd1..5103b5bf6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 ----------------------------------- diff --git a/_pytest/__init__.py b/_pytest/__init__.py index 8f181ddf4..283f55cfe 100644 --- a/_pytest/__init__.py +++ b/_pytest/__init__.py @@ -1,2 +1,2 @@ # -__version__ = '2.2.5.dev1' +__version__ = '2.2.5.dev2' diff --git a/_pytest/core.py b/_pytest/core.py index e609f9465..93bd553b2 100644 --- a/_pytest/core.py +++ b/_pytest/core.py @@ -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}) diff --git a/setup.py b/setup.py index 22dac2943..01be0ca47 100644 --- a/setup.py +++ b/setup.py @@ -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'], diff --git a/testing/test_core.py b/testing/test_core.py index 09497ef7c..405ef9811 100644 --- a/testing/test_core.py +++ b/testing/test_core.py @@ -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