aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2009-10-18 14:25:14 +0200
committerSebastian Thiel <byronimo@gmail.com>2009-10-18 14:25:14 +0200
commit225999e9442c746333a8baa17a6dbf7341c135ca (patch)
tree82e4bdf8a59fae869bae41aa6b9b048fee2d3e09 /test
parent919164df96d9f956c8be712f33a9a037b097745b (diff)
parent9acc7806d6bdb306a929c460437d3d03e5e48dcd (diff)
downloadGitPython-225999e9442c746333a8baa17a6dbf7341c135ca.tar.gz
GitPython-225999e9442c746333a8baa17a6dbf7341c135ca.zip
Merge branch 'diffing' into improvements
* diffing: DiffIndex implemented including test diff: implemented raw diff parsing which appears to be able to handle possible input types, DiffIndex still requires implementation though resolved cyclic inclusion issue by moving the Diffable interface into the diff module, which probably is the right thing to do anyway repo: fixed untracked files function which used git-commit before, it can open vim to get a message though which makes the program appear to freeze - using git-status now implemented diff tests, but will have to move the diff module as it needs to create objects, whose import would create a dependency cycle Removed a few diff-related test cases that fail now as the respective method is missing - these tests have to be redone in test-diff module accordingly added Diffable interface to objects.base, its used by Commit and Tree objects. Fixed object bug that would cause object ids not to be resolved to sha's as this was assumed - now there is a test for it as well
Diffstat (limited to 'test')
-rw-r--r--test/git/test_base.py3
-rw-r--r--test/git/test_commit.py150
-rw-r--r--test/git/test_diff.py58
-rw-r--r--test/git/test_repo.py39
-rw-r--r--test/testlib/helper.py32
5 files changed, 87 insertions, 195 deletions
diff --git a/test/git/test_base.py b/test/git/test_base.py
index 04222e2e..71576048 100644
--- a/test/git/test_base.py
+++ b/test/git/test_base.py
@@ -90,3 +90,6 @@ class TestBase(object):
assert_raises( ValueError, get_object_type_by_name, "doesntexist" )
+ def test_object_resolution(self):
+ # objects must be resolved to shas so they compare equal
+ assert self.repo.head.object == self.repo.active_branch.object
diff --git a/test/git/test_commit.py b/test/git/test_commit.py
index 4e698ed0..c8bca564 100644
--- a/test/git/test_commit.py
+++ b/test/git/test_commit.py
@@ -20,156 +20,6 @@ class TestCommit(object):
assert_equal("byronimo@gmail.com", commit.author.email)
- @patch_object(Git, '_call_process')
- def test_diff(self, git):
- git.return_value = fixture('diff_p')
-
- diffs = Commit.diff(self.repo, 'master')
-
- assert_equal(15, len(diffs))
-
- diff = diffs[0]
- assert_equal('.gitignore', diff.a_blob.path)
- assert_equal('.gitignore', diff.b_blob.path)
- assert_equal('4ebc8aea50e0a67e000ba29a30809d0a7b9b2666', diff.a_blob.id)
- assert_equal('2dd02534615434d88c51307beb0f0092f21fd103', diff.b_blob.id)
-
- assert_mode_644(diff.b_blob.mode)
-
- assert_equal(False, diff.new_file)
- assert_equal(False, diff.deleted_file)
- assert_equal("--- a/.gitignore\n+++ b/.gitignore\n@@ -1 +1,2 @@\n coverage\n+pkg", diff.diff)
-
- diff = diffs[5]
- assert_equal('lib/grit/actor.rb', diff.b_blob.path)
- assert_equal(None, diff.a_blob)
- assert_equal('f733bce6b57c0e5e353206e692b0e3105c2527f4', diff.b_blob.id)
- assert_equal( None, diff.a_mode )
- assert_equal(True, diff.new_file)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('diff', '-M', 'master'), {'full_index': True}))
-
- @patch_object(Git, '_call_process')
- def test_diff_with_rename(self, git):
- git.return_value = fixture('diff_rename')
-
- diffs = Commit.diff(self.repo, 'rename')
-
- assert_equal(1, len(diffs))
-
- diff = diffs[0]
- assert_true(diff.renamed)
- assert_equal(diff.rename_from, 'AUTHORS')
- assert_equal(diff.rename_to, 'CONTRIBUTORS')
-
- assert_true(git.called)
- assert_equal(git.call_args, (('diff', '-M', 'rename'), {'full_index': True}))
-
- @patch_object(Git, '_call_process')
- def test_diff_with_two_commits(self, git):
- git.return_value = fixture('diff_2')
-
- diffs = Commit.diff(self.repo, '59ddc32', '13d27d5')
-
- assert_equal(3, len(diffs))
-
- assert_true(git.called)
- assert_equal(git.call_args, (('diff', '-M', '59ddc32', '13d27d5'), {'full_index': True}))
-
- @patch_object(Git, '_call_process')
- def test_diff_with_files(self, git):
- git.return_value = fixture('diff_f')
-
- diffs = Commit.diff(self.repo, '59ddc32', ['lib'])
-
- assert_equal(1, len(diffs))
- assert_equal('lib/grit/diff.rb', diffs[0].a_blob.path)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('diff', '-M', '59ddc32', '--', 'lib'), {'full_index': True}))
-
- @patch_object(Git, '_call_process')
- def test_diff_with_two_commits_and_files(self, git):
- git.return_value = fixture('diff_2f')
-
- diffs = Commit.diff(self.repo, '59ddc32', '13d27d5', ['lib'])
-
- assert_equal(1, len(diffs))
- assert_equal('lib/grit/commit.rb', diffs[0].a_blob.path)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('diff', '-M', '59ddc32', '13d27d5', '--', 'lib'), {'full_index': True}))
-
- @patch_object(Git, '_call_process')
- def test_diffs(self, git):
- git.return_value = fixture('diff_p')
-
- commit = Commit(self.repo, id='91169e1f5fa4de2eaea3f176461f5dc784796769', parents=['038af8c329ef7c1bae4568b98bd5c58510465493'])
- diffs = commit.diffs
-
- assert_equal(15, len(diffs))
-
- diff = diffs[0]
- assert_equal('.gitignore', diff.a_blob.path)
- assert_equal('.gitignore', diff.b_blob.path)
- assert_equal('4ebc8aea50e0a67e000ba29a30809d0a7b9b2666', diff.a_blob.id)
- assert_equal('2dd02534615434d88c51307beb0f0092f21fd103', diff.b_blob.id)
- assert_mode_644(diff.b_blob.mode)
- assert_equal(False, diff.new_file)
- assert_equal(False, diff.deleted_file)
- assert_equal("--- a/.gitignore\n+++ b/.gitignore\n@@ -1 +1,2 @@\n coverage\n+pkg", diff.diff)
-
- diff = diffs[5]
- assert_equal('lib/grit/actor.rb', diff.b_blob.path)
- assert_equal(None, diff.a_blob)
- assert_equal('f733bce6b57c0e5e353206e692b0e3105c2527f4', diff.b_blob.id)
- assert_equal(True, diff.new_file)
-
- assert_true(git.called)
- assert_equal(git.call_args, (('diff', '-M',
- '038af8c329ef7c1bae4568b98bd5c58510465493',
- '91169e1f5fa4de2eaea3f176461f5dc784796769',
- ), {'full_index': True}))
-
- def test_diffs_on_initial_import(self):
- commit = Commit(self.repo, '33ebe7acec14b25c5f84f35a664803fcab2f7781')
-
- for diff in commit.diffs:
- assert isinstance(diff, Diff)
- assert isinstance(diff.a_blob, Blob) or isinstance(diff.b_blob, Blob)
-
- if diff.a_mode is not None:
- assert isinstance(diff.a_mode, int)
- if diff.b_mode is not None:
- isinstance(diff.b_mode, int)
-
- assert diff.diff is not None # can be empty
-
- if diff.renamed:
- assert diff.rename_from and diff.rename_to and diff.rename_from != diff.rename_to
- if diff.a_blob is None:
- assert diff.new_file and isinstance(diff.new_file, bool)
- if diff.b_blob is None:
- assert diff.deleted_file and isinstance(diff.deleted_file, bool)
- # END for each diff in initial import commit
-
- def test_diffs_on_initial_import_without_parents(self):
- commit = Commit(self.repo, id='33ebe7acec14b25c5f84f35a664803fcab2f7781')
- diffs = commit.diffs
- assert diffs
-
- def test_diffs_with_mode_only_change(self):
- commit = Commit(self.repo, id='ccde80b7a3037a004a7807a6b79916ce2a1e9729')
- diffs = commit.diffs
-
- # in case of mode-only changes, there is no blob
- assert_equal(1, len(diffs))
- assert_equal(None, diffs[0].a_blob)
- assert_equal(None, diffs[0].b_blob)
- assert_mode_644(diffs[0].a_mode)
- assert_mode_755(diffs[0].b_mode)
-
def test_stats(self):
commit = Commit(self.repo, id='33ebe7acec14b25c5f84f35a664803fcab2f7781')
stats = commit.stats
diff --git a/test/git/test_diff.py b/test/git/test_diff.py
index b2339455..deae7cfc 100644
--- a/test/git/test_diff.py
+++ b/test/git/test_diff.py
@@ -7,19 +7,19 @@
from test.testlib import *
from git import *
-class TestDiff(object):
- def setup(self):
+class TestDiff(TestCase):
+ def setUp(self):
self.repo = Repo(GIT_REPO)
def test_list_from_string_new_mode(self):
- output = fixture('diff_new_mode')
- diffs = Diff._list_from_string(self.repo, output)
+ output = ListProcessAdapter(fixture('diff_new_mode'))
+ diffs = Diff._index_from_patch_format(self.repo, output.stdout)
assert_equal(1, len(diffs))
assert_equal(10, len(diffs[0].diff.splitlines()))
def test_diff_with_rename(self):
- output = fixture('diff_rename')
- diffs = Diff._list_from_string(self.repo, output)
+ output = ListProcessAdapter(fixture('diff_rename'))
+ diffs = Diff._index_from_patch_format(self.repo, output.stdout)
assert_equal(1, len(diffs))
@@ -28,3 +28,49 @@ class TestDiff(object):
assert_equal(diff.rename_from, 'AUTHORS')
assert_equal(diff.rename_to, 'CONTRIBUTORS')
+ def test_diff_patch_format(self):
+ # test all of the 'old' format diffs for completness - it should at least
+ # be able to deal with it
+ fixtures = ("diff_2", "diff_2f", "diff_f", "diff_i", "diff_mode_only",
+ "diff_new_mode", "diff_numstat", "diff_p", "diff_rename",
+ "diff_tree_numstat_root" )
+
+ for fixture_name in fixtures:
+ diff_proc = ListProcessAdapter(fixture(fixture_name))
+ diffs = Diff._index_from_patch_format(self.repo, diff_proc.stdout)
+ # END for each fixture
+
+ def test_diff_interface(self):
+ # test a few variations of the main diff routine
+ assertion_map = dict()
+ for i, commit in enumerate(self.repo.iter_commits('0.1.6', max_count=10)):
+ diff_item = commit
+ if i%2 == 0:
+ diff_item = commit.tree
+ # END use tree every second item
+
+ for other in (None, commit.Index, commit.parents[0]):
+ for paths in (None, "CHANGES", ("CHANGES", "lib")):
+ for create_patch in range(2):
+ diff_index = diff_item.diff(other, paths, create_patch)
+ assert isinstance(diff_index, DiffIndex)
+
+ if diff_index:
+ for ct in DiffIndex.change_type:
+ key = 'ct_%s'%ct
+ assertion_map.setdefault(key, 0)
+ assertion_map[key] = assertion_map[key]+len(list(diff_index.iter_change_type(ct)))
+ # END for each changetype
+ # END diff index checking
+ # END for each patch option
+ # END for each path option
+ # END for each other side
+ # END for each commit
+
+ # assert we could always find at least one instance of the members we
+ # can iterate in the diff index - if not this indicates its not working correctly
+ # or our test does not span the whole range of possibilities
+ for key,value in assertion_map.items():
+ assert value, "Did not find diff for %s" % key
+ # END for each iteration type
+
diff --git a/test/git/test_repo.py b/test/git/test_repo.py
index 250974a5..87332067 100644
--- a/test/git/test_repo.py
+++ b/test/git/test_repo.py
@@ -43,7 +43,7 @@ class TestRepo(object):
git.return_value = ListProcessAdapter(fixture('rev_list'))
commits = list( self.repo.iter_commits('master', max_count=10) )
-
+
c = commits[0]
assert_equal('4c8124ffcf4039d292442eeccabdeca5af5c5017', c.id)
assert_equal(["634396b2f541a9f2d58b00be1a07f0c358b999b3"], [p.id for p in c.parents])
@@ -116,40 +116,6 @@ class TestRepo(object):
{ 'template': '/awesome'}))
assert_true(repo.called)
- @patch_object(Git, '_call_process')
- def test_diff(self, git):
- self.repo.diff('master^', 'master')
-
- assert_true(git.called)
- assert_equal(git.call_args, (('diff', 'master^', 'master', '--'), {}))
-
- self.repo.diff('master^', 'master', 'foo/bar')
-
- assert_true(git.called)
- assert_equal(git.call_args, (('diff', 'master^', 'master', '--', 'foo/bar'), {}))
-
- self.repo.diff('master^', 'master', 'foo/bar', 'foo/baz')
-
- assert_true(git.called)
- assert_equal(git.call_args, (('diff', 'master^', 'master', '--', 'foo/bar', 'foo/baz'), {}))
-
- @patch_object(Git, '_call_process')
- def test_diff_with_parents(self, git):
- git.return_value = fixture('diff_p')
-
- diffs = self.repo.commit_diff('master')
- assert_equal(15, len(diffs))
- assert_true(git.called)
-
- def test_archive(self):
- args = ( tuple(), (self.repo.heads[-1],),(None,"hello") )
- for arg_list in args:
- ftmp = os.tmpfile()
- self.repo.archive(ftmp, *arg_list)
- ftmp.seek(0,2)
- assert ftmp.tell()
- # END for each arg-list
-
@patch('git.utils.touch')
def test_enable_daemon_serve(self, touch):
self.repo.daemon_serve = False
@@ -199,6 +165,9 @@ class TestRepo(object):
assert_equal(self.repo.active_branch.name, 'major-refactoring')
assert_equal(git.call_args, (('symbolic_ref', 'HEAD'), {}))
+ def test_head(self):
+ assert self.repo.head.object == self.repo.active_branch.object
+
@patch_object(Git, '_call_process')
def test_should_display_blame_information(self, git):
git.return_value = fixture('blame')
diff --git a/test/testlib/helper.py b/test/testlib/helper.py
index b66d3eaa..c4c0f2ba 100644
--- a/test/testlib/helper.py
+++ b/test/testlib/helper.py
@@ -23,8 +23,32 @@ class ListProcessAdapter(object):
"""Allows to use lists as Process object as returned by SubProcess.Popen.
Its tailored to work with the test system only"""
+ class Stream(object):
+ """Simple stream emulater meant to work only with tests"""
+ def __init__(self, data):
+ self.data = data
+ self.cur_iter = None
+
+ def __iter__(self):
+ dat = self.data
+ if isinstance(dat, basestring):
+ dat = dat.splitlines()
+ if self.cur_iter is None:
+ self.cur_iter = iter(dat)
+ return self.cur_iter
+
+ def read(self):
+ dat = self.data
+ if isinstance(dat, (tuple,list)):
+ dat = "\n".join(dat)
+ return dat
+
+ def next(self):
+ if self.cur_iter is None:
+ self.cur_iter = iter(self)
+ return self.cur_iter.next()
+
+ # END stream
+
def __init__(self, input_list_or_string):
- l = input_list_or_string
- if isinstance(l,basestring):
- l = l.splitlines()
- self.stdout = iter(l)
+ self.stdout = self.Stream(input_list_or_string)