Bläddra i källkod

tests/utils: Build graphs of commits with arbitrary attributes.

Change-Id: I6a73e3f08ba8be1887b0a19cf9e1c4d795a1340d
Dave Borowitz 13 år sedan
förälder
incheckning
c567629a82
2 ändrade filer med 31 tillägg och 8 borttagningar
  1. 14 0
      dulwich/tests/test_utils.py
  2. 17 8
      dulwich/tests/utils.py

+ 14 - 0
dulwich/tests/test_utils.py

@@ -68,3 +68,17 @@ class BuildCommitGraphTest(TestCase):
                                            2: [('a', a2, 0100644)]})
                                            2: [('a', a2, 0100644)]})
         self.assertEqual((0100644, a1.id), self.store[c1.tree]['a'])
         self.assertEqual((0100644, a1.id), self.store[c1.tree]['a'])
         self.assertEqual((0100644, a2.id), self.store[c2.tree]['a'])
         self.assertEqual((0100644, a2.id), self.store[c2.tree]['a'])
+
+    def test_attrs(self):
+        c1, c2 = build_commit_graph(self.store, [[1], [2, 1]],
+                                    attrs={1: {'message': 'Hooray!'}})
+        self.assertEqual('Hooray!', c1.message)
+        self.assertEqual('Commit 2', c2.message)
+
+    def test_commit_time(self):
+        c1, c2, c3 = build_commit_graph(self.store, [[1], [2, 1], [3, 2]],
+                                        attrs={1: {'commit_time': 124},
+                                               2: {'commit_time': 123}})
+        self.assertEqual(124, c1.commit_time)
+        self.assertEqual(123, c2.commit_time)
+        self.assertTrue(c2.commit_time < c1.commit_time < c3.commit_time)

+ 17 - 8
dulwich/tests/utils.py

@@ -238,7 +238,7 @@ def build_pack(f, objects_spec, store=None):
     return expected
     return expected
 
 
 
 
-def build_commit_graph(object_store, commit_spec, trees=None):
+def build_commit_graph(object_store, commit_spec, trees=None, attrs=None):
     """Build a commit graph from a concise specification.
     """Build a commit graph from a concise specification.
 
 
     Sample usage:
     Sample usage:
@@ -262,11 +262,15 @@ def build_commit_graph(object_store, commit_spec, trees=None):
         trees for commits. The tree spec is an iterable of (path, blob, mode) or
         trees for commits. The tree spec is an iterable of (path, blob, mode) or
         (path, blob) entries; if mode is omitted, it defaults to the normal file
         (path, blob) entries; if mode is omitted, it defaults to the normal file
         mode (0100644).
         mode (0100644).
+    :param attrs: A dict of commit number -> (dict of attribute -> value) for
+        assigning additional values to the commits.
     :return: The list of commit objects created.
     :return: The list of commit objects created.
     :raise ValueError: If an undefined commit identifier is listed as a parent.
     :raise ValueError: If an undefined commit identifier is listed as a parent.
     """
     """
     if trees is None:
     if trees is None:
         trees = {}
         trees = {}
+    if attrs is None:
+        attrs = {}
     commit_time = 0
     commit_time = 0
     nums = {}
     nums = {}
     commits = []
     commits = []
@@ -289,13 +293,18 @@ def build_commit_graph(object_store, commit_spec, trees=None):
             object_store.add_object(blob)
             object_store.add_object(blob)
         tree_id = commit_tree(object_store, blobs)
         tree_id = commit_tree(object_store, blobs)
 
 
-        commit_obj = make_commit(
-          message=('Commit %i' % commit_num),
-          parents=parent_ids,
-          tree=tree_id,
-          commit_time=commit_time)
-
-        commit_time += 1
+        commit_attrs = {
+            'message': 'Commit %i' % commit_num,
+            'parents': parent_ids,
+            'tree': tree_id,
+            'commit_time': commit_time,
+            }
+        commit_attrs.update(attrs.get(commit_num, {}))
+        commit_obj = make_commit(**commit_attrs)
+
+        # By default, increment the time by a lot. Out-of-order commits should
+        # be closer together than this because their main cause is clock skew.
+        commit_time = commit_attrs['commit_time'] + 100
         nums[commit_num] = commit_obj.id
         nums[commit_num] = commit_obj.id
         object_store.add_object(commit_obj)
         object_store.add_object(commit_obj)
         commits.append(commit_obj)
         commits.append(commit_obj)