فهرست منبع

Fix Python3 compatibility and warnings during tests.

Jelmer Vernooij 8 سال پیش
والد
کامیت
464d0e281a
3فایلهای تغییر یافته به همراه20 افزوده شده و 24 حذف شده
  1. 6 4
      dulwich/repo.py
  2. 13 19
      dulwich/tests/compat/test_repository.py
  3. 1 1
      dulwich/tests/test_porcelain.py

+ 6 - 4
dulwich/repo.py

@@ -681,8 +681,10 @@ class Repo(BaseRepo):
             )
         commondir = self.get_named_file(COMMONDIR)
         if commondir is not None:
-            self._commondir = os.path.join(
-                self.controldir(), commondir.read().rstrip("\r\n"))
+            with commondir:
+                self._commondir = os.path.join(
+                    self.controldir(),
+                    commondir.read().rstrip(b"\r\n").decode(sys.getfilesystemencoding()))
         else:
             self._commondir = self._controldir
         self.path = root
@@ -975,7 +977,7 @@ class Repo(BaseRepo):
         worktree_controldir = os.path.join(main_worktreesdir, identifier)
         gitdirfile = os.path.join(path, CONTROLDIR)
         with open(gitdirfile, 'wb') as f:
-            f.write('gitdir: ' +
+            f.write(b'gitdir: ' +
                     worktree_controldir.encode(sys.getfilesystemencoding()) +
                     b'\n')
         try:
@@ -989,7 +991,7 @@ class Repo(BaseRepo):
             if e.errno != errno.EEXIST:
                 raise
         with open(os.path.join(worktree_controldir, GITDIR), 'wb') as f:
-            f.write(gitdirfile + b'\n')
+            f.write(gitdirfile.encode(sys.getfilesystemencoding()) + b'\n')
         with open(os.path.join(worktree_controldir, COMMONDIR), 'wb') as f:
             f.write(b'../..\n')
         with open(os.path.join(worktree_controldir, 'HEAD'), 'wb') as f:

+ 13 - 19
dulwich/tests/compat/test_repository.py

@@ -64,13 +64,6 @@ class ObjectStoreTestCase(CompatTestCase):
     def _parse_objects(self, output):
         return set(s.rstrip(b'\n').split(b' ')[0] for s in BytesIO(output))
 
-    def _parse_worktree_list(self, output):
-        worktrees = []
-        for line in BytesIO(output):
-            fields = line.rstrip(b'\n').split()
-            worktrees.append(tuple(f.decode() for f in fields))
-        return worktrees
-
     def test_bare(self):
         self.assertTrue(self._repo.bare)
         self.assertFalse(os.path.exists(os.path.join(self._repo.path, '.git')))
@@ -146,22 +139,18 @@ class WorkingTreeTestCase(ObjectStoreTestCase):
         temp_dir = tempfile.mkdtemp()
         run_git_or_fail(['worktree', 'add', temp_dir, branch],
                         cwd=repo_dir)
+        self.addCleanup(rmtree_ro, temp_dir)
         return temp_dir
 
     def setUp(self):
         super(WorkingTreeTestCase, self).setUp()
         self._worktree_path = self.create_new_worktree(self._repo.path, 'branch')
         self._worktree_repo = Repo(self._worktree_path)
+        self.addCleanup(self._worktree_repo.close)
         self._mainworktree_repo = self._repo
         self._number_of_working_tree = 2
         self._repo = self._worktree_repo
 
-    def tearDown(self):
-        self._worktree_repo.close()
-        rmtree_ro(self._worktree_path)
-        self._repo = self._mainworktree_repo
-        super(WorkingTreeTestCase, self).tearDown()
-
     def test_refs(self):
         super(WorkingTreeTestCase, self).test_refs()
         self.assertEqual(self._mainworktree_repo.refs.allkeys(),
@@ -175,6 +164,13 @@ class WorkingTreeTestCase(ObjectStoreTestCase):
         self.assertFalse(self._repo.bare)
         self.assertTrue(os.path.isfile(os.path.join(self._repo.path, '.git')))
 
+    def _parse_worktree_list(self, output):
+        worktrees = []
+        for line in BytesIO(output):
+            fields = line.rstrip(b'\n').split()
+            worktrees.append(tuple(f.decode() for f in fields))
+        return worktrees
+
     def test_git_worktree_list(self):
         output = run_git_or_fail(['worktree', 'list'], cwd=self._repo.path)
         worktrees = self._parse_worktree_list(output)
@@ -197,15 +193,13 @@ class InitNewWorkingDirectoryTestCase(WorkingTreeTestCase):
     def setUp(self):
         super(InitNewWorkingDirectoryTestCase, self).setUp()
         self._other_worktree = self._repo
+        worktree_repo_path = tempfile.mkdtemp()
+        self.addCleanup(rmtree_ro, worktree_repo_path)
         self._repo = Repo._init_new_working_directory(
-            tempfile.mkdtemp(), self._mainworktree_repo)
+            worktree_repo_path, self._mainworktree_repo)
+        self.addCleanup(self._repo.close)
         self._number_of_working_tree = 3
 
-    def tearDown(self):
-        self._repo.close()
-        rmtree_ro(self._repo.path)
-        super(InitNewWorkingDirectoryTestCase, self).tearDown()
-
     def test_head_equality(self):
         self.assertEqual(self._repo.refs[b'HEAD'],
                          self._mainworktree_repo.refs[b'HEAD'])

+ 1 - 1
dulwich/tests/test_porcelain.py

@@ -123,7 +123,7 @@ class CloneTests(PorcelainTestCase):
         self.assertEqual(r.path, target_path)
         target_repo = Repo(target_path)
         self.assertEqual(target_repo.head(), c3.id)
-        self.assertEquals(c3.id, target_repo.refs[b'refs/tags/foo'])
+        self.assertEqual(c3.id, target_repo.refs[b'refs/tags/foo'])
         self.assertTrue(b'f1' not in os.listdir(target_path))
         self.assertTrue(b'f2' not in os.listdir(target_path))