Skip to content

Commit 708b8dd

Browse files
committed
commit: fixed failing commit tests as the mocked git command would always return the same thing which does not work anymore - re-implemented it in a more dynamic manner, but in the end tests will have to be revised anyway
Added slots to Diff and Stats type respectively
1 parent ccde80b commit 708b8dd

File tree

6 files changed

+57
-68
lines changed

6 files changed

+57
-68
lines changed

lib/git/blob.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def blame(cls, repo, commit, file):
5757
blames = []
5858
info = None
5959

60-
for line in data.splitlines():
60+
for line in data.splitlines(False):
6161
parts = cls.re_whitespace.split(line, 1)
6262
firstpart = parts[0]
6363
if cls.re_hexsha_only.search(firstpart):

lib/git/commit.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ def list_from_string(cls, repo, text):
162162
Returns
163163
git.Commit[]
164164
"""
165-
lines = [l for l in text.splitlines() if l.strip('\r\n')]
166-
165+
lines =text.splitlines(False)
167166
commits = []
168167

169168
while lines:
@@ -173,18 +172,22 @@ def list_from_string(cls, repo, text):
173172
parents = []
174173
while lines and lines[0].startswith('parent'):
175174
parents.append(lines.pop(0).split()[-1])
175+
# END while there are parent lines
176176
author, authored_date = cls._actor(lines.pop(0))
177177
committer, committed_date = cls._actor(lines.pop(0))
178-
178+
179+
# free line
180+
lines.pop(0)
181+
179182
messages = []
180-
while lines and lines[0].startswith(' '):
183+
while lines and not lines[0].startswith('commit'):
181184
messages.append(lines.pop(0).strip())
182-
185+
# END while there are message lines
183186
message = '\n'.join(messages)
184187

185188
commits.append(Commit(repo, id=id, parents=parents, tree=tree, author=author, authored_date=authored_date,
186189
committer=committer, committed_date=committed_date, message=message))
187-
190+
# END while lines
188191
return commits
189192

190193
@classmethod

lib/git/diff.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ class Diff(object):
4545
\.\.(?P<b_blob_id>[0-9A-Fa-f]+)[ ]?(?P<b_mode>.+)?(?:\n|$))?
4646
""", re.VERBOSE | re.MULTILINE)
4747
re_is_null_hexsha = re.compile( r'^0{40}$' )
48+
__slots__ = ("a_blob", "b_blob", "a_mode", "b_mode", "new_file", "deleted_file",
49+
"rename_from", "rename_to", "renamed", "diff")
4850

4951
def __init__(self, repo, a_path, b_path, a_blob_id, b_blob_id, a_mode,
5052
b_mode, new_file, deleted_file, rename_from,
5153
rename_to, diff):
52-
self.repo = repo
53-
5454
if not a_blob_id or self.re_is_null_hexsha.search(a_blob_id):
5555
self.a_blob = None
5656
else:

