Skip to content

Commit 624556e

Browse files
committed
Optimized test-decorators, by completely removing with_bare_rw_repo, which was mainly copy-paste from with_rw_repo, what a shame
1 parent f97653a commit 624556e

File tree

9 files changed

+32
-51
lines changed

9 files changed

+32
-51
lines changed

lib/git/objects/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ def stream_data(self, ostream):
114114
class IndexObject(Object):
115115
"""Base for all objects that can be part of the index file , namely Tree, Blob and
116116
SubModule objects"""
117-
__slots__ = ("path", "mode")
117+
__slots__ = ("path", "mode")
118+
119+
# for compatability with iterable lists
120+
_id_attribute_ = 'path'
118121

119122
def __init__(self, repo, binsha, mode=None, path=None):
120123
"""Initialize a newly instanced IndexObject

lib/git/repo/base.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def remote(self, name='origin'):
225225
@property
226226
def submodules(self):
227227
""":return: git.IterableList(Submodule, ...) of direct submodules"""
228-
return self.list_submodules(recursive=False)
228+
return Submodule.list_items(self)
229229

230230
def submodule(self, name):
231231
""":return: Submodule with the given name
@@ -236,12 +236,11 @@ def submodule(self, name):
236236
raise ValueError("Didn't find submodule named %r" % name)
237237
# END exception handling
238238

239-
def list_submodules(self, recursive=False):
240-
"""A list if Submodule objects available in this repository
241-
:param recursive: If True, submodules of submodules (and so forth) will be
242-
returned as well as part of a depth-first traversal
243-
:return: ``git.IterableList(Submodule, ...)"""
244-
return RootModule(self).list_traverse(ignore_self=1, depth = recursive and -1 or 1)
239+
def iter_submodules(self, *args, **kwargs):
240+
"""An iterator yielding Submodule instances, see Traversable interface
241+
for a description of args and kwargs
242+
:return: Iterator"""
243+
return RootModule(self).traverse(*args, **kwargs)
245244

246245
@property
247246
def tags(self):

test/git/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_object_resolution(self):
8383
# objects must be resolved to shas so they compare equal
8484
assert self.rorepo.head.reference.object == self.rorepo.active_branch.object
8585

86-
@with_bare_rw_repo
86+
@with_rw_repo('HEAD', bare=True)
8787
def test_with_bare_rw_repo(self, bare_rw_repo):
8888
assert bare_rw_repo.config_reader("repository").getboolean("core", "bare")
8989
assert os.path.isfile(os.path.join(bare_rw_repo.git_dir,'HEAD'))

test/git/test_commit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def test_base(self):
237237
name_rev = self.rorepo.head.commit.name_rev
238238
assert isinstance(name_rev, basestring)
239239

240-
@with_bare_rw_repo
240+
@with_rw_repo('HEAD', bare=True)
241241
def test_serialization(self, rwrepo):
242242
# create all commits of our repo
243243
assert_commit_serialization(rwrepo, '0.1.6')

test/git/test_remote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def test_base(self, rw_repo, remote_repo):
422422
origin = rw_repo.remote('origin')
423423
assert origin == rw_repo.remotes.origin
424424

425-
@with_bare_rw_repo
425+
@with_rw_repo('HEAD', bare=True)
426426
def test_creation_and_removal(self, bare_rw_repo):
427427
new_name = "test_new_one"
428428
arg_list = (new_name, "git@server:hello.git")

test/git/test_repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ def test_repo_odbtype(self):
558558

559559
def test_submodules(self):
560560
assert len(self.rorepo.submodules) == 1 # non-recursive
561-
assert len(self.rorepo.list_submodules(recursive=True)) == 2
561+
assert len(list(self.rorepo.iter_submodules())) == 2
562562

563563
assert isinstance(self.rorepo.submodule("lib/git/ext/gitdb"), Submodule)
564564
self.failUnlessRaises(ValueError, self.rorepo.submodule, "doesn't exist")

