Merge lp:~lifeless/launchpad/fixtures into lp:launchpad

Proposed by Robert Collins
Status: Merged
Approved by: Michael Hudson-Doyle
Approved revision: no longer in the source branch.
Merged at revision: 11736
Proposed branch: lp:~lifeless/launchpad/fixtures
Merge into: lp:launchpad
Diff against target: 359 lines (+11/-274)
4 files modified
lib/lp/poppy/tests/test_poppy.py (+3/-3)
lib/lp/testing/__init__.py (+1/-15)
lib/lp/testing/fixture.py (+7/-118)
lib/lp/testing/tests/test_fixture.py (+0/-138)
To merge this branch: bzr merge lp:~lifeless/launchpad/fixtures
Reviewer Review Type Date Requested Status
Michael Hudson-Doyle Approve
Review via email: mp+38668@code.launchpad.net

Commit message

Cleanup some partially-migrated-from test support code.

Description of the change

Cleanup some partially-migrated-from test support code.

To post a comment you must log in.
Revision history for this message
Michael Hudson-Doyle (mwhudson) wrote :

Looks nice, thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/poppy/tests/test_poppy.py'
2--- lib/lp/poppy/tests/test_poppy.py 2010-09-28 22:33:42 +0000
3+++ lib/lp/poppy/tests/test_poppy.py 2010-10-17 21:28:46 +0000
4@@ -49,7 +49,7 @@
5 self.root_dir, port=self.port, cmd='echo CLOSED')
6 self.poppy.startPoppy()
7
8- def tearDown(self):
9+ def cleanUp(self):
10 self.poppy.killPoppy()
11
12 def getTransport(self):
13@@ -129,7 +129,7 @@
14 self._tac = PoppyTac(self.root_dir)
15 self._tac.setUp()
16
17- def tearDown(self):
18+ def cleanUp(self):
19 shutil.rmtree(self._home_dir)
20 os.environ['HOME'] = self._current_home
21 self._tac.tearDown()
22@@ -199,7 +199,7 @@
23 super(TestPoppy, self).setUp()
24 self.root_dir = self.makeTemporaryDirectory()
25 self.server = self.server_factory(self.root_dir, self.factory)
26- self.installFixture(self.server)
27+ self.useFixture(self.server)
28
29 def _uploadPath(self, path):
30 """Return system path of specified path inside an upload.
31
32=== modified file 'lib/lp/testing/__init__.py'
33--- lib/lp/testing/__init__.py 2010-10-05 01:54:15 +0000
34+++ lib/lp/testing/__init__.py 2010-10-17 21:28:46 +0000
35@@ -325,20 +325,6 @@
36 transaction.commit()
37 self.layer.switchDbUser(dbuser)
38
39- def installFixture(self, fixture):
40- """Install 'fixture', an object that has a `setUp` and `tearDown`.
41-
42- `installFixture` will run 'fixture.setUp' and schedule
43- 'fixture.tearDown' to be run during the test's tear down (using
44- `addCleanup`).
45-
46- :param fixture: Any object that has a `setUp` and `tearDown` method.
47- :return: `fixture`.
48- """
49- fixture.setUp()
50- self.addCleanup(fixture.tearDown)
51- return fixture
52-
53 def __str__(self):
54 """The string representation of a test is its id.
55
56@@ -511,7 +497,7 @@
57 self.factory = ObjectFactory()
58 # Record the oopses generated during the test run.
59 self.oopses = []
60- self.installFixture(ZopeEventHandlerFixture(self._recordOops))
61+ self.useFixture(ZopeEventHandlerFixture(self._recordOops))
62 self.addCleanup(self.attachOopses)
63
64 @adapter(ErrorReportEvent)
65
66=== modified file 'lib/lp/testing/fixture.py'
67--- lib/lp/testing/fixture.py 2010-08-20 20:31:18 +0000
68+++ lib/lp/testing/fixture.py 2010-10-17 21:28:46 +0000
69@@ -1,121 +1,25 @@
70 # Copyright 2009, 2010 Canonical Ltd. This software is licensed under the
71 # GNU Affero General Public License version 3 (see the file LICENSE).
72
73-# pylint: disable-msg=E0211
74-
75-"""Basic support for 'fixtures'.
76-
77-In this case, 'fixture' means an object that has a setUp and a tearDown
78-method.
79-"""
80+"""Launchpad test fixtures that have no better home."""
81
82 __metaclass__ = type
83 __all__ = [
84- 'Fixtures',
85- 'FixtureWithCleanup',
86- 'IFixture',
87- 'run_with_fixture',
88- 'ServerFixture',
89- 'with_fixture',
90+ 'ZopeEventHandlerFixture',
91 ]
92
93-from twisted.python.util import mergeFunctionMetadata
94+from fixtures import Fixture
95 from zope.component import (
96 getGlobalSiteManager,
97 provideHandler,
98 )
99-from zope.interface import (
100- implements,
101- Interface,
102- )
103-
104-
105-class IFixture(Interface):
106- """A fixture has a setUp and a tearDown method."""
107-
108- def setUp():
109- """Set up the fixture."""
110-
111- def tearDown():
112- """Tear down the fixture."""
113-
114-
115-class FixtureWithCleanup:
116- """Fixture that allows arbitrary cleanup methods to be added.
117-
118- Subclass this if you'd like to define a fixture that calls 'addCleanup'.
119- This is most often useful for fixtures that provide a way for users to
120- acquire resources arbitrarily.
121-
122- Cleanups are run during 'tearDown' in reverse order to the order they were
123- added. If any of the cleanups raise an error, this error will be bubbled
124- up, causing tearDown to raise an exception, and the rest of the cleanups
125- will be run in a finally block.
126- """
127-
128- implements(IFixture)
129-
130- def setUp(self):
131- """See `IFixture`."""
132- self._cleanups = []
133-
134- def _runCleanups(self):
135- if [] == self._cleanups:
136- return
137- f, args, kwargs = self._cleanups.pop()
138- try:
139- f(*args, **kwargs)
140- finally:
141- self._runCleanups()
142-
143- def tearDown(self):
144- """See `IFixture`."""
145- self._runCleanups()
146-
147- def addCleanup(self, function, *args, **kwargs):
148- """Run 'function' with arguments during tear down."""
149- self._cleanups.append((function, args, kwargs))
150-
151-
152-class Fixtures(FixtureWithCleanup):
153- """A collection of `IFixture`s."""
154-
155- def __init__(self, fixtures):
156- """Construct a fixture that groups many fixtures together.
157-
158- :param fixtures: A list of `IFixture` objects.
159- """
160- self._fixtures = fixtures
161-
162- def setUp(self):
163- super(Fixtures, self).setUp()
164- for fixture in self._fixtures:
165- fixture.setUp()
166- self.addCleanup(fixture.tearDown)
167-
168-
169-def with_fixture(fixture):
170- """Decorate a function to run with a given fixture."""
171- def decorator(f):
172- def decorated(*args, **kwargs):
173- return run_with_fixture(fixture, f, fixture, *args, **kwargs)
174- return mergeFunctionMetadata(f, decorated)
175- return decorator
176-
177-
178-def run_with_fixture(fixture, f, *args, **kwargs):
179- """Run `f` within the given `fixture`."""
180- try:
181- fixture.setUp()
182- return f(*args, **kwargs)
183- finally:
184- fixture.tearDown()
185-
186-
187-class ZopeEventHandlerFixture(FixtureWithCleanup):
188+
189+
190+class ZopeEventHandlerFixture(Fixture):
191 """A fixture that provides and then unprovides a Zope event handler."""
192
193 def __init__(self, handler):
194+ super(ZopeEventHandlerFixture, self).__init__()
195 self._handler = handler
196
197 def setUp(self):
198@@ -123,18 +27,3 @@
199 gsm = getGlobalSiteManager()
200 provideHandler(self._handler)
201 self.addCleanup(gsm.unregisterHandler, self._handler)
202-
203-
204-class ServerFixture:
205- """Adapt a bzrlib `Server` into an `IFixture`."""
206-
207- implements(IFixture)
208-
209- def __init__(self, server):
210- self.server = server
211-
212- def setUp(self):
213- self.server.start_server()
214-
215- def tearDown(self):
216- self.server.stop_server()
217
218=== removed file 'lib/lp/testing/tests/test_fixture.py'
219--- lib/lp/testing/tests/test_fixture.py 2010-08-20 20:31:18 +0000
220+++ lib/lp/testing/tests/test_fixture.py 1970-01-01 00:00:00 +0000
221@@ -1,138 +0,0 @@
222-# Copyright 2009 Canonical Ltd. This software is licensed under the
223-# GNU Affero General Public License version 3 (see the file LICENSE).
224-
225-"""Tests for fixture support."""
226-
227-__metaclass__ = type
228-
229-import unittest
230-
231-from zope.interface import implements
232-
233-from lp.testing import TestCase
234-from lp.testing.fixture import (
235- Fixtures,
236- FixtureWithCleanup,
237- IFixture,
238- run_with_fixture,
239- with_fixture,
240- )
241-
242-
243-class LoggingFixture:
244-
245- implements(IFixture)
246-
247- def __init__(self, log):
248- self.log = log
249-
250- def setUp(self):
251- self.log.append('setUp')
252-
253- def tearDown(self):
254- self.log.append('tearDown')
255-
256-
257-class TestFixture(TestCase):
258-
259- def test_run_with_fixture(self):
260- # run_with_fixture runs the setUp method of the fixture, the passed
261- # function and then the tearDown method of the fixture.
262- log = []
263- fixture = LoggingFixture(log)
264- run_with_fixture(fixture, log.append, 'hello')
265- self.assertEqual(['setUp', 'hello', 'tearDown'], log)
266-
267- def test_run_tearDown_even_with_exception(self):
268- # run_with_fixture runs the setUp method of the fixture, the passed
269- # function and then the tearDown method of the fixture even if the
270- # function raises an exception.
271- log = []
272- fixture = LoggingFixture(log)
273- self.assertRaises(
274- ZeroDivisionError, run_with_fixture, fixture, lambda: 1/0)
275- self.assertEqual(['setUp', 'tearDown'], log)
276-
277- def test_with_fixture(self):
278- # with_fixture decorates a function so that it gets passed the fixture
279- # and the fixture is set up and torn down around the function.
280- log = []
281- fixture = LoggingFixture(log)
282- @with_fixture(fixture)
283- def function(fixture, **kwargs):
284- log.append(fixture)
285- log.append(kwargs)
286- return 'oi'
287- result = function(foo='bar')
288- self.assertEqual('oi', result)
289- self.assertEqual(['setUp', fixture, {'foo': 'bar'}, 'tearDown'], log)
290-
291-
292-class TestFixtureWithCleanup(TestCase):
293- """Tests for `FixtureWithCleanup`."""
294-
295- def test_cleanup_called_during_teardown(self):
296- log = []
297- fixture = FixtureWithCleanup()
298- fixture.setUp()
299- fixture.addCleanup(log.append, 'foo')
300- self.assertEqual([], log)
301- fixture.tearDown()
302- self.assertEqual(['foo'], log)
303-
304- def test_cleanup_called_in_reverse_order(self):
305- log = []
306- fixture = FixtureWithCleanup()
307- fixture.setUp()
308- fixture.addCleanup(log.append, 'foo')
309- fixture.addCleanup(log.append, 'bar')
310- fixture.tearDown()
311- self.assertEqual(['bar', 'foo'], log)
312-
313- def test_cleanup_run_even_in_failure(self):
314- log = []
315- fixture = FixtureWithCleanup()
316- fixture.setUp()
317- fixture.addCleanup(log.append, 'foo')
318- fixture.addCleanup(lambda: 1/0)
319- self.assertRaises(ZeroDivisionError, fixture.tearDown)
320- self.assertEqual(['foo'], log)
321-
322-
323-class TestFixtures(TestCase):
324- """Tests the `Fixtures` class, which groups multiple `IFixture`s."""
325-
326- class LoggingFixture:
327-
328- def __init__(self, log):
329- self._log = log
330-
331- def setUp(self):
332- self._log.append((self, 'setUp'))
333-
334- def tearDown(self):
335- self._log.append((self, 'tearDown'))
336-
337- def test_with_single_fixture(self):
338- log = []
339- a = self.LoggingFixture(log)
340- fixtures = Fixtures([a])
341- fixtures.setUp()
342- fixtures.tearDown()
343- self.assertEqual([(a, 'setUp'), (a, 'tearDown')], log)
344-
345- def test_with_multiple_fixtures(self):
346- log = []
347- a = self.LoggingFixture(log)
348- b = self.LoggingFixture(log)
349- fixtures = Fixtures([a, b])
350- fixtures.setUp()
351- fixtures.tearDown()
352- self.assertEqual(
353- [(a, 'setUp'), (b, 'setUp'), (b, 'tearDown'), (a, 'tearDown')],
354- log)
355-
356-
357-def test_suite():
358- return unittest.TestLoader().loadTestsFromName(__name__)
359-