Skip to content

Commit c68459a

Browse files
committed
Added remaining tests for new base classes and removed some methods whose existance was doubtful or unsafe
1 parent b01824b commit c68459a

File tree

6 files changed

+53
-33
lines changed

6 files changed

+53
-33
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ General
1313
* Adjusted class hierarchy to generally allow comparison and hash for Objects and Refs
1414
* Improved Tag object which now is a Ref that may contain a tag object with additional
1515
Information
16+
* id_abbrev method has been removed as it could not assure the returned short SHA's
17+
where unique
18+
* removed basename method from Objects with path's as it replicated features of os.path
1619

1720
Diff
1821
----

lib/git/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from git.head import Head
1919
from git.repo import Repo
2020
from git.stats import Stats
21-
from git.tag import Tag
21+
from git.tag import Tag,TagRef,TagObject
2222
from git.tree import Tree
2323
from git.utils import dashify
2424
from git.utils import touch

lib/git/base.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ def __repr__(self):
110110
"""
111111
return '<git.%s "%s">' % (self.__class__.__name__, self.id)
112112

113-
@property
114-
def id_abbrev(self):
115-
"""
116-
Returns
117-
First 7 bytes of the commit's sha id as an abbreviation of the full string.
118-
"""
119-
return self.id[0:7]
120-
121113
@classmethod
122114
def get_type_by_name(cls, object_type_name):
123115
"""
@@ -169,12 +161,15 @@ def __init__(self, repo, id, mode=None, path=None):
169161
``path`` : str
170162
is the path to the file in the file system, relative to the git repository root, i.e.
171163
file.ext or folder/other.ext
164+
165+
NOTE
166+
Path may not be set of the index object has been created directly as it cannot
167+
be retrieved without knowing the parent tree.
172168
"""
173169
super(IndexObject, self).__init__(repo, id)
170+
self._set_self_from_args_(locals())
174171
if isinstance(mode, basestring):
175-
mode = self._mode_str_to_int(mode)
176-
self.mode = mode
177-
self.path = path
172+
self.mode = self._mode_str_to_int(mode)
178173

179174
@classmethod
180175
def _mode_str_to_int( cls, modestr ):
@@ -191,14 +186,6 @@ def _mode_str_to_int( cls, modestr ):
191186
mode += int(char) << iteration*3
192187
# END for each char
193188
return mode
194-
195-
@property
196-
def basename(self):
197-
"""
198-
Returns
199-
The basename of the IndexObject's file path
200-
"""
201-
return os.path.basename(self.path)
202189

203190

204191
class Ref(object):
@@ -222,7 +209,7 @@ def __init__(self, path, object = None):
222209
self.object = object
223210

224211
def __str__(self):
225-
return self.name()
212+
return self.name
226213

227214
def __repr__(self):
228215
return '<git.%s "%s">' % (self.__class__.__name__, self.path)

test/git/test_base.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,66 @@
88
from test.testlib import *
99
from git import *
1010
import git.base as base
11+
from itertools import chain
1112

1213
class TestBase(object):
1314

1415
type_tuples = ( ("blob", "8741fc1d09d61f02ffd8cded15ff603eff1ec070"),
1516
("tree", "3a6a5e3eeed3723c09f1ef0399f81ed6b8d82e79"),
16-
("commit", "4251bd59fb8e11e40c40548cba38180a9536118c") )
17+
("commit", "4251bd59fb8e11e40c40548cba38180a9536118c"),
18+
("tag", "e56a60e8e9cd333cfba0140a77cd12b0d9398f10") )
1719

1820
def setup(self):
1921
self.repo = Repo(GIT_REPO)
2022

21-
def test_base(self):
22-
# test interface of base classes
23-
fcreators = (self.repo.blob, self.repo.tree, self.repo.commit )
23+
def test_base_object(self):
24+
# test interface of base object classes
25+
fcreators = (self.repo.blob, self.repo.tree, self.repo.commit, lambda id: TagObject(self.repo,id) )
2426
assert len(fcreators) == len(self.type_tuples)
25-
for fcreator, (typename, hexsha) in zip(fcreators, self.type_tuples):
27+
28+
s = set()
29+
num_objs = 0
30+
num_index_objs = 0
31+
for fcreator, (typename, hexsha) in zip(fcreators, self.type_tuples):
2632
item = fcreator(hexsha)
33+
num_objs += 1
2734
assert item.id == hexsha
2835
assert item.type == typename
2936
assert item.size
37+
assert item.data
38+
assert item == item
39+
assert not item != item
40+
assert str(item) == item.id
41+
assert repr(item)
42+
s.add(item)
43+
44+
if isinstance(item, base.IndexObject):
45+
num_index_objs += 1
46+
if hasattr(item,'path'): # never runs here
47+
assert not item.path.startswith("/") # must be relative
48+
assert isinstance(item.mode, int)
49+
# END index object check
3050
# END for each object type to create
3151

32-
assert False,"TODO: Test for all types"
52+
# each has a unique sha
53+
assert len(s) == num_objs
54+
assert num_index_objs == 2
55+
3356

3457
def test_tags(self):
3558
# tag refs can point to tag objects or to commits
36-
assert False, "TODO: Tag handling"
59+
s = set()
60+
ref_count = 0
61+
for ref in chain(self.repo.tags, self.repo.heads):
62+
ref_count += 1
63+
assert isinstance(ref, base.Ref)
64+
assert str(ref) == ref.name
65+
assert repr(ref)
66+
assert ref == ref
67+
assert not ref != ref
68+
s.add(ref)
69+
# END for each ref
70+
assert len(s) == ref_count
3771

3872
def test_get_type_by_name(self):
3973
for tname in base.Object.TYPES:

test/git/test_commit.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ def test_bake(self, git):
2424
assert_true(git.called)
2525
assert_equal(git.call_args, (('rev_list', '4c8124ffcf4039d292442eeccabdeca5af5c5017', '--', ''), {'pretty': 'raw', 'max_count': 1}))
2626

27-
@patch_object(Git, '_call_process')
28-
def test_id_abbrev(self, git):
29-
git.return_value = fixture('rev_list_commit_idabbrev')
30-
assert_equal('80f136f', self.repo.commit('80f136f500dfdb8c3e8abf4ae716f875f0a1b57f').id_abbrev)
3127

3228
@patch_object(Git, '_call_process')
3329
def test_diff(self, git):

test/git/test_repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_commits(self, git):
5858
assert_equal("implement Grit#heads", c.message)
5959

6060
c = commits[1]
61-
assert_equal((,), c.parents)
61+
assert_equal(tuple(), c.parents)
6262

6363
c = commits[2]
6464
assert_equal(["6e64c55896aabb9a7d8e9f8f296f426d21a78c2c", "7f874954efb9ba35210445be456c74e037ba6af2"], map(lambda p: p.id, c.parents))

0 commit comments

Comments
 (0)