test/git/test_submodule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _do_base_tests(self, rwrepo):
105105
def test_base_rw(self, rwrepo):
106106
self._do_base_tests(rwrepo)
107107

108-
@with_bare_rw_repo
108+
@with_rw_repo(k_subm_current, bare=True)
109109
def test_base_bare(self, rwrepo):
110110
self._do_base_tests(rwrepo)
111111

test/git/test_tree.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def test_traverse(self):
102102
assert isinstance(obj, (Blob, Tree))
103103
all_items.append(obj)
104104
# END for each object
105+
assert all_items == root.list_traverse()
106+
105107
# limit recursion level to 0 - should be same as default iteration
106108
assert all_items
107109
assert 'CHANGES' in root

test/testlib/helper.py

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
GIT_REPO = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
1616

17+
__all__ = (
18+
'fixture_path', 'fixture', 'absolute_project_path', 'StringProcessAdapter',
19+
'with_rw_repo', 'with_rw_and_rw_remote_repo', 'TestBase', 'TestCase', 'GIT_REPO'
20+
)
21+
1722
#{ Routines
1823

1924
def fixture_path(name):
@@ -58,41 +63,7 @@ def _rmtree_onerror(osremove, fullpath, exec_info):
5863
os.chmod(fullpath, 0777)
5964
os.remove(fullpath)
6065

61-
def with_bare_rw_repo(func):
62-
"""
63-
Decorator providing a specially made read-write repository to the test case
64-
decorated with it. The test case requires the following signature::
65-
def case(self, rw_repo)
66-
67-
The rwrepo will be a bare clone or the types rorepo. Once the method finishes,
68-
it will be removed completely.
69-
70-
Use this if you want to make purely index based adjustments, change refs, create
71-
heads, generally operations that do not need a working tree."""
72-
def bare_repo_creator(self):
73-
repo_dir = tempfile.mktemp("bare_repo_%s" % func.__name__)
74-
rw_repo = self.rorepo.clone(repo_dir, shared=True, bare=True)
75-
prev_cwd = os.getcwd()
76-
try:
77-
try:
78-
return func(self, rw_repo)
79-
except:
80-
# assure we keep the repo for debugging
81-
print >> sys.stderr, "Keeping bare repo after failure: %s" % repo_dir
82-
repo_dir = None
83-
raise
84-
# END handle exceptions
85-
finally:
86-
rw_repo.git.clear_cache()
87-
if repo_dir is not None:
88-
shutil.rmtree(repo_dir, onerror=_rmtree_onerror)
89-
# END remove repo dir
90-
# END cleanup
91-
# END bare repo creator
92-
bare_repo_creator.__name__ = func.__name__
93-
return bare_repo_creator
94-
95-
def with_rw_repo(working_tree_ref):
66+
def with_rw_repo(working_tree_ref, bare=False):
9667
"""
9768
Same as with_bare_repo, but clones the rorepo as non-bare repository, checking
9869
out the working tree at the given working_tree_ref.
@@ -105,11 +76,17 @@ def with_rw_repo(working_tree_ref):
10576
assert isinstance(working_tree_ref, basestring), "Decorator requires ref name for working tree checkout"
10677
def argument_passer(func):
10778
def repo_creator(self):
108-
repo_dir = tempfile.mktemp("non_bare_%s" % func.__name__)
109-
rw_repo = self.rorepo.clone(repo_dir, shared=True, bare=False, n=True)
79+
prefix = 'non_'
80+
if bare:
81+
prefix = ''
82+
#END handle prefix
83+
repo_dir = tempfile.mktemp("%sbare_%s" % (prefix, func.__name__))
84+
rw_repo = self.rorepo.clone(repo_dir, shared=True, bare=bare, n=True)
11085

11186
rw_repo.head.commit = rw_repo.commit(working_tree_ref)
112-
rw_repo.head.reference.checkout()
87+
if not bare:
88+
rw_repo.head.reference.checkout()
89+
# END handle checkout
11390

11491
prev_cwd = os.getcwd()
11592
os.chdir(rw_repo.working_dir)

0 commit comments

Comments
 (0)