Browse Source

tests: Use .addCleanup in a couple more places.

Jelmer Vernooij 13 năm trước cách đây
mục cha
commit
63b947f4c3

+ 2 - 14
dulwich/tests/compat/server_utils.py

@@ -46,23 +46,11 @@ class ServerTests(object):
     Does not inherit from TestCase so tests are not automatically run.
     """
 
-    def setUp(self):
-        self._old_repo = None
-        self._new_repo = None
-        self._server = None
-
-    def tearDown(self):
-        if self._server is not None:
-            self._server.shutdown()
-            self._server = None
-        if self._old_repo is not None:
-            tear_down_repo(self._old_repo)
-        if self._new_repo is not None:
-            tear_down_repo(self._new_repo)
-
     def import_repos(self):
         self._old_repo = import_repo('server_old.export')
+        self.addCleanup(tear_down_repo, self._old_repo)
         self._new_repo = import_repo('server_new.export')
+        self.addCleanup(tear_down_repo, self._new_repo)
 
     def url(self, port):
         return '%s://localhost:%s/' % (self.protocol, port)

+ 1 - 8
dulwich/tests/compat/test_server.py

@@ -62,14 +62,6 @@ class GitServerTestCase(ServerTests, CompatTestCase):
 
     protocol = 'git'
 
-    def setUp(self):
-        ServerTests.setUp(self)
-        CompatTestCase.setUp(self)
-
-    def tearDown(self):
-        ServerTests.tearDown(self)
-        CompatTestCase.tearDown(self)
-
     def _handlers(self):
         return {'git-receive-pack': NoSideBand64kReceivePackHandler}
 
@@ -83,6 +75,7 @@ class GitServerTestCase(ServerTests, CompatTestCase):
         dul_server = TCPGitServer(backend, 'localhost', 0,
                                   handlers=self._handlers())
         self._check_server(dul_server)
+        self.addCleanup(dul_server.shutdown)
         threading.Thread(target=dul_server.serve).start()
         self._server = dul_server
         _, port = self._server.socket.getsockname()

+ 1 - 16
dulwich/tests/compat/test_web.py

@@ -77,6 +77,7 @@ class WebTests(ServerTests):
         dul_server = simple_server.make_server(
           'localhost', 0, app, server_class=WSGIServer,
           handler_class=HTTPGitRequestHandler)
+        self.addCleanup(dul_server.shutdown)
         threading.Thread(target=dul_server.serve_forever).start()
         self._server = dul_server
         _, port = dul_server.socket.getsockname()
@@ -91,14 +92,6 @@ class SmartWebTestCase(WebTests, CompatTestCase):
 
     min_git_version = (1, 6, 6)
 
-    def setUp(self):
-        WebTests.setUp(self)
-        CompatTestCase.setUp(self)
-
-    def tearDown(self):
-        WebTests.tearDown(self)
-        CompatTestCase.tearDown(self)
-
     def _handlers(self):
         return {'git-receive-pack': NoSideBand64kReceivePackHandler}
 
@@ -131,14 +124,6 @@ class SmartWebSideBand64kTestCase(SmartWebTestCase):
 class DumbWebTestCase(WebTests, CompatTestCase):
     """Test cases for dumb HTTP server."""
 
-    def setUp(self):
-        WebTests.setUp(self)
-        CompatTestCase.setUp(self)
-
-    def tearDown(self):
-        WebTests.tearDown(self)
-        CompatTestCase.tearDown(self)
-
     def _make_app(self, backend):
         return HTTPGitApplication(backend, dumb=True)
 

+ 1 - 4
dulwich/tests/test_pack.py

@@ -91,10 +91,7 @@ class PackTests(TestCase):
     def setUp(self):
         super(PackTests, self).setUp()
         self.tempdir = tempfile.mkdtemp()
-
-    def tearDown(self):
-        shutil.rmtree(self.tempdir)
-        super(PackTests, self).tearDown()
+        self.addCleanup(shutil.rmtree, self.tempdir)
 
     datadir = os.path.abspath(os.path.join(os.path.dirname(__file__),
         'data/packs'))

+ 0 - 1
dulwich/tests/utils.py

@@ -20,7 +20,6 @@
 """Utility functions common to Dulwich tests."""
 
 
-from cStringIO import StringIO
 import datetime
 import os
 import shutil