summaryrefslogtreecommitdiff
path: root/test/git
diff options
context:
space:
mode:
Diffstat (limited to 'test/git')
-rw-r--r--test/git/test_base.py23
-rw-r--r--test/git/test_blob.py49
-rw-r--r--test/git/test_commit.py29
-rw-r--r--test/git/test_git.py35
-rw-r--r--test/git/test_performance.py38
-rw-r--r--test/git/test_repo.py48
-rw-r--r--test/git/test_tree.py159
7 files changed, 135 insertions, 246 deletions
diff --git a/test/git/test_base.py b/test/git/test_base.py
index a153eb83..402cdba3 100644
--- a/test/git/test_base.py
+++ b/test/git/test_base.py
@@ -10,7 +10,7 @@ from git import *
import git.objects.base as base
import git.refs as refs
from itertools import chain
-from git.objects.util import get_object_type_by_name
+from git.objects.utils import get_object_type_by_name
class TestBase(object):
@@ -24,14 +24,14 @@ class TestBase(object):
def test_base_object(self):
# test interface of base object classes
- fcreators = (self.repo.blob, self.repo.tree, self.repo.commit, lambda id: TagObject(self.repo,id) )
- assert len(fcreators) == len(self.type_tuples)
+ types = (Blob, Tree, Commit, TagObject)
+ assert len(types) == len(self.type_tuples)
s = set()
num_objs = 0
num_index_objs = 0
- for fcreator, (typename, hexsha) in zip(fcreators, self.type_tuples):
- item = fcreator(hexsha)
+ for obj_type, (typename, hexsha) in zip(types, self.type_tuples):
+ item = obj_type(self.repo,hexsha)
num_objs += 1
assert item.id == hexsha
assert item.type == typename
@@ -53,6 +53,7 @@ class TestBase(object):
# each has a unique sha
assert len(s) == num_objs
+ assert len(s|s) == num_objs
assert num_index_objs == 2
@@ -70,6 +71,18 @@ class TestBase(object):
s.add(ref)
# END for each ref
assert len(s) == ref_count
+ assert len(s|s) == ref_count
+
+ def test_heads(self):
+ # see how it dynmically updates its object
+ for head in self.repo.heads:
+ head.name
+ head.path
+ prev_object = head.object
+ cur_object = head.object
+ assert prev_object == cur_object # represent the same git object
+ assert prev_object is not cur_object # but are different instances
+ # END for each head
def test_get_object_type_by_name(self):
for tname in base.Object.TYPES:
diff --git a/test/git/test_blob.py b/test/git/test_blob.py
index ebb53d0c..266f3a23 100644
--- a/test/git/test_blob.py
+++ b/test/git/test_blob.py
@@ -12,51 +12,14 @@ class TestBlob(object):
def setup(self):
self.repo = Repo(GIT_REPO)
- @patch_object(Git, '_call_process')
- def test_should_return_blob_contents(self, git):
- git.return_value = fixture('cat_file_blob')
- blob = Blob(self.repo, **{'id': 'abc'})
- assert_equal("Hello world", blob.data)
- assert_true(git.called)
- assert_equal(git.call_args, (('cat_file', 'abc'), {'p': True, 'with_raw_output': True}))
-
- @patch_object(Git, '_call_process')
- def test_should_return_blob_contents_with_newline(self, git):
- git.return_value = fixture('cat_file_blob_nl')
- blob = Blob(self.repo, **{'id': 'abc'})
- assert_equal("Hello world\n", blob.data)
- assert_true(git.called)
- assert_equal(git.call_args, (('cat_file', 'abc'), {'p': True, 'with_raw_output': True}))
-
- @patch_object(Git, '_call_process')
- def test_should_cache_data(self, git):
- git.return_value = fixture('cat_file_blob')
- bid = '787b92b63f629398f3d2ceb20f7f0c2578259e84'
+ def test_should_cache_data(self):
+ bid = 'a802c139d4767c89dcad79d836d05f7004d39aac'
blob = Blob(self.repo, bid)
blob.data
- blob.data
- assert_true(git.called)
- assert_equal(git.call_count, 1)
- assert_equal(git.call_args, (('cat_file', bid), {'p': True, 'with_raw_output': True}))
-
- @patch_object(Git, '_call_process')
- def test_should_return_file_size(self, git):
- git.return_value = fixture('cat_file_blob_size')
- blob = Blob(self.repo, **{'id': 'abc'})
- assert_equal(11, blob.size)
- assert_true(git.called)
- assert_equal(git.call_args, (('cat_file', 'abc'), {'s': True}))
-
- @patch_object(Git, '_call_process')
- def test_should_cache_file_size(self, git):
- git.return_value = fixture('cat_file_blob_size')
- blob = Blob(self.repo, **{'id': 'abc'})
- assert_equal(11, blob.size)
- assert_equal(11, blob.size)
- assert_true(git.called)
- assert_equal(git.call_count, 1)
- assert_equal(git.call_args, (('cat_file', 'abc'), {'s': True}))
-
+ assert blob.data
+ blob.size
+ blob.size
+
def test_mime_type_should_return_mime_type_for_known_types(self):
blob = Blob(self.repo, **{'id': 'abc', 'path': 'foo.png'})
assert_equal("image/png", blob.mime_type)
diff --git a/test/git/test_commit.py b/test/git/test_commit.py
index fa49821d..a95fb675 100644
--- a/test/git/test_commit.py
+++ b/test/git/test_commit.py
@@ -11,18 +11,13 @@ class TestCommit(object):
def setup(self):
self.repo = Repo(GIT_REPO)
- @patch_object(Git, '_call_process')
- def test_bake(self, git):
- git.return_value = fixture('rev_list_single')
+ def test_bake(self):
- commit = Commit(self.repo, **{'id': '4c8124ffcf4039d292442eeccabdeca5af5c5017'})
+ commit = Commit(self.repo, **{'id': '2454ae89983a4496a445ce347d7a41c0bb0ea7ae'})
commit.author # bake
- assert_equal("Tom Preston-Werner", commit.author.name)
- assert_equal("tom@mojombo.com", commit.author.email)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('rev_list', '4c8124ffcf4039d292442eeccabdeca5af5c5017', '--', ''), {'pretty': 'raw', 'max_count': 1}))
+ assert_equal("Sebastian Thiel", commit.author.name)
+ assert_equal("byronimo@gmail.com", commit.author.email)
@patch_object(Git, '_call_process')
@@ -159,17 +154,10 @@ class TestCommit(object):
assert diff.deleted_file and isinstance(diff.deleted_file, bool)
# END for each diff in initial import commit
- @patch_object(Git, '_call_process')
- def test_diffs_on_initial_import_with_empty_commit(self, git):
- git.return_value = fixture('show_empty_commit')
-
- commit = Commit(self.repo, id='634396b2f541a9f2d58b00be1a07f0c358b999b3')
+ def test_diffs_on_initial_import_without_parents(self):
+ commit = Commit(self.repo, id='33ebe7acec14b25c5f84f35a664803fcab2f7781')
diffs = commit.diffs
-
- assert_equal([], diffs)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('show', '634396b2f541a9f2d58b00be1a07f0c358b999b3', '-M'), {'full_index': True, 'pretty': 'raw'}))
+ assert diffs
def test_diffs_with_mode_only_change(self):
commit = Commit(self.repo, id='ccde80b7a3037a004a7807a6b79916ce2a1e9729')
@@ -216,7 +204,7 @@ class TestCommit(object):
bisect_all=True)
assert_true(git.called)
- commits = Commit._list_from_string(self.repo, revs)
+ commits = Commit._iter_from_process_or_stream(self.repo, ListProcessAdapter(revs))
expected_ids = (
'cf37099ea8d1d8c7fbf9b6d12d7ec0249d3acb8b',
'33ebe7acec14b25c5f84f35a664803fcab2f7781',
@@ -241,3 +229,4 @@ class TestCommit(object):
commit3 = Commit(self.repo, id='zyx')
assert_equal(commit1, commit2)
assert_not_equal(commit2, commit3)
+
diff --git a/test/git/test_git.py b/test/git/test_git.py
index c9f399cc..1f44aebc 100644
--- a/test/git/test_git.py
+++ b/test/git/test_git.py
@@ -10,8 +10,7 @@ from git import Git, GitCommandError
class TestGit(object):
def setup(self):
- base = os.path.join(os.path.dirname(__file__), "../..")
- self.git = Git(base)
+ self.git = Git(GIT_REPO)
@patch_object(Git, 'execute')
def test_call_process_calls_execute(self, git):
@@ -56,3 +55,35 @@ class TestGit(object):
# this_should_not_be_ignored=False implies it *should* be ignored
output = self.git.version(pass_this_kwarg=False)
assert_true("pass_this_kwarg" not in git.call_args[1])
+
+ def test_persistent_cat_file_command(self):
+ # read header only
+ import subprocess as sp
+ hexsha = "b2339455342180c7cc1e9bba3e9f181f7baa5167"
+ g = self.git.cat_file(batch_check=True, istream=sp.PIPE,as_process=True)
+ g.stdin.write("b2339455342180c7cc1e9bba3e9f181f7baa5167\n")
+ g.stdin.flush()
+ obj_info = g.stdout.readline()
+
+ # read header + data
+ g = self.git.cat_file(batch=True, istream=sp.PIPE,as_process=True)
+ g.stdin.write("b2339455342180c7cc1e9bba3e9f181f7baa5167\n")
+ g.stdin.flush()
+ obj_info_two = g.stdout.readline()
+ assert obj_info == obj_info_two
+
+ # read data - have to read it in one large chunk
+ size = int(obj_info.split()[2])
+ data = g.stdout.read(size)
+ terminating_newline = g.stdout.read(1)
+
+ # now we should be able to read a new object
+ g.stdin.write("b2339455342180c7cc1e9bba3e9f181f7baa5167\n")
+ g.stdin.flush()
+ assert g.stdout.readline() == obj_info
+
+
+ # same can be achived using the respective command functions
+ hexsha, typename, size = self.git.get_object_header(hexsha)
+ hexsha, typename_two, size_two, data = self.git.get_object_data(hexsha)
+ assert typename == typename_two and size == size_two
diff --git a/test/git/test_performance.py b/test/git/test_performance.py
new file mode 100644
index 00000000..96f13a2e
--- /dev/null
+++ b/test/git/test_performance.py
@@ -0,0 +1,38 @@
+# test_performance.py
+# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
+#
+# This module is part of GitPython and is released under
+# the BSD License: http://www.opensource.org/licenses/bsd-license.php
+
+from test.testlib import *
+from git import *
+from time import time
+
+class TestPerformance(object):
+ def setup(self):
+ self.repo = Repo(GIT_REPO)
+
+ def test_iteration(self):
+ num_objs = 0
+ num_commits = 0
+
+ # find the first commit containing the given path - always do a full
+ # iteration ( restricted to the path in question ), but in fact it should
+ # return quite a lot of commits, we just take one and hence abort the operation
+
+ st = time()
+ for c in self.repo.commits():
+ num_commits += 1
+ c.author
+ c.authored_date
+ c.committer
+ c.committed_date
+ c.message
+ for obj in c.tree.traverse():
+ obj.size
+ num_objs += 1
+ # END for each object
+ # END for each commit
+ elapsed_time = time() - st
+ print "Traversed %i Trees and a total of %i unchached objects in %s [s] ( %f objs/s )" % (num_commits, num_objs, elapsed_time, num_objs/elapsed_time)
+
diff --git a/test/git/test_repo.py b/test/git/test_repo.py
index 3e2fb3dc..e998ac6d 100644
--- a/test/git/test_repo.py
+++ b/test/git/test_repo.py
@@ -41,7 +41,7 @@ class TestRepo(object):
@patch_object(Git, '_call_process')
def test_commits(self, git):
- git.return_value = fixture('rev_list')
+ git.return_value = ListProcessAdapter(fixture('rev_list'))
commits = self.repo.commits('master', max_count=10)
@@ -65,7 +65,6 @@ class TestRepo(object):
assert_equal("Merge branch 'site'", c.summary)
assert_true(git.called)
- assert_equal(git.call_args, (('rev_list', 'master', '--', ''), {'skip': 0, 'pretty': 'raw', 'max_count': 10}))
@patch_object(Git, '_call_process')
def test_commit_count(self, git):
@@ -78,37 +77,14 @@ class TestRepo(object):
@patch_object(Git, '_call_process')
def test_commit(self, git):
- git.return_value = fixture('rev_list_single')
+ git.return_value = ListProcessAdapter(fixture('rev_list_single'))
commit = self.repo.commit('4c8124ffcf4039d292442eeccabdeca5af5c5017')
assert_equal("4c8124ffcf4039d292442eeccabdeca5af5c5017", commit.id)
assert_true(git.called)
- assert_equal(git.call_args, (('rev_list', '4c8124ffcf4039d292442eeccabdeca5af5c5017', '--', ''), {'pretty': 'raw', 'max_count': 1}))
-
- @patch_object(Git, '_call_process')
- def test_tree(self, git):
- git.return_value = fixture('ls_tree_a')
-
- tree = self.repo.tree('master')
-
- assert_equal(4, len([c for c in tree.values() if isinstance(c, Blob)]))
- assert_equal(3, len([c for c in tree.values() if isinstance(c, Tree)]))
-
- assert_true(git.called)
- assert_equal(git.call_args, (('ls_tree', 'master'), {}))
-
- @patch_object(Git, '_call_process')
- def test_blob(self, git):
- git.return_value = fixture('cat_file_blob')
-
- blob = self.repo.blob("abc")
- assert_equal("Hello world", blob.data)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('cat_file', 'abc'), {'p': True, 'with_raw_output': True}))
-
+
@patch_object(Repo, '__init__')
@patch_object(Git, '_call_process')
def test_init_bare(self, git, repo):
@@ -218,22 +194,6 @@ class TestRepo(object):
path = os.path.join(os.path.abspath(GIT_REPO), '.git')
assert_equal('<git.Repo "%s">' % path, repr(self.repo))
- @patch_object(Git, '_call_process')
- def test_log(self, git):
- git.return_value = fixture('rev_list')
- assert_equal('4c8124ffcf4039d292442eeccabdeca5af5c5017', self.repo.log()[0].id)
- assert_equal('ab25fd8483882c3bda8a458ad2965d2248654335', self.repo.log()[-1].id)
- assert_true(git.called)
- assert_equal(git.call_count, 2)
- assert_equal(git.call_args, (('log', 'master', '--'), {'pretty': 'raw'}))
-
- @patch_object(Git, '_call_process')
- def test_log_with_path_and_options(self, git):
- git.return_value = fixture('rev_list')
- self.repo.log('master', 'file.rb', **{'max_count': 1})
- assert_true(git.called)
- assert_equal(git.call_args, (('log', 'master', '--', 'file.rb'), {'pretty': 'raw', 'max_count': 1}))
-
def test_is_dirty_with_bare_repository(self):
self.repo.bare = True
assert_false(self.repo.is_dirty)
@@ -255,7 +215,7 @@ class TestRepo(object):
@patch_object(Git, '_call_process')
def test_active_branch(self, git):
git.return_value = 'refs/heads/major-refactoring'
- assert_equal(self.repo.active_branch, 'major-refactoring')
+ assert_equal(self.repo.active_branch.name, 'major-refactoring')
assert_equal(git.call_args, (('symbolic_ref', 'HEAD'), {}))
@patch_object(Git, '_call_process')
diff --git a/test/git/test_tree.py b/test/git/test_tree.py
index cb8ebb04..dafb6f3f 100644
--- a/test/git/test_tree.py
+++ b/test/git/test_tree.py
@@ -7,143 +7,38 @@
from test.testlib import *
from git import *
-class TestTree(object):
- def setup(self):
- self.repo = Repo(GIT_REPO)
-
- @patch_object(Git, '_call_process')
- def test_contents_should_cache(self, git):
- git.return_value = fixture('ls_tree_a') + fixture('ls_tree_b')
+class TestTree(TestCase):
- tree = self.repo.tree('master')
-
- child = tree['grit']
- child.items()
- child.items()
-
- assert_true(git.called)
- assert_equal(2, git.call_count)
- assert_equal(git.call_args, (('ls_tree', '34868e6e7384cb5ee51c543a8187fdff2675b5a7'), {}))
-
- def test_content_from_string_tree_should_return_tree(self):
- text = fixture('ls_tree_a').splitlines()[-1]
- tree = Tree.content__from_string(None, text)
-
- assert_equal(Tree, tree.__class__)
- assert_equal("650fa3f0c17f1edb4ae53d8dcca4ac59d86e6c44", tree.id)
- assert_equal(0,tree.mode) # git tree objects always use this mode
- assert_equal("test", tree.path)
-
- def test_content_from_string_tree_should_return_blob(self):
- text = fixture('ls_tree_b').split("\n")[0]
-
- tree = Tree.content__from_string(None, text)
-
- assert_equal(Blob, tree.__class__)
- assert_equal("aa94e396335d2957ca92606f909e53e7beaf3fbb", tree.id)
- assert_mode_644(tree.mode)
- assert_equal("grit.rb", tree.path)
+ def setUp(self):
+ self.repo = Repo(GIT_REPO)
- def test_content_from_string_tree_should_return_commit(self):
- text = fixture('ls_tree_commit').split("\n")[1]
-
- tree = Tree.content__from_string(None, text)
- assert_none(tree)
- @raises(TypeError)
- def test_content_from_string_invalid_type_should_raise(self):
- Tree.content__from_string(None, "040000 bogus 650fa3f0c17f1edb4ae53d8dcca4ac59d86e6c44 test")
-
- @patch_object(Blob, 'size')
- @patch_object(Git, '_call_process')
- def test_slash(self, git, blob):
- git.return_value = fixture('ls_tree_a')
- blob.return_value = 1
-
- tree = self.repo.tree('master')
-
- assert_equal('aa06ba24b4e3f463b3c4a85469d0fb9e5b421cf8', (tree/'lib').id)
- assert_equal('8b1e02c0fb554eed2ce2ef737a68bb369d7527df', (tree/'README.txt').id)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('ls_tree', 'master'), {}))
-
- @patch_object(Blob, 'size')
- @patch_object(Git, '_call_process')
- def test_slash_with_zero_length_file(self, git, blob):
- git.return_value = fixture('ls_tree_a')
- blob.return_value = 0
-
- tree = self.repo.tree('master')
-
- assert_not_none(tree/'README.txt')
- assert_equal('8b1e02c0fb554eed2ce2ef737a68bb369d7527df', (tree/'README.txt').id)
- assert_true(git.called)
- assert_equal(git.call_args, (('ls_tree', 'master'), {}))
+ def test_traverse(self):
+ root = self.repo.tree()
+ num_recursive = 0
+ all_items = list()
+ for obj in root.traverse():
+ if "/" in obj.path:
+ num_recursive += 1
+
+ assert isinstance(obj, (Blob, Tree))
+ all_items.append(obj)
+ # END for each object
+ # limit recursion level to 0 - should be same as default iteration
+ assert all_items
+ assert 'CHANGES' in root
+ assert len(list(root)) == len(list(root.traverse(max_depth=0)))
+
+ # only choose trees
+ trees_only = lambda i: i.type == "tree"
+ trees = list(root.traverse(predicate = trees_only))
+ assert len(trees) == len(list( i for i in root.traverse() if trees_only(i) ))
+
+ # trees and blobs
+ assert len(set(trees)|set(root.trees)) == len(trees)
+ assert len(set(b for b in root if isinstance(b, Blob)) | set(root.blobs)) == len( root.blobs )
- @patch_object(Git, '_call_process')
- def test_slash_with_commits(self, git):
- git.return_value = fixture('ls_tree_commit')
-
- tree = self.repo.tree('master')
-
- assert_none(tree/'bar')
- assert_equal('2afb47bcedf21663580d5e6d2f406f08f3f65f19', (tree/'foo').id)
- assert_equal('f623ee576a09ca491c4a27e48c0dfe04be5f4a2e', (tree/'baz').id)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('ls_tree', 'master'), {}))
-
- @patch_object(Blob, 'size')
- @patch_object(Git, '_call_process')
- def test_dict(self, git, blob):
- git.return_value = fixture('ls_tree_a')
- blob.return_value = 1
-
- tree = self.repo.tree('master')
-
- assert_equal('aa06ba24b4e3f463b3c4a85469d0fb9e5b421cf8', tree['lib'].id)
- assert_equal('8b1e02c0fb554eed2ce2ef737a68bb369d7527df', tree['README.txt'].id)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('ls_tree', 'master'), {}))
-
- @patch_object(Blob, 'size')
- @patch_object(Git, '_call_process')
- def test_dict_with_zero_length_file(self, git, blob):
- git.return_value = fixture('ls_tree_a')
- blob.return_value = 0
-
- tree = self.repo.tree('master')
-
- assert_not_none(tree['README.txt'])
- assert_equal('8b1e02c0fb554eed2ce2ef737a68bb369d7527df', tree['README.txt'].id)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('ls_tree', 'master'), {}))
-
- @patch_object(Git, '_call_process')
- def test_dict_with_commits(self, git):
- git.return_value = fixture('ls_tree_commit')
-
- tree = self.repo.tree('master')
-
- assert_none(tree.get('bar'))
- assert_equal('2afb47bcedf21663580d5e6d2f406f08f3f65f19', tree['foo'].id)
- assert_equal('f623ee576a09ca491c4a27e48c0dfe04be5f4a2e', tree['baz'].id)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('ls_tree', 'master'), {}))
-
- @patch_object(Git, '_call_process')
- @raises(KeyError)
- def test_dict_with_non_existant_file(self, git):
- git.return_value = fixture('ls_tree_commit')
-
- tree = self.repo.tree('master')
- tree['bar']
-
def test_repr(self):
tree = Tree(self.repo, id='abc')
assert_equal('<git.Tree "abc">', repr(tree))