Merge lp:~free.ekanayaka/python-zope-fixtures/restore-former-hooks into lp:python-zope-fixtures

Proposed by Free Ekanayaka
Status: Merged
Merged at revision: 5
Proposed branch: lp:~free.ekanayaka/python-zope-fixtures/restore-former-hooks
Merge into: lp:python-zope-fixtures
Diff against target: 79 lines (+46/-2)
2 files modified
lib/zope_fixtures/components.py (+24/-2)
lib/zope_fixtures/tests/test_components.py (+22/-0)
To merge this branch: bzr merge lp:~free.ekanayaka/python-zope-fixtures/restore-former-hooks
Reviewer Review Type Date Requested Status
Robert Collins Approve
Review via email: mp+87694@code.launchpad.net

Description of the change

This branch makes ComponentsFixture keep track of existing getSiteManager hooks and restore them upon cleanup. It also adds support for specifying bases different than the default global site manager.

To post a comment you must log in.
Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

Hey Robert, would you have a look at this when you have some time?

Revision history for this message
Robert Collins (lifeless) wrote :

Doh! yes, I shall do so - I had budapest, then ubu-flu, then - well
stuff. We should get more reviewers and less bus factor I think.
Anyhow, for this, I 'll get it landed ~monday.

-Rob

Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

Thanks a lot Robert! I know you're busy, just wanted to make sure it's in your radar.

Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

Hi Robert, monthly ping :)

Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

Hey there Robert, monthly ping 2 :D

Revision history for this message
Robert Collins (lifeless) wrote :

This can be a bit smaller if you use cleanups idiomatically - I've changed it thusly.

review: Approve
Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

Thanks for reviewing and polishing!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/zope_fixtures/components.py'
2--- lib/zope_fixtures/components.py 2011-11-28 08:21:02 +0000
3+++ lib/zope_fixtures/components.py 2012-01-05 22:25:28 +0000
4@@ -42,12 +42,34 @@
5 getSiteManager().registerUtility(...)
6 # more code here
7 """
8+ _old_implementation = None
9+
10+ def __init__(self, bases=None):
11+ """Initialize the fixture.
12+
13+ :param bases: Optionally, the registries that should be used as bases
14+ for the fixture's registry. The default is to base on the current
15+ global site manager, as returned by getSiteManager().
16+ """
17+ super(ComponentsFixture, self).__init__()
18+ if bases is None:
19+ bases = (getSiteManager(),)
20+ self._bases = bases
21
22 def setUp(self):
23 super(ComponentsFixture, self).setUp()
24- self.registry = Components(bases=(getSiteManager(),))
25+ self.registry = Components(bases=self._bases)
26+ if getSiteManager.implementation is not getSiteManager.original:
27+ # This means that sethook was already called by some other codem,
28+ # let's keep track of this hook so we can restore it upon cleanup.
29+ self._old_implementation = getSiteManager.implementation
30 getSiteManager.sethook(lambda context=None: self.registry)
31- self.addCleanup(getSiteManager.reset)
32+ self.addCleanup(self._reset_hook)
33+
34+ def _reset_hook(self):
35+ getSiteManager.reset()
36+ if self._old_implementation is not None:
37+ getSiteManager.sethook(self._old_implementation)
38
39
40 class UtilityFixture(Fixture):
41
42=== modified file 'lib/zope_fixtures/tests/test_components.py'
43--- lib/zope_fixtures/tests/test_components.py 2011-11-23 09:12:55 +0000
44+++ lib/zope_fixtures/tests/test_components.py 2012-01-05 22:25:28 +0000
45@@ -17,6 +17,7 @@
46
47 from zope.interface import Interface, implements
48 from zope.component import provideUtility, queryUtility, getSiteManager
49+from zope.component.registry import Components
50
51 from zope_fixtures import ComponentsFixture, UtilityFixture
52
53@@ -68,6 +69,27 @@
54 self.assertIs(overridden_utility, queryUtility(ITestUtility))
55 self.assertIs(original_utility, queryUtility(ITestUtility))
56
57+ def test_restores_original_implementation_hook(self):
58+ original = getSiteManager.original
59+ registry = Components()
60+ implementation = lambda context=None: registry
61+ getSiteManager.sethook(implementation)
62+ self.assertIs(registry, getSiteManager())
63+ fixture = ComponentsFixture()
64+ with fixture:
65+ self.assertIs(fixture.registry, getSiteManager())
66+ self.assertIs(original, getSiteManager.original)
67+ self.assertIs(implementation, getSiteManager.implementation)
68+ self.assertIs(registry, getSiteManager())
69+ getSiteManager.reset()
70+
71+ def test_use_alternate_bases(self):
72+ registry = Components()
73+ utility = TestUtility()
74+ registry.registerUtility(utility)
75+ self.useFixture(ComponentsFixture(bases=(registry,)))
76+ self.assertIs(utility, queryUtility(ITestUtility))
77+
78
79 class TestUtilityFixture(testtools.TestCase):
80

Subscribers

People subscribed via source and target branches