← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~lifeless/launchpad/fixtures into lp:launchpad/devel

 

Robert Collins has proposed merging lp:~lifeless/launchpad/fixtures into lp:launchpad/devel.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)


Cleanup some partially-migrated-from test support code.
-- 
https://code.launchpad.net/~lifeless/launchpad/fixtures/+merge/38668
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~lifeless/launchpad/fixtures into lp:launchpad/devel.
=== modified file 'lib/lp/poppy/tests/test_poppy.py'
--- lib/lp/poppy/tests/test_poppy.py	2010-09-28 22:33:42 +0000
+++ lib/lp/poppy/tests/test_poppy.py	2010-10-17 21:28:46 +0000
@@ -49,7 +49,7 @@
             self.root_dir, port=self.port, cmd='echo CLOSED')
         self.poppy.startPoppy()
 
-    def tearDown(self):
+    def cleanUp(self):
         self.poppy.killPoppy()
 
     def getTransport(self):
@@ -129,7 +129,7 @@
         self._tac = PoppyTac(self.root_dir)
         self._tac.setUp()
 
-    def tearDown(self):
+    def cleanUp(self):
         shutil.rmtree(self._home_dir)
         os.environ['HOME'] = self._current_home
         self._tac.tearDown()
@@ -199,7 +199,7 @@
         super(TestPoppy, self).setUp()
         self.root_dir = self.makeTemporaryDirectory()
         self.server = self.server_factory(self.root_dir, self.factory)
-        self.installFixture(self.server)
+        self.useFixture(self.server)
 
     def _uploadPath(self, path):
         """Return system path of specified path inside an upload.

=== modified file 'lib/lp/testing/__init__.py'
--- lib/lp/testing/__init__.py	2010-10-05 01:54:15 +0000
+++ lib/lp/testing/__init__.py	2010-10-17 21:28:46 +0000
@@ -325,20 +325,6 @@
         transaction.commit()
         self.layer.switchDbUser(dbuser)
 
-    def installFixture(self, fixture):
-        """Install 'fixture', an object that has a `setUp` and `tearDown`.
-
-        `installFixture` will run 'fixture.setUp' and schedule
-        'fixture.tearDown' to be run during the test's tear down (using
-        `addCleanup`).
-
-        :param fixture: Any object that has a `setUp` and `tearDown` method.
-        :return: `fixture`.
-        """
-        fixture.setUp()
-        self.addCleanup(fixture.tearDown)
-        return fixture
-
     def __str__(self):
         """The string representation of a test is its id.
 
@@ -511,7 +497,7 @@
         self.factory = ObjectFactory()
         # Record the oopses generated during the test run.
         self.oopses = []
-        self.installFixture(ZopeEventHandlerFixture(self._recordOops))
+        self.useFixture(ZopeEventHandlerFixture(self._recordOops))
         self.addCleanup(self.attachOopses)
 
     @adapter(ErrorReportEvent)

=== modified file 'lib/lp/testing/fixture.py'
--- lib/lp/testing/fixture.py	2010-08-20 20:31:18 +0000
+++ lib/lp/testing/fixture.py	2010-10-17 21:28:46 +0000
@@ -1,121 +1,25 @@
 # Copyright 2009, 2010 Canonical Ltd.  This software is licensed under the
 # GNU Affero General Public License version 3 (see the file LICENSE).
 
-# pylint: disable-msg=E0211
-
-"""Basic support for 'fixtures'.
-
-In this case, 'fixture' means an object that has a setUp and a tearDown
-method.
-"""
+"""Launchpad test fixtures that have no better home."""
 
 __metaclass__ = type
 __all__ = [
-    'Fixtures',
-    'FixtureWithCleanup',
-    'IFixture',
-    'run_with_fixture',
-    'ServerFixture',
-    'with_fixture',
+    'ZopeEventHandlerFixture',
     ]
 
-from twisted.python.util import mergeFunctionMetadata
+from fixtures import Fixture
 from zope.component import (
     getGlobalSiteManager,
     provideHandler,
     )
