瀏覽代碼

Use new unittest addCleanup method.

Jelmer Vernooij 13 年之前
父節點
當前提交
521ff969bf
共有 2 個文件被更改,包括 25 次插入46 次删除
  1. 0 1
      dulwich/tests/test_pack.py
  2. 25 45
      dulwich/tests/test_repository.py

+ 0 - 1
dulwich/tests/test_pack.py

@@ -330,7 +330,6 @@ class TestPack(PackTests):
         finally:
             origpack.close()
 
-
     def test_commit_obj(self):
         p = self.get_pack(pack1_sha)
         commit = p[commit_sha]

+ 25 - 45
dulwich/tests/test_repository.py

@@ -76,21 +76,17 @@ class CreateRepositoryTests(TestCase):
 
     def test_create_disk_bare(self):
         tmp_dir = tempfile.mkdtemp()
-        try:
-            repo = Repo.init_bare(tmp_dir)
-            self.assertEquals(tmp_dir, repo._controldir)
-            self._check_repo_contents(repo, True)
-        finally:
-            shutil.rmtree(tmp_dir)
+        self.addCleanup(shutil.rmtree, tmp_dir)
+        repo = Repo.init_bare(tmp_dir)
+        self.assertEquals(tmp_dir, repo._controldir)
+        self._check_repo_contents(repo, True)
 
     def test_create_disk_non_bare(self):
         tmp_dir = tempfile.mkdtemp()
-        try:
-            repo = Repo.init(tmp_dir)
-            self.assertEquals(os.path.join(tmp_dir, '.git'), repo._controldir)
-            self._check_repo_contents(repo, False)
-        finally:
-            shutil.rmtree(tmp_dir)
+        self.addCleanup(shutil.rmtree, tmp_dir)
+        repo = Repo.init(tmp_dir)
+        self.assertEquals(os.path.join(tmp_dir, '.git'), repo._controldir)
+        self._check_repo_contents(repo, False)
 
     def test_create_memory(self):
         repo = MemoryRepo.init_bare([], {})
@@ -171,48 +167,38 @@ class RepositoryTests(TestCase):
     def test_commit(self):
         r = self._repo = open_repo('a.git')
         warnings.simplefilter("ignore", DeprecationWarning)
-        try:
-            obj = r.commit(r.head())
-        finally:
-            warnings.resetwarnings()
+        self.addCleanup(warnings.resetwarnings)
+        obj = r.commit(r.head())
         self.assertEqual(obj.type_name, 'commit')
 
     def test_commit_not_commit(self):
         r = self._repo = open_repo('a.git')
         warnings.simplefilter("ignore", DeprecationWarning)
-        try:
-            self.assertRaises(errors.NotCommitError,
-                r.commit, '4f2e6529203aa6d44b5af6e3292c837ceda003f9')
-        finally:
-            warnings.resetwarnings()
+        self.addCleanup(warnings.resetwarnings)
+        self.assertRaises(errors.NotCommitError,
+            r.commit, '4f2e6529203aa6d44b5af6e3292c837ceda003f9')
 
     def test_tree(self):
         r = self._repo = open_repo('a.git')
         commit = r[r.head()]
         warnings.simplefilter("ignore", DeprecationWarning)
-        try:
-            tree = r.tree(commit.tree)
-        finally:
-            warnings.resetwarnings()
+        self.addCleanup(warnings.resetwarnings)
+        tree = r.tree(commit.tree)
         self.assertEqual(tree.type_name, 'tree')
         self.assertEqual(tree.sha().hexdigest(), commit.tree)
 
     def test_tree_not_tree(self):
         r = self._repo = open_repo('a.git')
         warnings.simplefilter("ignore", DeprecationWarning)
-        try:
-            self.assertRaises(errors.NotTreeError, r.tree, r.head())
-        finally:
-            warnings.resetwarnings()
+        self.addCleanup(warnings.resetwarnings)
+        self.assertRaises(errors.NotTreeError, r.tree, r.head())
 
     def test_tag(self):
         r = self._repo = open_repo('a.git')
         tag_sha = '28237f4dc30d0d462658d6b937b08a0f0b6ef55a'
         warnings.simplefilter("ignore", DeprecationWarning)
-        try:
-            tag = r.tag(tag_sha)
-        finally:
-            warnings.resetwarnings()
+        self.addCleanup(warnings.resetwarnings)
+        tag = r.tag(tag_sha)
         self.assertEqual(tag.type_name, 'tag')
         self.assertEqual(tag.sha().hexdigest(), tag_sha)
         obj_class, obj_sha = tag.object
@@ -222,10 +208,8 @@ class RepositoryTests(TestCase):
     def test_tag_not_tag(self):
         r = self._repo = open_repo('a.git')
         warnings.simplefilter("ignore", DeprecationWarning)
-        try:
-            self.assertRaises(errors.NotTagError, r.tag, r.head())
-        finally:
-            warnings.resetwarnings()
+        self.addCleanup(warnings.resetwarnings)
+        self.assertRaises(errors.NotTagError, r.tag, r.head())
 
     def test_get_peeled(self):
         # unpacked ref
@@ -252,20 +236,16 @@ class RepositoryTests(TestCase):
         tree = r[commit.tree]
         blob_sha = tree.items()[0][2]
         warnings.simplefilter("ignore", DeprecationWarning)
-        try:
-            blob = r.get_blob(blob_sha)
-        finally:
-            warnings.resetwarnings()
+        self.addCleanup(warnings.resetwarnings)
+        blob = r.get_blob(blob_sha)
         self.assertEqual(blob.type_name, 'blob')
         self.assertEqual(blob.sha().hexdigest(), blob_sha)
 
     def test_get_blob_notblob(self):
         r = self._repo = open_repo('a.git')
         warnings.simplefilter("ignore", DeprecationWarning)
-        try:
-            self.assertRaises(errors.NotBlobError, r.get_blob, r.head())
-        finally:
-            warnings.resetwarnings()
+        self.addCleanup(warnings.resetwarnings)
+        self.assertRaises(errors.NotBlobError, r.get_blob, r.head())
 
     def test_linear_history(self):
         r = self._repo = open_repo('a.git')