Skip to content

Commit

Permalink
test utils: implement a repo context manager
Browse files Browse the repository at this point in the history
The current hierarchy of test cases that create and blow away test
repositories in the ``setUp`` and ``tearDown`` methods is inadequate
for testing scenarios where multiple repositories must be edited,
e.g.  testing push.

Extract the temporary repository behaviour into a context manager
class ``TemporaryRepository`` which can be used via the ``with``
keyword.  The context manager's ``__enter__` method returns creates
the specified repository in a temporary directory and returns its
path.  The temporary directory is removed in ``__exit__``.

Update the existing test case base classes to use the context
manager and invoke ``__enter__`` and ``__exit__`` in the ``setUp``
and ``tearDown`` methods respectively.

Finally, make some small tweaks to a handful of tests to get them
working under this new system.
  • Loading branch information
frasertweedale committed Aug 30, 2013
1 parent 179c7db commit 74b1628
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
5 changes: 2 additions & 3 deletions test/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,12 @@ def test_create_blob_fromworkdir(self):


def test_create_blob_outside_workdir(self):
path = join(dirname(__file__), 'data', self.repo_dir + '.tar')
path = __file__
self.assertRaises(KeyError, self.repo.create_blob_fromworkdir, path)


def test_create_blob_fromdisk(self):
path = join(dirname(__file__), 'data', self.repo_dir + '.tar')
blob_oid = self.repo.create_blob_fromdisk(path)
blob_oid = self.repo.create_blob_fromdisk(__file__)
blob = self.repo[blob_oid]

self.assertTrue(isinstance(blob, pygit2.Blob))
Expand Down
6 changes: 3 additions & 3 deletions test/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_lookup_commit_prefix(self):

def test_get_path(self):
directory = realpath(self.repo.path)
expected = realpath(join(self._temp_dir, 'testrepo.git'))
expected = realpath(self.repo_path)
self.assertEqual(directory, expected)

def test_get_workdir(self):
Expand Down Expand Up @@ -179,12 +179,12 @@ def test_is_bare(self):

def test_get_path(self):
directory = realpath(self.repo.path)
expected = realpath(join(self._temp_dir, 'testrepo', '.git'))
expected = realpath(join(self.repo_path, '.git'))
self.assertEqual(directory, expected)

def test_get_workdir(self):
directory = realpath(self.repo.workdir)
expected = realpath(join(self._temp_dir, 'testrepo'))
expected = realpath(self.repo_path)
self.assertEqual(directory, expected)

def test_checkout_ref(self):
Expand Down
65 changes: 37 additions & 28 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ def rmtree(path):
shutil.rmtree(path, onerror=onerror)


class TemporaryRepository(object):
def __init__(self, repo_spec):
self.repo_spec = repo_spec

def __enter__(self):
container, name = self.repo_spec
repo_path = os.path.join(os.path.dirname(__file__), 'data', name)
self.temp_dir = tempfile.mkdtemp()
temp_repo_path = os.path.join(self.temp_dir, name)
if container == 'tar':
tar = tarfile.open('.'.join((repo_path, 'tar')))
tar.extractall(self.temp_dir)
tar.close()
else:
shutil.copytree(repo_path, temp_repo_path)
return temp_repo_path

def __exit__(self, exc_type, exc_value, traceback):
rmtree(self.temp_dir)


class NoRepoTestCase(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -103,45 +124,33 @@ def assertEqualSignature(self, a, b):
self.assertEqual(a.offset, b.offset)


class BareRepoTestCase(NoRepoTestCase):

repo_dir = 'testrepo.git'

class AutoRepoTestCase(NoRepoTestCase):
def setUp(self):
super(BareRepoTestCase, self).setUp()

repo_dir = self.repo_dir
repo_path = os.path.join(os.path.dirname(__file__), 'data', repo_dir)
temp_repo_path = os.path.join(self._temp_dir, repo_dir)

shutil.copytree(repo_path, temp_repo_path)

self.repo = pygit2.Repository(temp_repo_path)
super(AutoRepoTestCase, self).setUp()
self.repo_ctxtmgr = TemporaryRepository(self.repo_spec)
self.repo_path = self.repo_ctxtmgr.__enter__()
self.repo = pygit2.Repository(self.repo_path)

def tearDown(self):
self.repo_ctxtmgr.__exit__(None, None, None)
super(AutoRepoTestCase, self).tearDown()

class RepoTestCase(NoRepoTestCase):

repo_dir = 'testrepo'
class BareRepoTestCase(AutoRepoTestCase):

def setUp(self):
super(RepoTestCase, self).setUp()
repo_spec = 'git', 'testrepo.git'

repo_dir = self.repo_dir
repo_path = os.path.join(os.path.dirname(__file__), 'data', repo_dir)
temp_repo_path = os.path.join(self._temp_dir, repo_dir, '.git')

tar = tarfile.open(repo_path + '.tar')
tar.extractall(self._temp_dir)
tar.close()
class RepoTestCase(AutoRepoTestCase):

self.repo = pygit2.Repository(temp_repo_path)
repo_spec = 'tar', 'testrepo'


class DirtyRepoTestCase(RepoTestCase):
class DirtyRepoTestCase(AutoRepoTestCase):

repo_dir = 'dirtyrepo'
repo_spec = 'tar', 'dirtyrepo'


class EmptyRepoTestCase(RepoTestCase):
class EmptyRepoTestCase(AutoRepoTestCase):

repo_dir = 'emptyrepo'
repo_spec = 'tar', 'emptyrepo'

0 comments on commit 74b1628

Please sign in to comment.