-from zope.interface import (
-    implements,
-    Interface,
-    )
-
-
-class IFixture(Interface):
-    """A fixture has a setUp and a tearDown method."""
-
-    def setUp():
-        """Set up the fixture."""
-
-    def tearDown():
-        """Tear down the fixture."""
-
-
-class FixtureWithCleanup:
-    """Fixture that allows arbitrary cleanup methods to be added.
-
-    Subclass this if you'd like to define a fixture that calls 'addCleanup'.
-    This is most often useful for fixtures that provide a way for users to
-    acquire resources arbitrarily.
-
-    Cleanups are run during 'tearDown' in reverse order to the order they were
-    added. If any of the cleanups raise an error, this error will be bubbled
-    up, causing tearDown to raise an exception, and the rest of the cleanups
-    will be run in a finally block.
-    """
-
-    implements(IFixture)
-
-    def setUp(self):
-        """See `IFixture`."""
-        self._cleanups = []
-
-    def _runCleanups(self):
-        if [] == self._cleanups:
-            return
-        f, args, kwargs = self._cleanups.pop()
-        try:
-            f(*args, **kwargs)
-        finally:
-            self._runCleanups()
-
-    def tearDown(self):
-        """See `IFixture`."""
-        self._runCleanups()
-
-    def addCleanup(self, function, *args, **kwargs):
-        """Run 'function' with arguments during tear down."""
-        self._cleanups.append((function, args, kwargs))
-
-
-class Fixtures(FixtureWithCleanup):
-    """A collection of `IFixture`s."""
-
-    def __init__(self, fixtures):
-        """Construct a fixture that groups many fixtures together.
-
-        :param fixtures: A list of `IFixture` objects.
-        """
-        self._fixtures = fixtures
-
-    def setUp(self):
-        super(Fixtures, self).setUp()
-        for fixture in self._fixtures:
-            fixture.setUp()
-            self.addCleanup(fixture.tearDown)
-
-
-def with_fixture(fixture):
-    """Decorate a function to run with a given fixture."""
-    def decorator(f):
-        def decorated(*args, **kwargs):
-            return run_with_fixture(fixture, f, fixture, *args, **kwargs)
-        return mergeFunctionMetadata(f, decorated)
-    return decorator
-
-
-def run_with_fixture(fixture, f, *args, **kwargs):
-    """Run `f` within the given `fixture`."""
-    try:
-        fixture.setUp()
-        return f(*args, **kwargs)
-    finally:
-        fixture.tearDown()
-
-
-class ZopeEventHandlerFixture(FixtureWithCleanup):
+
+
+class ZopeEventHandlerFixture(Fixture):
     """A fixture that provides and then unprovides a Zope event handler."""
 
     def __init__(self, handler):
+        super(ZopeEventHandlerFixture, self).__init__()
         self._handler = handler
 
     def setUp(self):
@@ -123,18 +27,3 @@
         gsm = getGlobalSiteManager()
         provideHandler(self._handler)
         self.addCleanup(gsm.unregisterHandler, self._handler)
-
-
-class ServerFixture:
-    """Adapt a bzrlib `Server` into an `IFixture`."""
-
-    implements(IFixture)
-
-    def __init__(self, server):
-        self.server = server
-
-    def setUp(self):
-        self.server.start_server()
-
-    def tearDown(self):
-        self.server.stop_server()

