13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for the Registry classes"""
187
187
], sorted((key, a_registry.get_info(key))
188
188
for key in a_registry.keys()))
191
class TestRegistryWithDirs(TestCaseInTempDir):
190
def test_get_prefix(self):
191
my_registry = registry.Registry()
192
http_object = object()
193
sftp_object = object()
194
my_registry.register('http:', http_object)
195
my_registry.register('sftp:', sftp_object)
196
found_object, suffix = my_registry.get_prefix('http://foo/bar')
197
self.assertEqual('//foo/bar', suffix)
198
self.assertIs(http_object, found_object)
199
self.assertIsNot(sftp_object, found_object)
200
found_object, suffix = my_registry.get_prefix('sftp://baz/qux')
201
self.assertEqual('//baz/qux', suffix)
202
self.assertIs(sftp_object, found_object)
205
class TestRegistryIter(tests.TestCase):
206
"""Test registry iteration behaviors.
208
There are dark corner cases here when the registered objects trigger
209
addition in the iterated registry.
213
super(TestRegistryIter, self).setUp()
215
# We create a registry with "official" objects and "hidden"
216
# objects. The later represent the side effects that led to bug #277048
218
self.registry = registry.Registry()
221
self.registry.register('hidden', None)
223
self.registry.register('passive', None)
224
self.registry.register('active', register_more)
225
self.registry.register('passive-too', None)
227
class InvasiveGetter(registry._ObjectGetter):
229
def get_obj(inner_self):
230
# Surprise ! Getting a registered object (think lazy loaded
231
# module) register yet another object !
232
self.registry.register('more hidden', None)
233
return inner_self._obj
235
self.registry.register('hacky', None)
236
# We peek under the covers because the alternative is to use lazy
237
# registration and create a module that can reference our test registry
238
# it's too much work for such a corner case -- vila 090916
239
self.registry._dict['hacky'] = InvasiveGetter(None)
241
def _iter_them(self, iter_func_name):
242
iter_func = getattr(self.registry, iter_func_name, None)
243
self.assertIsNot(None, iter_func)
245
for name, func in iter_func():
247
self.assertFalse(name in ('hidden', 'more hidden'))
249
# Using an object register another one as a side effect
251
self.assertEqual(4, count)
253
def test_iteritems(self):
254
# the dict is modified during the iteration
255
self.assertRaises(RuntimeError, self._iter_them, 'iteritems')
257
def test_items(self):
258
# we should be able to iterate even if one item modify the dict
259
self._iter_them('items')
262
class TestRegistryWithDirs(tests.TestCaseInTempDir):
192
263
"""Registry tests that require temporary dirs"""
194
265
def create_plugin_file(self, contents):