From 4177eefd7bdaea96a529b00ba9cf751924ede202 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 5 May 2011 19:43:22 +0200 Subject: Added all code from gitdb to gitpython. Next is to make it generally work. Then the tests will need some work --- git/test/lib/base.py | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 git/test/lib/base.py (limited to 'git/test/lib/base.py') diff --git a/git/test/lib/base.py b/git/test/lib/base.py new file mode 100644 index 00000000..9224f5f6 --- /dev/null +++ b/git/test/lib/base.py @@ -0,0 +1,200 @@ +# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors +# +# This module is part of PureGitDB and is released under +# the New BSD License: http://www.opensource.org/licenses/bsd-license.php +"""Utilities used in ODB testing""" +from gitdb import OStream +from gitdb.db.py import PureGitDB +from gitdb.stream import ( + Sha1Writer, + ZippedStoreShaWriter + ) + +from gitdb.util import zlib + +import sys +import random +from array import array +from cStringIO import StringIO + +import glob +import unittest +import tempfile +import shutil +import os +import gc + + +#{ Decorators + +def with_rw_directory(func): + """Create a temporary directory which can be written to, remove it if the + test suceeds, but leave it otherwise to aid additional debugging""" + def wrapper(self): + path = tempfile.mktemp(prefix=func.__name__) + os.mkdir(path) + keep = False + try: + try: + return func(self, path) + except Exception: + print >> sys.stderr, "Test %s.%s failed, output is at %r" % (type(self).__name__, func.__name__, path) + keep = True + raise + finally: + # Need to collect here to be sure all handles have been closed. It appears + # a windows-only issue. In fact things should be deleted, as well as + # memory maps closed, once objects go out of scope. For some reason + # though this is not the case here unless we collect explicitly. + if not keep: + gc.collect() + shutil.rmtree(path) + # END handle exception + # END wrapper + + wrapper.__name__ = func.__name__ + return wrapper + + +def with_rw_repo(func): + """Create a copy of our repository and put it into a writable location. It will + be removed if the test doesn't result in an error. + As we can currently only copy the fully working tree, tests must not rely on + being on a certain branch or on anything really except for the default tags + that should exist + Wrapped function obtains a git repository """ + def wrapper(self, path): + src_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) + assert(os.path.isdir(path)) + os.rmdir(path) # created by wrapper, but must not exist for copy operation + shutil.copytree(src_dir, path) + target_gitdir = os.path.join(path, '.git') + assert os.path.isdir(target_gitdir) + return func(self, PureGitDB(target_gitdir)) + #END wrapper + wrapper.__name__ = func.__name__ + return with_rw_directory(wrapper) + + + +def with_packs_rw(func): + """Function that provides a path into which the packs for testing should be + copied. Will pass on the path to the actual function afterwards + + :note: needs with_rw_directory wrapped around it""" + def wrapper(self, path): + src_pack_glob = fixture_path('packs/*') + copy_files_globbed(src_pack_glob, path, hard_link_ok=True) + return func(self, path) + # END wrapper + + wrapper.__name__ = func.__name__ + return with_rw_directory(wrapper) + +#} END decorators + +#{ Routines + +def repo_dir(): + """:return: path to our own repository, being our own .git directory. + :note: doesn't work in bare repositories""" + base = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), '.git') + assert os.path.isdir(base) + return base + + +def maketemp(*args): + """Wrapper around default tempfile.mktemp to fix an osx issue""" + tdir = tempfile.mktemp(*args) + if sys.platform == 'darwin': + tdir = '/private' + tdir + return tdir + +def fixture_path(relapath=''): + """:return: absolute path into the fixture directory + :param relapath: relative path into the fixtures directory, or '' + to obtain the fixture directory itself""" + return os.path.join(os.path.dirname(__file__), 'fixtures', relapath) + +def copy_files_globbed(source_glob, target_dir, hard_link_ok=False): + """Copy all files found according to the given source glob into the target directory + :param hard_link_ok: if True, hard links will be created if possible. Otherwise + the files will be copied""" + for src_file in glob.glob(source_glob): + if hard_link_ok and hasattr(os, 'link'): + target = os.path.join(target_dir, os.path.basename(src_file)) + try: + os.link(src_file, target) + except OSError: + shutil.copy(src_file, target_dir) + # END handle cross device links ( and resulting failure ) + else: + shutil.copy(src_file, target_dir) + # END try hard link + # END for each file to copy + + +def make_bytes(size_in_bytes, randomize=False): + """:return: string with given size in bytes + :param randomize: try to produce a very random stream""" + actual_size = size_in_bytes / 4 + producer = xrange(actual_size) + if randomize: + producer = list(producer) + random.shuffle(producer) + # END randomize + a = array('i', producer) + return a.tostring() + +def make_object(type, data): + """:return: bytes resembling an uncompressed object""" + odata = "blob %i\0" % len(data) + return odata + data + +def make_memory_file(size_in_bytes, randomize=False): + """:return: tuple(size_of_stream, stream) + :param randomize: try to produce a very random stream""" + d = make_bytes(size_in_bytes, randomize) + return len(d), StringIO(d) + +#} END routines + +#{ Stream Utilities + +class DummyStream(object): + def __init__(self): + self.was_read = False + self.bytes = 0 + self.closed = False + + def read(self, size): + self.was_read = True + self.bytes = size + + def close(self): + self.closed = True + + def _assert(self): + assert self.was_read + + +class DeriveTest(OStream): + def __init__(self, sha, type, size, stream, *args, **kwargs): + self.myarg = kwargs.pop('myarg') + self.args = args + + def _assert(self): + assert self.args + assert self.myarg + +#} END stream utilitiess + +#{ Bases + +class TestBase(unittest.TestCase): + """Base class for all tests""" + # The non-database specific tests just provides a default pure git database + rorepo = PureGitDB(repo_dir()) + +#} END bases + -- cgit v1.2.3 From acf5e6ea64a2f24117f1d419c208ed1c38c43690 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 6 May 2011 15:03:14 +0200 Subject: replaced all gitdb strings with git --- git/test/lib/base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'git/test/lib/base.py') diff --git a/git/test/lib/base.py b/git/test/lib/base.py index 9224f5f6..9ed2c4b2 100644 --- a/git/test/lib/base.py +++ b/git/test/lib/base.py @@ -3,14 +3,14 @@ # This module is part of PureGitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php """Utilities used in ODB testing""" -from gitdb import OStream -from gitdb.db.py import PureGitDB -from gitdb.stream import ( +from git import OStream +from git.db.py import PureGitDB +from git.stream import ( Sha1Writer, ZippedStoreShaWriter ) -from gitdb.util import zlib +from git.util import zlib import sys import random -- cgit v1.2.3 From 7ae36c3e019a5cc16924d1b6007774bfb625036f Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 6 May 2011 18:53:59 +0200 Subject: Started to fix imports - tests still have no chance to work as database changed drastically. Now the actual work begins --- git/test/lib/base.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'git/test/lib/base.py') diff --git a/git/test/lib/base.py b/git/test/lib/base.py index 9ed2c4b2..3725d544 100644 --- a/git/test/lib/base.py +++ b/git/test/lib/base.py @@ -3,14 +3,17 @@ # This module is part of PureGitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php """Utilities used in ODB testing""" -from git import OStream +from git.base import OStream from git.db.py import PureGitDB from git.stream import ( Sha1Writer, ZippedStoreShaWriter ) -from git.util import zlib +from git.util import ( + zlib, + dirname + ) import sys import random @@ -31,7 +34,7 @@ def with_rw_directory(func): """Create a temporary directory which can be written to, remove it if the test suceeds, but leave it otherwise to aid additional debugging""" def wrapper(self): - path = tempfile.mktemp(prefix=func.__name__) + path = maketemp(prefix=func.__name__) os.mkdir(path) keep = False try: @@ -64,7 +67,7 @@ def with_rw_repo(func): that should exist Wrapped function obtains a git repository """ def wrapper(self, path): - src_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) + src_dir = dirname(dirname(dirname(__file__))) assert(os.path.isdir(path)) os.rmdir(path) # created by wrapper, but must not exist for copy operation shutil.copytree(src_dir, path) @@ -98,7 +101,7 @@ def with_packs_rw(func): def repo_dir(): """:return: path to our own repository, being our own .git directory. :note: doesn't work in bare repositories""" - base = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), '.git') + base = os.path.join(dirname(dirname(dirname(dirname(__file__)))), '.git') assert os.path.isdir(base) return base @@ -114,7 +117,7 @@ def fixture_path(relapath=''): """:return: absolute path into the fixture directory :param relapath: relative path into the fixtures directory, or '' to obtain the fixture directory itself""" - return os.path.join(os.path.dirname(__file__), 'fixtures', relapath) + return os.path.join(dirname(__file__), 'fixtures', relapath) def copy_files_globbed(source_glob, target_dir, hard_link_ok=False): """Copy all files found according to the given source glob into the target directory -- cgit v1.2.3 From 024adf37acddd6a5d8293b6b5d15795c59a142c0 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 30 May 2011 13:06:37 +0200 Subject: 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 --- git/test/lib/base.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) (limited to 'git/test/lib/base.py') diff --git a/git/test/lib/base.py b/git/test/lib/base.py index 3725d544..221395c9 100644 --- a/git/test/lib/base.py +++ b/git/test/lib/base.py @@ -1,10 +1,9 @@ # Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors # -# This module is part of PureGitDB and is released under +# This module is part of PureCmdGitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php """Utilities used in ODB testing""" from git.base import OStream -from git.db.py import PureGitDB from git.stream import ( Sha1Writer, ZippedStoreShaWriter @@ -73,7 +72,7 @@ def with_rw_repo(func): shutil.copytree(src_dir, path) target_gitdir = os.path.join(path, '.git') assert os.path.isdir(target_gitdir) - return func(self, PureGitDB(target_gitdir)) + return func(self, self.RepoCls(target_gitdir)) #END wrapper wrapper.__name__ = func.__name__ return with_rw_directory(wrapper) @@ -98,7 +97,7 @@ def with_packs_rw(func): #{ Routines -def repo_dir(): +def rorepo_dir(): """:return: path to our own repository, being our own .git directory. :note: doesn't work in bare repositories""" base = os.path.join(dirname(dirname(dirname(dirname(__file__)))), '.git') @@ -106,9 +105,9 @@ def repo_dir(): return base -def maketemp(*args): +def maketemp(*args, **kwargs): """Wrapper around default tempfile.mktemp to fix an osx issue""" - tdir = tempfile.mktemp(*args) + tdir = tempfile.mktemp(*args, **kwargs) if sys.platform == 'darwin': tdir = '/private' + tdir return tdir @@ -192,12 +191,3 @@ class DeriveTest(OStream): #} END stream utilitiess -#{ Bases - -class TestBase(unittest.TestCase): - """Base class for all tests""" - # The non-database specific tests just provides a default pure git database - rorepo = PureGitDB(repo_dir()) - -#} END bases - -- cgit v1.2.3 From 1f71ed94578799ee1667ba54b66a369e307f415b Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 30 May 2011 16:32:56 +0200 Subject: git cmd implementation of repository appears to work, at least this is what the test suggests. Pure python implementation still has some trouble, but this should be very fixable --- git/test/lib/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git/test/lib/base.py') diff --git a/git/test/lib/base.py b/git/test/lib/base.py index 221395c9..7bd9215e 100644 --- a/git/test/lib/base.py +++ b/git/test/lib/base.py @@ -1,6 +1,6 @@ # Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors # -# This module is part of PureCmdGitDB and is released under +# This module is part of PureCompatibilityGitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php """Utilities used in ODB testing""" from git.base import OStream -- cgit v1.2.3 From c192638aae09c1b5c087d67cc99dd4c7ec4ed916 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 30 May 2011 18:19:44 +0200 Subject: Fixed all remaining python repository tests --- git/test/lib/base.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'git/test/lib/base.py') diff --git a/git/test/lib/base.py b/git/test/lib/base.py index 7bd9215e..bc160783 100644 --- a/git/test/lib/base.py +++ b/git/test/lib/base.py @@ -86,6 +86,7 @@ def with_packs_rw(func): :note: needs with_rw_directory wrapped around it""" def wrapper(self, path): src_pack_glob = fixture_path('packs/*') + print src_pack_glob copy_files_globbed(src_pack_glob, path, hard_link_ok=True) return func(self, path) # END wrapper @@ -103,7 +104,6 @@ def rorepo_dir(): base = os.path.join(dirname(dirname(dirname(dirname(__file__)))), '.git') assert os.path.isdir(base) return base - def maketemp(*args, **kwargs): """Wrapper around default tempfile.mktemp to fix an osx issue""" @@ -116,8 +116,15 @@ def fixture_path(relapath=''): """:return: absolute path into the fixture directory :param relapath: relative path into the fixtures directory, or '' to obtain the fixture directory itself""" - return os.path.join(dirname(__file__), 'fixtures', relapath) + test_dir = os.path.dirname(os.path.dirname(__file__)) + return os.path.join(test_dir, "fixtures", relapath) +def fixture(name): + return open(fixture_path(name), 'rb').read() + +def absolute_project_path(): + return os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) + def copy_files_globbed(source_glob, target_dir, hard_link_ok=False): """Copy all files found according to the given source glob into the target directory :param hard_link_ok: if True, hard links will be created if possible. Otherwise -- cgit v1.2.3