=== removed file 'lib/lp/testing/tests/test_fixture.py'
--- lib/lp/testing/tests/test_fixture.py	2010-08-20 20:31:18 +0000
+++ lib/lp/testing/tests/test_fixture.py	1970-01-01 00:00:00 +0000
@@ -1,138 +0,0 @@
-# Copyright 2009 Canonical Ltd.  This software is licensed under the
-# GNU Affero General Public License version 3 (see the file LICENSE).
-
-"""Tests for fixture support."""
-
-__metaclass__ = type
-
-import unittest
-
-from zope.interface import implements
-
-from lp.testing import TestCase
-from lp.testing.fixture import (
-    Fixtures,
-    FixtureWithCleanup,
-    IFixture,
-    run_with_fixture,
-    with_fixture,
-    )
-
-
-class LoggingFixture:
-
-    implements(IFixture)
-
-    def __init__(self, log):
-        self.log = log
-
-    def setUp(self):
-        self.log.append('setUp')
-
-    def tearDown(self):
-        self.log.append('tearDown')
-
-
-class TestFixture(TestCase):
-
-    def test_run_with_fixture(self):
-        # run_with_fixture runs the setUp method of the fixture, the passed
-        # function and then the tearDown method of the fixture.
-        log = []
-        fixture = LoggingFixture(log)
-        run_with_fixture(fixture, log.append, 'hello')
-        self.assertEqual(['setUp', 'hello', 'tearDown'], log)
-
-    def test_run_tearDown_even_with_exception(self):
-        # run_with_fixture runs the setUp method of the fixture, the passed
-        # function and then the tearDown method of the fixture even if the
-        # function raises an exception.
-        log = []
-        fixture = LoggingFixture(log)
-        self.assertRaises(
-            ZeroDivisionError, run_with_fixture, fixture, lambda: 1/0)
-        self.assertEqual(['setUp', 'tearDown'], log)
-
-    def test_with_fixture(self):
-        # with_fixture decorates a function so that it gets passed the fixture
-        # and the fixture is set up and torn down around the function.
-        log = []
-        fixture = LoggingFixture(log)
-        @with_fixture(fixture)
-        def function(fixture, **kwargs):
-            log.append(fixture)
-            log.append(kwargs)
-            return 'oi'
-        result = function(foo='bar')
-        self.assertEqual('oi', result)
-        self.assertEqual(['setUp', fixture, {'foo': 'bar'}, 'tearDown'], log)
-
-
-class TestFixtureWithCleanup(TestCase):
-    """Tests for `FixtureWithCleanup`."""
-
-    def test_cleanup_called_during_teardown(self):
-        log = []
-        fixture = FixtureWithCleanup()
-        fixture.setUp()
-        fixture.addCleanup(log.append, 'foo')
-        self.assertEqual([], log)
-        fixture.tearDown()
-        self.assertEqual(['foo'], log)
-
-    def test_cleanup_called_in_reverse_order(self):
-        log = []
-        fixture = FixtureWithCleanup()
-        fixture.setUp()
-        fixture.addCleanup(log.append, 'foo')
-        fixture.addCleanup(log.append, 'bar')
-        fixture.tearDown()
-        self.assertEqual(['bar', 'foo'], log)
-
-    def test_cleanup_run_even_in_failure(self):
-        log = []
-        fixture = FixtureWithCleanup()
-        fixture.setUp()
-        fixture.addCleanup(log.append, 'foo')
-        fixture.addCleanup(lambda: 1/0)
-        self.assertRaises(ZeroDivisionError, fixture.tearDown)
-        self.assertEqual(['foo'], log)
-
-
-class TestFixtures(TestCase):
-    """Tests the `Fixtures` class, which groups multiple `IFixture`s."""
-
-    class LoggingFixture:
-
-        def __init__(self, log):
-            self._log = log
-
-        def setUp(self):
-            self._log.append((self, 'setUp'))
-
-        def tearDown(self):
-            self._log.append((self, 'tearDown'))
-
-    def test_with_single_fixture(self):
-        log = []
-        a = self.LoggingFixture(log)
-        fixtures = Fixtures([a])
-        fixtures.setUp()
-        fixtures.tearDown()
-        self.assertEqual([(a, 'setUp'), (a, 'tearDown')], log)
-
-    def test_with_multiple_fixtures(self):
-        log = []
-        a = self.LoggingFixture(log)
-        b = self.LoggingFixture(log)
-        fixtures = Fixtures([a, b])
-        fixtures.setUp()
-        fixtures.tearDown()
-        self.assertEqual(
-            [(a, 'setUp'), (b, 'setUp'), (b, 'tearDown'), (a, 'tearDown')],
-            log)
-
-
-def test_suite():
-    return unittest.TestLoader().loadTestsFromName(__name__)
-