lib/git/stats.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ class Stats(object):
3131
files = number of changed files as int
3232
3333
"""
34-
def __init__(self, repo, total, files):
35-
self.repo = repo
34+
__slots__ = ("total", "files")
35+
36+
def __init__(self, total, files):
3637
self.total = total
3738
self.files = files
3839

@@ -56,4 +57,4 @@ def list_from_string(cls, repo, text):
5657
hsh['files'][filename.strip()] = {'insertions': insertions,
5758
'deletions': deletions,
5859
'lines': insertions + deletions}
59-
return Stats(repo, hsh['total'], hsh['files'])
60+
return Stats(hsh['total'], hsh['files'])

test/git/test_commit.py

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -135,30 +135,21 @@ def test_diffs(self, git):
135135
'91169e1f5fa4de2eaea3f176461f5dc784796769',
136136
), {'full_index': True}))
137137

138-
@patch_object(Git, '_call_process')
139-
def test_diffs_on_initial_import(self, git):
140-
git.return_value = fixture('diff_i')
141-
142-
commit = Commit(self.repo, id='634396b2f541a9f2d58b00be1a07f0c358b999b3')
143-
diffs = commit.diffs
144-
145-
assert_equal(10, len(diffs))
146-
147-
assert_equal('History.txt', diffs[0].b_blob.path)
148-
assert_equal(None, diffs[0].a_blob)
149-
assert_equal('100644', diffs[0].b_blob.mode)
150-
assert_equal('81d2c27608b352814cbe979a6acd678d30219678', diffs[0].b_blob.id)
151-
assert_equal(True, diffs[0].new_file)
152-
assert_equal(False, diffs[0].deleted_file)
153-
assert_equal("--- /dev/null\n+++ b/History.txt\n@@ -0,0 +1,5 @@\n+== 1.0.0 / 2007-10-09\n+\n+* 1 major enhancement\n+ * Birthday!\n+", diffs[0].diff)
154-
155-
assert_equal('lib/grit.rb', diffs[5].b_blob.path)
156-
assert_equal(None, diffs[5].a_blob)
157-
assert_equal('32cec87d1e78946a827ddf6a8776be4d81dcf1d1', diffs[5].b_blob.id)
158-
assert_equal(True, diffs[5].new_file)
159-
160-
assert_true(git.called)
161-
assert_equal(git.call_args, (('show', '634396b2f541a9f2d58b00be1a07f0c358b999b3', '-M'), {'full_index': True, 'pretty': 'raw'}))
138+
def test_diffs_on_initial_import(self):
139+
commit = Commit(self.repo, '33ebe7acec14b25c5f84f35a664803fcab2f7781')
140+
141+
for diff in commit.diffs:
142+
assert isinstance(diff, Diff)
143+
assert isinstance(diff.a_blob, Blob) or isinstance(diff.b_blob, Blob)
144+
assert isinstance(diff.a_mode, int) and isinstance(diff.b_mode, int)
145+
assert diff.diff
146+
if diff.renamed:
147+
assert diff.rename_from and diff.rename_to and diff.rename_from != diff.rename_to
148+
if diff.a_blob is None:
149+
assert diff.new_file and isinstance(diff.new_file, bool)
150+
if diff.b_blob is None:
151+
assert diff.deleted_file and isinstance(diff.deleted_file, bool)
152+
# END for each diff in initial import commit
162153

163154
@patch_object(Git, '_call_process')
164155
def test_diffs_on_initial_import_with_empty_commit(self, git):
@@ -172,37 +163,36 @@ def test_diffs_on_initial_import_with_empty_commit(self, git):
172163
assert_true(git.called)
173164
assert_equal(git.call_args, (('show', '634396b2f541a9f2d58b00be1a07f0c358b999b3', '-M'), {'full_index': True, 'pretty': 'raw'}))
174165

175-
@patch_object(Git, '_call_process')
176-
def test_diffs_with_mode_only_change(self, git):
177-
git.return_value = fixture('diff_mode_only')
178-
179-
commit = Commit(self.repo, id='91169e1f5fa4de2eaea3f176461f5dc784796769')
166+
def test_diffs_with_mode_only_change(self):
167+
commit = Commit(self.repo, id='ccde80b7a3037a004a7807a6b79916ce2a1e9729')
180168
diffs = commit.diffs
181169

182170
# in case of mode-only changes, there is no blob
183-
assert_equal(23, len(diffs))
171+
assert_equal(1, len(diffs))
184172
assert_equal(None, diffs[0].a_blob)
185173
assert_equal(None, diffs[0].b_blob)
186174
assert_equal('100644', diffs[0].a_mode)
187175
assert_equal('100755', diffs[0].b_mode)
188176

189-
assert_true(git.called)
190-
assert_equal(git.call_args, (('show', '91169e1f5fa4de2eaea3f176461f5dc784796769', '-M'), {'full_index': True, 'pretty': 'raw'}))
191-
192-
@patch_object(Git, '_call_process')
193-
def test_stats(self, git):
194-
git.return_value = fixture('diff_tree_numstat_root')
195-
196-
commit = Commit(self.repo, id='634396b2f541a9f2d58b00be1a07f0c358b999b3')
177+
def test_stats(self):
178+
commit = Commit(self.repo, id='33ebe7acec14b25c5f84f35a664803fcab2f7781')
197179
stats = commit.stats
198-
199-
keys = stats.files.keys()
200-
keys.sort()
201-
assert_equal(["a.txt", "b.txt"], keys)
202-
203-
assert_true(git.called)
204-
assert_equal(git.call_args, (('diff_tree', '634396b2f541a9f2d58b00be1a07f0c358b999b3', '--'), {'numstat': True, 'root': True }))
205-
180+
181+
def check_entries(d):
182+
assert isinstance(d, dict)
183+
for key in ("insertions", "deletions", "lines"):
184+
assert key in d
185+
# END assertion helper
186+
assert stats.files
187+
assert stats.total
188+
189+
check_entries(stats.total)
190+
assert "files" in stats.total
191+
192+
for filepath, d in stats.files.items():
193+
check_entries(d)
194+
# END for each stated file
195+
206196
@patch_object(Git, '_call_process')
207197
def test_rev_list_bisect_all(self, git):
208198
"""

test/git/test_repo.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,11 @@ def test_heads_should_return_array_of_head_objects(self):
3333
for head in self.repo.heads:
3434
assert_equal(Head, head.__class__)
3535

36-
@patch_object(Git, '_call_process')
37-
def test_heads_should_populate_head_data(self, git):
38-
git.return_value = fixture('for_each_ref')
39-
40-
head = self.repo.heads[0]
41-
assert_equal('master', head.name)
42-
assert_equal('634396b2f541a9f2d58b00be1a07f0c358b999b3', head.commit.id)
43-
44-
assert_true(git.called)
45-
assert_equal(git.call_args, (('for_each_ref', 'refs/heads'), {'sort': 'committerdate', 'format': '%(refname)%00%(objectname)'}))
36+
def test_heads_should_populate_head_data(self):
37+
for head in self.repo.heads:
38+
assert head.name
39+
assert isinstance(head.commit,Commit)
40+
# END for each head
4641

4742
@patch_object(Git, '_call_process')
4843
def test_commits(self, git):

0 commit comments

Comments
 (0)