Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef48ca5

Browse files
committed
Added rest of submodule.add test code which should be pretty much 100% coverage for it
1 parent 7b3ef45 commit ef48ca5

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

lib/git/objects/submodule.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Submodule(base.IndexObject, Iterable, Traversable):
8585
k_modules_file = '.gitmodules'
8686
k_head_option = 'branch'
8787
k_head_default = 'master'
88-
k_def_mode = stat.S_IFDIR | stat.S_IFLNK # submodules are directories with link-status
88+
k_default_mode = stat.S_IFDIR | stat.S_IFLNK # submodules are directories with link-status
8989

9090
# this is a bogus type for base class compatability
9191
type = 'submodule'
@@ -244,7 +244,7 @@ def add(cls, repo, name, path, url=None, branch=None, no_checkout=False):
244244
# END handle trailing slash
245245

246246
# INSTANTIATE INTERMEDIATE SM
247-
sm = cls(repo, cls.NULL_BIN_SHA, cls.k_def_mode, path, name)
247+
sm = cls(repo, cls.NULL_BIN_SHA, cls.k_default_mode, path, name)
248248
if sm.exists():
249249
# reretrieve submodule from tree
250250
return repo.head.commit.tree[path]
@@ -712,10 +712,17 @@ def iter_items(cls, repo, parent_commit='HEAD'):
712712
# END handle optional information
713713

714714
# get the binsha
715+
index = repo.index
715716
try:
716717
sm = rt[p]
717718
except KeyError:
718-
raise InvalidGitRepositoryError("Gitmodule path %r did not exist in revision of parent commit %s" % (p, parent_commit))
719+
# try the index, maybe it was just added
720+
try:
721+
entry = index.entries[index.entry_key(p, 0)]
722+
sm = cls(repo, entry.binsha, entry.mode, entry.path)
723+
except KeyError:
724+
raise InvalidGitRepositoryError("Gitmodule path %r did not exist in revision of parent commit %s" % (p, parent_commit))
725+
# END handle keyerror
719726
# END handle critical error
720727

721728
# fill in remaining info - saves time as it doesn't have to be parsed again
@@ -743,7 +750,7 @@ def __init__(self, repo):
743750
super(RootModule, self).__init__(
744751
repo,
745752
binsha = self.NULL_BIN_SHA,
746-
mode = self.k_def_mode,
753+
mode = self.k_default_mode,
747754
path = '',
748755
name = self.k_root_name,
749756
parent_commit = repo.head.commit,

test/git/test_submodule.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _do_base_tests(self, rwrepo):
3232
assert len(Submodule.list_items(rwrepo, self.k_no_subm_tag)) == 0
3333

3434
assert sm.path == 'lib/git/ext/gitdb'
35-
assert sm.path == sm.name # for now, this is True
35+
assert sm.path == sm.name # for now, this is True
3636
assert sm.url == 'git://gitorious.org/git-python/gitdb.git'
3737
assert sm.branch.name == 'master' # its unset in this case
3838
assert sm.parent_commit == rwrepo.head.commit
@@ -109,8 +109,6 @@ def _do_base_tests(self, rwrepo):
109109
# no url and no module at path fails
110110
self.failUnlessRaises(ValueError, Submodule.add, rwrepo, "newsubm", "pathtorepo", url=None)
111111

112-
# TODO: Test no remote url in existing repository
113-
114112
# CONTINUE UPDATE
115113
#################
116114

@@ -123,6 +121,7 @@ def _do_base_tests(self, rwrepo):
123121
os.rmdir(newdir)
124122

125123
assert sm.update() is sm
124+
sm_repopath = sm.path # cache for later
126125
assert sm.module_exists()
127126
assert isinstance(sm.module(), git.Repo)
128127
assert sm.module().working_tree_dir == sm.module_path()
@@ -146,6 +145,7 @@ def _do_base_tests(self, rwrepo):
146145
assert len(sm.children()) == 1 # its not checked out yet
147146
csm = sm.children()[0]
148147
assert not csm.module_exists()
148+
csm_repopath = csm.path
149149

150150
# adjust the path of the submodules module to point to the local destination
151151
new_csmclone_path = to_native_path_linux(join_path_native(self.rorepo.working_tree_dir, sm.path, csm.path))
@@ -233,10 +233,44 @@ def _do_base_tests(self, rwrepo):
233233
assert not sm.exists()
234234
assert not sm.module_exists()
235235

236+
assert len(rwrepo.submodules) == 0
237+
236238
# ADD NEW SUBMODULE
237239
###################
238-
# raise if url does not match remote url of existing repo
240+
# add a simple remote repo - trailing slashes are no problem
241+
smid = "newsub"
242+
osmid = "othersub"
243+
nsm = Submodule.add(rwrepo, smid, sm_repopath, new_smclone_path, None, no_checkout=True)
244+
assert nsm.name == smid
245+
assert nsm.module_exists()
246+
assert nsm.exists()
247+
# its not checked out
248+
assert not os.path.isfile(join_path_native(nsm.module().working_tree_dir, Submodule.k_modules_file))
249+
assert len(rwrepo.submodules) == 1
250+
251+
# add another submodule, but into the root, not as submodule
252+
osm = Submodule.add(rwrepo, osmid, csm_repopath, new_csmclone_path, Submodule.k_head_default)
253+
assert osm != nsm
254+
assert osm.module_exists()
255+
assert osm.exists()
256+
assert os.path.isfile(join_path_native(osm.module().working_tree_dir, 'setup.py'))
257+
258+
assert len(rwrepo.submodules) == 2
259+
260+
# commit the changes, just to finalize the operation
261+
rwrepo.index.commit("my submod commit")
262+
assert len(rwrepo.submodules) == 2
239263

264+
# if a submodule's repo has no remotes, it can't be added without an explicit url
265+
osmod = osm.module()
266+
# needs update as the head changed, it thinks its in the history
267+
# of the repo otherwise
268+
osm._parent_commit = rwrepo.head.commit
269+
osm.remove(module=False)
270+
for remote in osmod.remotes:
271+
remote.remove(osmod, remote.name)
272+
assert not osm.exists()
273+
self.failUnlessRaises(ValueError, Submodule.add, rwrepo, osmid, csm_repopath, url=None)
240274
# END handle bare mode
241275

242276

0 commit comments

Comments
 (0)