Skip to content

Commit 024adf3

Browse files
committed
Fixed tests far enough to allow basic repository tests to be applied to any of the new database types. This reduces code duplication to the mere minimum, but allows custom tests to be added on top easily and flexibly
1 parent 112bb16 commit 024adf3

21 files changed

+152
-65
lines changed

doc/source/changes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ NEXT
2121
* ### Module Changes ###
2222

2323
* Removed rev_parse function from git.repo.fun - the respective functionality is available only through the repository's rev_parse method, which might in turn translate to any implementation.
24+
25+
* ### Exceptions ###
26+
27+
* There is a new common base for all exceptions git-python will throw, namely `GitPythonError`.
2428

2529
0.3.1 Beta 2
2630
============

git/db/cmd/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
2-
from complex import *
1+
# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
2+
#
3+
# This module is part of GitDB and is released under
4+
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php

git/db/cmd/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
bin_to_hex,
1616
hex_to_bin
1717
)
18-
from git.db.compat import RepoCompatInterface
18+
from git.db.compat import RepoCompatibilityInterface
1919
from git.util import RemoteProgress
2020
from git.db.interface import FetchInfo as GitdbFetchInfo
2121
from git.db.interface import PushInfo as GitdbPushInfo

git/db/cmd/complex.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
"""Module with our own git implementation - it uses the git command"""
22

3-
from git.db.compat import RepoCompatInterface
3+
from git.db.compat import RepoCompatibilityInterface
44
from git.db.py.complex import PureGitDB
55

66
from base import *
77

88

9-
__all__ = ['GitCmdDB', 'CmdCompatibilityGitDB']
9+
__all__ = ['GitCmdDB', 'CmdCompatibilityGitDB', 'CmdPartialGitDB']
1010

1111

12-
class CmdGitDB( GitCommandMixin, CmdObjectDBRMixin, CmdTransportMixin,
13-
CmdHighLevelRepository, PureGitDB):
12+
class CmdPartialGitDB( GitCommandMixin, CmdObjectDBRMixin, CmdTransportMixin,
13+
CmdHighLevelRepository ):
14+
"""Utility repository which only partially implements all required methods.
15+
It cannot be reliably used alone, but is provided to allow mixing it with other
16+
implementations"""
1417
pass
1518

16-
class CmdCompatibilityGitDB(CmdGitDB, RepoCompatInterface):
19+
20+
class CmdGitDB(CmdPartialGitDB, PureGitDB):
21+
"""A database which fills in its missing implementation using the pure python
22+
implementation"""
23+
pass
24+
25+
26+
class CmdCompatibilityGitDB(CmdGitDB, RepoCompatibilityInterface):
1727
"""Command git database with the compatabilty interface added for 0.3x code"""

git/db/compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
55
"""Module providing adaptors to maintain backwards compatability"""
66

7-
class RepoCompatInterface(object):
7+
class RepoCompatibilityInterface(object):
88
"""Interface to install backwards compatability of the new complex repository
99
types with the previous, all in one, repository."""
1010

