Răsfoiți Sursa

Support python3 for index tests.

Jelmer Vernooij 10 ani în urmă
părinte
comite
8129e12c06
2 a modificat fișierele cu 56 adăugiri și 60 ștergeri
  1. 4 4
      dulwich/index.py
  2. 52 56
      dulwich/tests/test_index.py

+ 4 - 4
dulwich/index.py

@@ -437,7 +437,7 @@ def build_file_from_blob(blob, mode, target_path, honor_filemode=True):
             os.chmod(target_path, mode)
 
 
-INVALID_DOTNAMES = (".git", ".", "..", "")
+INVALID_DOTNAMES = (b".git", b".", b"..", b"")
 
 
 def validate_path_element_default(element):
@@ -445,10 +445,10 @@ def validate_path_element_default(element):
 
 
 def validate_path_element_ntfs(element):
-    stripped = element.rstrip(". ").lower()
+    stripped = element.rstrip(b". ").lower()
     if stripped in INVALID_DOTNAMES:
         return False
-    if stripped == "git~1":
+    if stripped == b"git~1":
         return False
     return True
 
@@ -527,7 +527,7 @@ def get_unstaged_changes(index, path):
     """
     # For each entry in the index check the sha1 & ensure not staged
     for name, entry in index.iteritems():
-        fp = os.path.join(path, name)
+        fp = os.path.join(path, name.decode(sys.getfilesystemencoding()))
         blob = blob_from_path_and_stat(fp, os.lstat(fp))
         if blob.id != entry.sha:
             yield name

+ 52 - 56
dulwich/tests/test_index.py

@@ -50,7 +50,6 @@ from dulwich.objects import (
     )
 from dulwich.repo import Repo
 from dulwich.tests import TestCase
-from dulwich.tests.utils import skipIfPY3
 
 class IndexTestCase(TestCase):
 
@@ -131,7 +130,6 @@ class ReadIndexDictTests(IndexTestCase):
             self.assertEqual(entries, read_index_dict(x))
 
 
-@skipIfPY3
 class CommitTreeTests(TestCase):
 
     def setUp(self):
@@ -140,25 +138,25 @@ class CommitTreeTests(TestCase):
 
     def test_single_blob(self):
         blob = Blob()
-        blob.data = "foo"
+        blob.data = b"foo"
         self.store.add_object(blob)
-        blobs = [("bla", blob.id, stat.S_IFREG)]
+        blobs = [(b"bla", blob.id, stat.S_IFREG)]
         rootid = commit_tree(self.store, blobs)
-        self.assertEqual(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
-        self.assertEqual((stat.S_IFREG, blob.id), self.store[rootid]["bla"])
+        self.assertEqual(rootid, b"1a1e80437220f9312e855c37ac4398b68e5c1d50")
+        self.assertEqual((stat.S_IFREG, blob.id), self.store[rootid][b"bla"])
         self.assertEqual(set([rootid, blob.id]), set(self.store._data.keys()))
 
     def test_nested(self):
         blob = Blob()
-        blob.data = "foo"
+        blob.data = b"foo"
         self.store.add_object(blob)
-        blobs = [("bla/bar", blob.id, stat.S_IFREG)]
+        blobs = [(b"bla/bar", blob.id, stat.S_IFREG)]
         rootid = commit_tree(self.store, blobs)
-        self.assertEqual(rootid, "d92b959b216ad0d044671981196781b3258fa537")
-        dirid = self.store[rootid]["bla"][1]
-        self.assertEqual(dirid, "c1a1deb9788150829579a8b4efa6311e7b638650")
-        self.assertEqual((stat.S_IFDIR, dirid), self.store[rootid]["bla"])
-        self.assertEqual((stat.S_IFREG, blob.id), self.store[dirid]["bar"])
+        self.assertEqual(rootid, b"d92b959b216ad0d044671981196781b3258fa537")
+        dirid = self.store[rootid][b"bla"][1]
+        self.assertEqual(dirid, b"c1a1deb9788150829579a8b4efa6311e7b638650")
+        self.assertEqual((stat.S_IFDIR, dirid), self.store[rootid][b"bla"])
+        self.assertEqual((stat.S_IFREG, blob.id), self.store[dirid][b"bar"])
         self.assertEqual(set([rootid, dirid, blob.id]),
                           set(self.store._data.keys()))
 
@@ -241,7 +239,6 @@ class IndexEntryFromStatTests(TestCase):
             0))
 
 
-@skipIfPY3  # These tests depend on repo.
 class BuildIndexTests(TestCase):
 
     def assertReasonableIndexEntry(self, index_entry, mode, filesize, sha):
@@ -283,12 +280,12 @@ class BuildIndexTests(TestCase):
         self.addCleanup(shutil.rmtree, repo_dir)
 
         # Populate repo
-        filea = Blob.from_string('file a')
-        filee = Blob.from_string('d')
+        filea = Blob.from_string(b'file a')
+        filee = Blob.from_string(b'd')
 
         tree = Tree()
-        tree['.git/a'] = (stat.S_IFREG | 0o644, filea.id)
-        tree['c/e'] = (stat.S_IFREG | 0o644, filee.id)
+        tree[b'.git/a'] = (stat.S_IFREG | 0o644, filea.id)
+        tree[b'c/e'] = (stat.S_IFREG | 0o644, filee.id)
 
         repo.object_store.add_objects([(o, None)
             for o in [filea, filee, tree]])
@@ -307,9 +304,9 @@ class BuildIndexTests(TestCase):
         # filee
         epath = os.path.join(repo.path, 'c', 'e')
         self.assertTrue(os.path.exists(epath))
-        self.assertReasonableIndexEntry(index['c/e'],
+        self.assertReasonableIndexEntry(index[b'c/e'],
             stat.S_IFREG | 0o644, 1, filee.id)
-        self.assertFileContents(epath, 'd')
+        self.assertFileContents(epath, b'd')
 
     def test_nonempty(self):
         if os.name != 'posix':
@@ -320,16 +317,16 @@ class BuildIndexTests(TestCase):
         self.addCleanup(shutil.rmtree, repo_dir)
 
         # Populate repo
-        filea = Blob.from_string('file a')
-        fileb = Blob.from_string('file b')
-        filed = Blob.from_string('file d')
-        filee = Blob.from_string('d')
+        filea = Blob.from_string(b'file a')
+        fileb = Blob.from_string(b'file b')
+        filed = Blob.from_string(b'file d')
+        filee = Blob.from_string(b'd')
 
         tree = Tree()
-        tree['a'] = (stat.S_IFREG | 0o644, filea.id)
-        tree['b'] = (stat.S_IFREG | 0o644, fileb.id)
-        tree['c/d'] = (stat.S_IFREG | 0o644, filed.id)
-        tree['c/e'] = (stat.S_IFLNK, filee.id)  # symlink
+        tree[b'a'] = (stat.S_IFREG | 0o644, filea.id)
+        tree[b'b'] = (stat.S_IFREG | 0o644, fileb.id)
+        tree[b'c/d'] = (stat.S_IFREG | 0o644, filed.id)
+        tree[b'c/e'] = (stat.S_IFLNK, filee.id)  # symlink
 
         repo.object_store.add_objects([(o, None)
             for o in [filea, fileb, filed, filee, tree]])
@@ -344,28 +341,28 @@ class BuildIndexTests(TestCase):
         # filea
         apath = os.path.join(repo.path, 'a')
         self.assertTrue(os.path.exists(apath))
-        self.assertReasonableIndexEntry(index['a'],
+        self.assertReasonableIndexEntry(index[b'a'],
             stat.S_IFREG | 0o644, 6, filea.id)
-        self.assertFileContents(apath, 'file a')
+        self.assertFileContents(apath, b'file a')
 
         # fileb
         bpath = os.path.join(repo.path, 'b')
         self.assertTrue(os.path.exists(bpath))
-        self.assertReasonableIndexEntry(index['b'],
+        self.assertReasonableIndexEntry(index[b'b'],
             stat.S_IFREG | 0o644, 6, fileb.id)
-        self.assertFileContents(bpath, 'file b')
+        self.assertFileContents(bpath, b'file b')
 
         # filed
         dpath = os.path.join(repo.path, 'c', 'd')
         self.assertTrue(os.path.exists(dpath))
-        self.assertReasonableIndexEntry(index['c/d'],
+        self.assertReasonableIndexEntry(index[b'c/d'],
             stat.S_IFREG | 0o644, 6, filed.id)
-        self.assertFileContents(dpath, 'file d')
+        self.assertFileContents(dpath, b'file d')
 
         # symlink to d
         epath = os.path.join(repo.path, 'c', 'e')
         self.assertTrue(os.path.exists(epath))
-        self.assertReasonableIndexEntry(index['c/e'],
+        self.assertReasonableIndexEntry(index[b'c/e'],
             stat.S_IFLNK, 1, filee.id)
         self.assertFileContents(epath, 'd', symlink=True)
 
@@ -376,7 +373,6 @@ class BuildIndexTests(TestCase):
             sorted(os.listdir(os.path.join(repo.path, 'c'))))
 
 
-@skipIfPY3  # These tests depend on repo.
 class GetUnstagedChangesTests(TestCase):
 
     def test_get_unstaged_changes(self):
@@ -388,41 +384,41 @@ class GetUnstagedChangesTests(TestCase):
 
         # Commit a dummy file then modify it
         foo1_fullpath = os.path.join(repo_dir, 'foo1')
-        with open(foo1_fullpath, 'w') as f:
-            f.write('origstuff')
+        with open(foo1_fullpath, 'wb') as f:
+            f.write(b'origstuff')
 
         foo2_fullpath = os.path.join(repo_dir, 'foo2')
-        with open(foo2_fullpath, 'w') as f:
-            f.write('origstuff')
+        with open(foo2_fullpath, 'wb') as f:
+            f.write(b'origstuff')
 
         repo.stage(['foo1', 'foo2'])
-        repo.do_commit('test status', author='', committer='')
+        repo.do_commit(b'test status', author=b'', committer=b'')
 
-        with open(foo1_fullpath, 'w') as f:
-            f.write('newstuff')
+        with open(foo1_fullpath, 'wb') as f:
+            f.write(b'newstuff')
 
         # modify access and modify time of path
         os.utime(foo1_fullpath, (0, 0))
 
         changes = get_unstaged_changes(repo.open_index(), repo_dir)
 
-        self.assertEqual(list(changes), ['foo1'])
+        self.assertEqual(list(changes), [b'foo1'])
 
 
 class TestValidatePathElement(TestCase):
 
     def test_default(self):
-        self.assertTrue(validate_path_element_default("bla"))
-        self.assertTrue(validate_path_element_default(".bla"))
-        self.assertFalse(validate_path_element_default(".git"))
-        self.assertFalse(validate_path_element_default(".giT"))
-        self.assertFalse(validate_path_element_default(".."))
-        self.assertTrue(validate_path_element_default("git~1"))
+        self.assertTrue(validate_path_element_default(b"bla"))
+        self.assertTrue(validate_path_element_default(b".bla"))
+        self.assertFalse(validate_path_element_default(b".git"))
+        self.assertFalse(validate_path_element_default(b".giT"))
+        self.assertFalse(validate_path_element_default(b".."))
+        self.assertTrue(validate_path_element_default(b"git~1"))
 
     def test_ntfs(self):
-        self.assertTrue(validate_path_element_ntfs("bla"))
-        self.assertTrue(validate_path_element_ntfs(".bla"))
-        self.assertFalse(validate_path_element_ntfs(".git"))
-        self.assertFalse(validate_path_element_ntfs(".giT"))
-        self.assertFalse(validate_path_element_ntfs(".."))
-        self.assertFalse(validate_path_element_ntfs("git~1"))
+        self.assertTrue(validate_path_element_ntfs(b"bla"))
+        self.assertTrue(validate_path_element_ntfs(b".bla"))
+        self.assertFalse(validate_path_element_ntfs(b".git"))
+        self.assertFalse(validate_path_element_ntfs(b".giT"))
+        self.assertFalse(validate_path_element_ntfs(b".."))
+        self.assertFalse(validate_path_element_ntfs(b"git~1"))