← Back to team overview

dulwich-users team mailing list archive

[PATCH 03/34] tests/utils: Build graphs of commits with arbitrary attributes.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: I6a73e3f08ba8be1887b0a19cf9e1c4d795a1340d
---
 dulwich/tests/test_utils.py |   14 ++++++++++++++
 dulwich/tests/utils.py      |   25 +++++++++++++++++--------
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/dulwich/tests/test_utils.py b/dulwich/tests/test_utils.py
index 591ec05..c90287c 100644
--- a/dulwich/tests/test_utils.py
+++ b/dulwich/tests/test_utils.py
@@ -68,3 +68,17 @@ class BuildCommitGraphTest(TestCase):
                                            2: [('a', a2, 0100644)]})
         self.assertEqual((0100644, a1.id), self.store[c1.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)
diff --git a/dulwich/tests/utils.py b/dulwich/tests/utils.py
index c057dac..806af54 100644
--- a/dulwich/tests/utils.py
+++ b/dulwich/tests/utils.py
@@ -238,7 +238,7 @@ def build_pack(f, objects_spec, store=None):
     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.
 
     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
         (path, blob) entries; if mode is omitted, it defaults to the normal file
         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.
     :raise ValueError: If an undefined commit identifier is listed as a parent.
     """
     if trees is None:
         trees = {}
+    if attrs is None:
+        attrs = {}
     commit_time = 0
     nums = {}
     commits = []
@@ -289,13 +293,18 @@ def build_commit_graph(object_store, commit_spec, trees=None):
             object_store.add_object(blob)
         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
         object_store.add_object(commit_obj)
         commits.append(commit_obj)
-- 
1.7.3.1



References