git/db/complex.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""Module with many useful complex databases with different useful combinations of primary implementations"""
22

33
from py.complex import PureGitDB
4-
from cmd.complex import CmdGitDB
5-
from compat import RepoCompatInterface
4+
from cmd.complex import CmdPartialGitDB
5+
from compat import RepoCompatibilityInterface
66

7-
__all__ = ['CmdGitDB', 'PureGitDB', 'PureCmdGitDB']
7+
__all__ = ['CmdPartialGitDB', 'PureGitDB', 'PureCmdGitDB']
88

9-
class PureCmdGitDB(PureGitDB, CmdGitDB, RepoCompatInterface):
9+
class PureCmdGitDB(PureGitDB, CmdPartialGitDB, RepoCompatibilityInterface):
1010
"""Repository which uses the pure implementation primarily, but falls back
1111
to the git command implementation. Please note that the CmdGitDB does it
1212
the opposite way around."""

git/db/py/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
#
33
# This module is part of GitDB and is released under
44
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
5-
6-
from complex import *

git/db/py/complex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from ref import PureReferenceDB
2121
from submodule import PureSubmoduleDB
2222

23-
from git.db.compat import RepoCompatInterface
23+
from git.db.compat import RepoCompatibilityInterface
2424

2525
from git.util import (
2626
LazyMixin,
@@ -123,6 +123,6 @@ def __init__(self, root_path):
123123

124124

125125

126-
class PureCompatibilityGitDB(PureGitDB, RepoCompatInterface):
126+
class PureCompatibilityGitDB(PureGitDB, RepoCompatibilityInterface):
127127
"""Pure git database with a compatability layer required by 0.3x code"""
128128

git/db/py/resolve.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33

44
from git.db.interface import ReferencesMixin
55
from git.exc import BadObject
6-
from git.refs import SymbolicReference
7-
from git.objects.base import Object
8-
from git.objects.commit import Commit
6+
from git.refs import (
7+
SymbolicReference,
8+
Reference,
9+
HEAD,
10+
Head,
11+
TagReference
12+
)
913
from git.refs.head import HEAD
1014
from git.refs.headref import Head
1115
from git.refs.tag import TagReference
16+
17+
from git.objects.base import Object
18+
from git.objects.commit import Commit
1219
from git.util import (
1320
join,
1421
isdir,

git/exc.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
from util import to_hex_sha
99

10-
class ODBError(Exception):
10+
class GitPythonError(Exception):
11+
"""Base exception for all git-python related errors"""
12+
13+
class ODBError(GitPythonError):
1114
"""All errors thrown by the object database"""
1215

1316

@@ -40,15 +43,15 @@ class UnsupportedOperation(ODBError):
4043
"""Thrown if the given operation cannot be supported by the object database"""
4144

4245

43-
class InvalidGitRepositoryError(Exception):
46+
class InvalidGitRepositoryError(GitPythonError):
4447
""" Thrown if the given repository appears to have an invalid format. """
4548

4649

47-
class NoSuchPathError(OSError):
50+
class NoSuchPathError(GitPythonError):
4851
""" Thrown if a path could not be access by the system. """
4952

5053

51-
class GitCommandError(Exception):
54+
class GitCommandError(GitPythonError):
5255
""" Thrown if execution of the git command fails with non-zero status code. """
5356
def __init__(self, command, status, stderr=None):
5457
self.stderr = stderr
@@ -60,7 +63,7 @@ def __str__(self):
6063
(' '.join(str(i) for i in self.command), self.status, self.stderr))
6164

6265

63-
class CheckoutError( Exception ):
66+
class CheckoutError(GitPythonError):
6467
"""Thrown if a file could not be checked out from the index as it contained
6568
changes.
6669
@@ -83,7 +86,7 @@ def __str__(self):
8386
return Exception.__str__(self) + ":%s" % self.failed_files
8487

8588

86-
class CacheError(Exception):
89+
class CacheError(GitPythonError):
8790
"""Base for all errors related to the git index, which is called cache internally"""
8891

8992

git/index/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
)
6464

6565
from git.base import IStream
66-
from git.db.py.mem import PureMemoryDB
6766
from git.util import to_bin_sha
6867
from itertools import izip
6968

