Bläddra i källkod

Fix spacing issue in timezone offsets, add some simple tests for commit and tree serialization.

Jelmer Vernooij 16 år sedan
förälder
incheckning
1e39a3709f
2 ändrade filer med 57 tillägg och 2 borttagningar
  1. 8 2
      dulwich/objects.py
  2. 49 0
      dulwich/tests/test_objects.py

+ 8 - 2
dulwich/objects.py

@@ -463,7 +463,7 @@ def parse_timezone(text):
 def format_timezone(offset):
     if offset % 60 != 0:
         raise ValueError("Unable to handle non-minute offset.")
-    return ' %+03d%02d' % (offset / 3600, (offset / 60) % 60)
+    return '%+03d%02d' % (offset / 3600, (offset / 60) % 60)
 
 
 class Commit(ShaFile):
@@ -583,7 +583,13 @@ class Commit(ShaFile):
         self._ensure_parsed()
         return self._parents
 
-    parents = property(get_parents)
+    def set_parents(self, value):
+        """Return a list of parents of this commit."""
+        self._ensure_parsed()
+        self._needs_serialization = True
+        self._parents = value
+
+    parents = property(get_parents, set_parents)
 
     author = serializable_property("author", 
         "The name of the author of the commit")

+ 49 - 0
dulwich/tests/test_objects.py

@@ -25,6 +25,7 @@ from dulwich.objects import (
     Tree,
     Commit,
     Tag,
+    hex_to_sha,
     )
 
 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
@@ -138,3 +139,51 @@ class BlobReadTests(unittest.TestCase):
         self.assertEqual(c.author_timezone, 0)
         self.assertEqual(c.message, 'Merge ../b\n')
   
+
+
+class CommitSerializationTests(unittest.TestCase):
+
+    def make_base(self):
+        c = Commit()
+        c.tree = 'd80c186a03f423a81b39df39dc87fd269736ca86'
+        c.parents = ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd', '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6']
+        c.author = 'James Westby <jw+debian@jameswestby.net>'
+        c.committer = 'James Westby <jw+debian@jameswestby.net>'
+        c.commit_time = 1174773719
+        c.author_time = 1174773719
+        c.commit_timezone = 0
+        c.author_timezone = 0
+        c.message =  'Merge ../b\n'
+        return c
+
+    def test_simple(self):
+        c = self.make_base()
+        self.assertEquals(c.id, '5dac377bdded4c9aeb8dff595f0faeebcc8498cc')
+        self.assertEquals(
+                'tree d80c186a03f423a81b39df39dc87fd269736ca86\n'
+                'parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n'
+                'parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n'
+                'author James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
+                'committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
+                '\n'
+                'Merge ../b\n', c.as_raw_string())
+
+    def test_timezone(self):
+        c = self.make_base()
+        c.commit_timezone = 5 * 60
+        self.assertTrue(" +0005\n" in c.as_raw_string())
+
+    def test_neg_timezone(self):
+        c = self.make_base()
+        c.commit_timezone = -1 * 3600
+        self.assertTrue(" -0100\n" in c.as_raw_string())
+
+
+class TreeSerializationTests(unittest.TestCase):
+
+    def test_simple(self):
+        myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86"
+        x = Tree()
+        x["myname"] = (0100755, myhexsha)
+        self.assertEquals('100755 myname\0' + hex_to_sha(myhexsha),
+                x.as_raw_string())