Explorar o código

Add tests for commit.

Jelmer Vernooij %!s(int64=16) %!d(string=hai) anos
pai
achega
1db1601b49
Modificáronse 4 ficheiros con 42 adicións e 6 borrados
  1. 3 1
      dulwich/index.py
  2. 5 2
      dulwich/object_store.py
  3. 10 3
      dulwich/objects.py
  4. 24 0
      dulwich/tests/test_index.py

+ 3 - 1
dulwich/index.py

@@ -18,6 +18,7 @@
 
 """Parser for the git index file format."""
 
+import os
 import stat
 import struct
 
@@ -227,8 +228,9 @@ def commit_tree(object_store, blobs):
 
     for path in sorted(trees.keys(), reverse=True):
         tree = Tree()
-        for basename, (mode, sha) in trees[path]:
+        for basename, (mode, sha) in trees[path].iteritems():
             tree.add(mode, basename, sha)
+        object_store.add_object(tree)
         if path != "":
             # Add to object store
             parent_path, basename = os.path.split(path)

+ 5 - 2
dulwich/object_store.py

@@ -319,13 +319,16 @@ class MemoryObjectStore(BaseObjectStore):
         :param name: sha for the object.
         :return: tuple with object type and object contents.
         """
-        return self[sha].as_raw_string()
+        return self[name].as_raw_string()
+
+    def __getitem__(self, name):
+        return self._data[name]
 
     def add_object(self, obj):
         """Add a single object to this object store.
 
         """
-        self._dict[obj.id] = obj
+        self._data[obj.id] = obj
 
     def add_objects(self, objects):
         """Add a set of objects to this object store.

+ 10 - 3
dulwich/objects.py

@@ -231,11 +231,15 @@ class Blob(ShaFile):
     _needs_serialization = False
     _needs_parsing = False
 
-    @property
-    def data(self):
-        """The text contained within the blob object."""
+    def get_data(self):
         return self._text
 
+    def set_data(self, data):
+        self._text = data
+
+    data = property(get_data, set_data, 
+            "The text contained within the blob object.")
+
     @classmethod
     def from_file(cls, filename):
         blob = ShaFile.from_file(filename)
@@ -420,6 +424,9 @@ class Tree(ShaFile):
         self._needs_serialization = True
 
     def add(self, mode, name, hexsha):
+        assert type(mode) == int
+        assert type(name) == str
+        assert type(hexsha) == str
         self._ensure_parsed()
         self._entries[name] = mode, hexsha
         self._needs_serialization = True

+ 24 - 0
dulwich/tests/test_index.py

@@ -17,13 +17,21 @@
 # MA  02110-1301, USA.
 
 import os
+import stat
 from unittest import TestCase
 
 from dulwich.index import (
     Index,
+    commit_tree,
     read_index,
     write_index,
     )
+from dulwich.object_store import (
+    MemoryObjectStore,
+    )
+from dulwich.objects import (
+    Blob,
+    )
 
 class IndexTestCase(TestCase):
 
@@ -62,3 +70,19 @@ class SimpleIndexWriterTestCase(IndexTestCase):
         finally:
             x.close()
 
+
+class CommitTreeTests(TestCase):
+
+    def setUp(self):
+        super(CommitTreeTests, self).setUp()
+        self.store = MemoryObjectStore()
+
+    def test_single_blob(self):
+        blob = Blob()
+        blob.data = "foo"
+        self.store.add_object(blob)
+        blobs = [("bla", blob.id, stat.S_IFREG)]
+        rootid = commit_tree(self.store, blobs)
+        self.assertEquals(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
+        self.assertEquals(blob.id, self.store[rootid]["bla"][1])
+        self.assertEquals(set([rootid, blob.id]), set(self.store._data.keys()))