@@ -512,7 +511,9 @@ def write_tree(self):
512511
:raise UnmergedEntriesError: """
513512
# we obtain no lock as we just flush our contents to disk as tree
514513
# If we are a new index, the entries access will load our data accordingly
515-
mdb = PureMemoryDB()
514+
# Needs delayed import as db.py import IndexFile as well
515+
import git.db.py.mem
516+
mdb = git.db.py.mem.PureMemoryDB()
516517
entries = self._entries_sorted()
517518
binsha, tree_items = write_tree_from_cache(entries, mdb, slice(0, len(entries)))
518519

git/repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66
"""This module is just to maintain compatibility to git-python 0.3x"""
77

8-
from git.db.cmd import CmdCompatibilityGitDB
8+
from git.db.cmd.complex import CmdCompatibilityGitDB
99

1010

1111
import warnings

git/test/test_repo.py renamed to git/test/db/base.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6-
from git.test.lib import TestBase
6+
from lib import TestDBBase
7+
from git.test.lib import *
78
from git import *
89
from git.util import join_path_native
910
from git.exc import BadObject
@@ -15,18 +16,22 @@
1516
from cStringIO import StringIO
1617

1718

18-
class TestRepo(TestBase):
19+
class RepoGlobalsItemDeletorMetaCls(GlobalsItemDeletorMetaCls):
20+
ModuleToDelete = 'RepoBase'
21+
22+
23+
class RepoBase(TestDBBase):
24+
"""Basic test for everything a fully implemented repository should support"""
25+
__metaclass__ = RepoGlobalsItemDeletorMetaCls
1926

20-
@raises(InvalidGitRepositoryError)
2127
def test_new_should_raise_on_invalid_repo_location(self):
22-
Repo(tempfile.gettempdir())
28+
self.failUnlessRaises(InvalidGitRepositoryError, self.RepoCls, tempfile.gettempdir())
2329

24-
@raises(NoSuchPathError)
2530
def test_new_should_raise_on_non_existant_path(self):
26-
Repo("repos/foobar")
31+
self.failUnlessRaises(NoSuchPathError, self.RepoCls, "repos/foobar")
2732

2833
def test_repo_creation_from_different_paths(self):
29-
r_from_gitdir = Repo(self.rorepo.git_dir)
34+
r_from_gitdir = self.RepoCls(self.rorepo.git_dir)
3035
assert r_from_gitdir.git_dir == self.rorepo.git_dir
3136
assert r_from_gitdir.git_dir.endswith('.git')
3237
assert not self.rorepo.git.working_dir.endswith('.git')
@@ -184,7 +189,10 @@ def test_init(self):
184189
# END restore previous state
185190

186191
def test_bare_property(self):
187-
self.rorepo.bare
192+
if isinstance(self.rorepo, RepoCompatibilityInterface):
193+
self.rorepo.bare
194+
#END handle compatability
195+
self.rorepo.is_bare
188196

189197
def test_daemon_export(self):
190198
orig_val = self.rorepo.daemon_export
@@ -204,7 +212,7 @@ def test_alternates(self):
204212
self.rorepo.alternates = cur_alternates
205213

206214
def test_repr(self):
207-
path = os.path.join(os.path.abspath(GIT_REPO), '.git')
215+
path = os.path.join(os.path.abspath(rorepo_dir()), '.git')
208216
assert_equal('<git.Repo "%s">' % path, repr(self.rorepo))
209217

210218
def test_is_dirty_with_bare_repository(self):

git/test/db/cmd/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
2+
#
3+
# This module is part of GitDB and is released under
4+
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php

git/test/db/cmd/test_base.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
2+
#
3+
# This module is part of GitDB and is released under
4+
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
5+
from git.test.lib import *
6+
from git.db import RefSpec
7+
8+
class TestBase(TestDBBase):
9+
10+
@with_rw_directory
11+
def test_basics(self, path):
12+
assert False
13+

git/test/db/lib.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
)
1313

1414
from git.stream import Sha1Writer
15-
16-
# import database types we want to support
17-
# they will be set to None if the respective library could not be loaded
18-
from git.db.py import PureGitDB
19-
2015
from git.base import (
2116
IStream,
2217
OStream,
@@ -40,8 +35,6 @@ class TestDBBase(TestBase):
4035
two_lines = "1234\nhello world"
4136
all_data = (two_lines, )
4237

43-
# all supported database types. Add your own type
44-
ref_db_types = (PureGitDB, )
4538

4639
def _assert_object_writing_simple(self, db):
4740
# write a bunch of objects and query their streams and info

git/test/db/py/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
2+
#
3+
# This module is part of GitDB and is released under
4+
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php

git/test/db/py/test_base.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
2+
#
3+
# This module is part of GitDB and is released under
4+
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
5+
from git.test.lib import *
6+
from git.test.db.base import RepoBase
7+
from git.db.py.complex import *
8+
9+
from git.db.complex import PureCmdGitDB
10+
11+
class TestPyDBBase(RepoBase):
12+
13+
RepoCls = PureCmdGitDB
14+
15+
def test_instantiation(self):
16+
db = PureGitDB(rorepo_dir())
17+
cdb = PureCompatibilityGitDB(rorepo_dir())
18+

git/test/lib/base.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
22
#
3-
# This module is part of PureGitDB and is released under
3+
# This module is part of PureCmdGitDB and is released under
44
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
55
"""Utilities used in ODB testing"""
66
from git.base import OStream
7-
from git.db.py import PureGitDB
87
from git.stream import (
98
Sha1Writer,
109
ZippedStoreShaWriter
@@ -73,7 +72,7 @@ def wrapper(self, path):
7372
shutil.copytree(src_dir, path)
7473
target_gitdir = os.path.join(path, '.git')
7574
assert os.path.isdir(target_gitdir)
76-
return func(self, PureGitDB(target_gitdir))
75+
return func(self, self.RepoCls(target_gitdir))
7776
#END wrapper
7877
wrapper.__name__ = func.__name__
7978
return with_rw_directory(wrapper)
@@ -98,17 +97,17 @@ def wrapper(self, path):
9897

9998
#{ Routines
10099

101-
def repo_dir():
100+
def rorepo_dir():
102101
""":return: path to our own repository, being our own .git directory.
103102
:note: doesn't work in bare repositories"""
104103
base = os.path.join(dirname(dirname(dirname(dirname(__file__)))), '.git')
105104
assert os.path.isdir(base)
106105
return base
107106

108107

109-
def maketemp(*args):
108+
def maketemp(*args, **kwargs):
110109
"""Wrapper around default tempfile.mktemp to fix an osx issue"""
111-
tdir = tempfile.mktemp(*args)
110+
tdir = tempfile.mktemp(*args, **kwargs)
112111
if sys.platform == 'darwin':
113112
tdir = '/private' + tdir
114113
return tdir
@@ -192,12 +191,3 @@ def _assert(self):
192191

193192
#} END stream utilitiess
194193

195-
#{ Bases
196-
197-
class TestBase(unittest.TestCase):
198-
"""Base class for all tests"""
199-
# The non-database specific tests just provides a default pure git database
200-
rorepo = PureGitDB(repo_dir())
201-
202-
#} END bases
203-

0 commit comments

Comments
 (0)