Update tests to create isolate git repo

Tests should be run in a pristine git repo where we control how the
commits are created. This allows us to modify the contents of the git
repo to perform more complete set ups to support exercising more
specific senarios in a predicatable fashion.
This commit is contained in:
Darragh Bailey
2014-07-15 16:34:01 +01:00
parent 51e5eaec53
commit d8f077003e
3 changed files with 142 additions and 63 deletions

View File

@@ -14,12 +14,42 @@
# under the License.
import os
import tempfile
import git
import fixtures
import git
import testtools
LOREM_IPSUM = """\
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi
enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis
nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in
hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu
feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui
blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla
facilisi.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure
dolor in hendrerit in vulputate velit esse molestie consequat, vel illum
dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio
dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te
feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing
elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam
erat volutpat.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie
consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et
accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit
augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis
nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea
commodo consequat."""
class DiveDir(fixtures.Fixture):
"""Dive into given directory and return back on cleanup.
@@ -36,11 +66,12 @@ class DiveDir(fixtures.Fixture):
class GitRepo(fixtures.Fixture):
"""Create a copy git repo in which to operate."""
"""Create an empty git repo in which to operate."""
def __init__(self, repo):
self.repo = repo
def __init__(self):
self.repo = None
self.path = ''
self._file_list = set()
def setUp(self):
super(GitRepo, self).setUp()
@@ -48,7 +79,52 @@ class GitRepo(fixtures.Fixture):
self.addCleanup(tempdir.cleanUp)
tempdir.setUp()
self.path = os.path.join(tempdir.path, 'git')
self.repo.clone(self.path)
os.mkdir(self.path)
g = git.Git(self.path)
g.init()
self.repo = git.Repo(self.path)
self.repo.git.config('user.email', 'user@example.com')
self.repo.git.config('user.name', 'Example User')
self._create_file_commit()
def _create_file(self, contents=None):
if not contents:
contents = LOREM_IPSUM
# always want to ensure the files added to the repo are unique no
# matter which branch they are added to, as otherwise there may
# be conflicts caused by replaying local changes and performing
# merges
while True:
tmpfile = tempfile.NamedTemporaryFile(dir=self.repo.working_dir,
delete=False)
if tmpfile.name not in self._file_list:
self._file_list.add(tmpfile.name)
break
tmpfile.close()
os.remote(tmpfile.name)
tmpfile.write(contents)
tmpfile.close()
return tmpfile.name
def _create_file_commit(self, change_id=None):
filename = self._create_file()
self.repo.git.add(filename)
message = "Adding %s" % os.path.basename(filename)
if change_id:
message = message + "\n\nChange-Id: %s" % change_id
self.repo.git.commit(m=message)
def add_commits(self, num=1, ref="HEAD", change_ids=None):
"""Create the given number of commits using generated files"""
if ref != "HEAD":
self.repo.git.checkout(ref)
num = max(num, len(change_ids))
ids = list(change_ids) + [None] * (num - len(change_ids))
for x in range(num):
self._create_file_commit(ids[x])
class BaseTestCase(testtools.TestCase):
@@ -57,8 +133,7 @@ class BaseTestCase(testtools.TestCase):
def setUp(self):
super(BaseTestCase, self).setUp()
repo_path = self.useFixture(GitRepo(git.Repo('.'))).path
self.testrepo = self.useFixture(GitRepo())
repo_path = self.testrepo.path
self.useFixture(DiveDir(repo_path))
repo = git.Repo('.')
repo.git.config('user.email', 'user@example.com')
repo.git.config('user.name', 'Example User')
self.repo = self.testrepo.repo

View File

@@ -17,58 +17,57 @@
from git_upstream.commands import drop as d
from git_upstream.tests import base
from git import repo as r
from git import GitCommandError
class TestDrop(base.BaseTestCase):
"""Test case for Drop class"""
first_commit = "bd6b9eefe961abe8c15cb5dc6905b92e14714a4e"
second_commit = "05fac847a5629e36050dcd69b9a782b2645d3cc7"
invalid_commit = "this_is_an_invalid_commit"
author = "Walter White <heisenberg@hp.com>"
note_ref = 'refs/notes/upstream-merge'
def setUp(self):
super(TestDrop, self).setUp()
self.first_commit = self.repo.commit()
def test_valid_parameters(self):
"""Test drop initialization and read properties"""
repo = r.Repo('.')
automatic_author = '%s <%s>' % (repo.git.config('user.name'),
repo.git.config('user.email'))
t = d.Drop(git_object=TestDrop.first_commit)
automatic_author = '%s <%s>' % (self.repo.git.config('user.name'),
self.repo.git.config('user.email'))
t = d.Drop(git_object=self.first_commit)
self.assertEquals(t.author, automatic_author)
t = d.Drop(git_object=TestDrop.first_commit, author=TestDrop.author)
self.assertEquals(t.author, TestDrop.author)
t = d.Drop(git_object=self.first_commit, author=self.author)
self.assertEquals(t.author, self.author)
def test_invalid_commit(self):
"""Test drop initialization with invalid commit"""
self.assertRaises(d.DropError, d.Drop,
git_object=TestDrop.invalid_commit)
git_object=self.invalid_commit)
def test_mark(self):
"""Test drop mark"""
t = d.Drop(git_object=TestDrop.first_commit, author=TestDrop.author)
t = d.Drop(git_object=self.first_commit, author=self.author)
repo = r.Repo('.')
try:
# Older git versions don't support --ignore-missing so we need to
# catch GitCommandError exception
repo.git.notes('--ref', TestDrop.note_ref, 'remove',
TestDrop.first_commit)
self.repo.git.notes('--ref', self.note_ref, 'remove',
self.first_commit)
except GitCommandError:
pass
t.mark()
self.assertRegexpMatches(
'^Dropped: %s' % TestDrop.author,
repo.git.notes('--ref', TestDrop.note_ref, 'show',
TestDrop.first_commit)
'^Dropped: %s' % self.author,
self.repo.git.notes('--ref', self.note_ref, 'show',
self.first_commit)
)
repo.git.notes('--ref', TestDrop.note_ref, 'remove',
TestDrop.first_commit)
self.repo.git.notes('--ref', self.note_ref, 'remove',
self.first_commit)

View File

@@ -17,7 +17,6 @@
from git_upstream.commands import supersede as s
from git_upstream.tests import base
from git import repo as r
from git import GitCommandError
@@ -36,84 +35,90 @@ class TestSupersede(base.BaseTestCase):
invalid_change_ids_branch = "this_is_an_invalid_change_ids_branch"
note_ref = 'refs/notes/upstream-merge'
def setUp(self):
super(TestSupersede, self).setUp()
self.first_commit = self.repo.commit()
self.testrepo.add_commits(change_ids=self.first_change_ids)
self.second_commit = self.repo.commit()
self.testrepo.add_commits(change_ids=self.second_change_ids)
def test_valid_parameters(self):
"""Test supersede initialization and read properties"""
t = s.Supersede(git_object=TestSupersede.first_commit,
change_ids=TestSupersede.first_change_ids,
upstream_branch=TestSupersede.change_ids_branch)
t = s.Supersede(git_object=self.first_commit,
change_ids=self.first_change_ids,
upstream_branch=self.change_ids_branch)
self.assertEquals(str(t.commit), TestSupersede.first_commit)
self.assertNotEqual(str(t.commit), TestSupersede.second_commit)
self.assertEquals(t.commit, self.first_commit)
self.assertNotEqual(t.commit, self.second_commit)
self.assertEqual(str(t.change_ids_branch),
TestSupersede.change_ids_branch)
self.change_ids_branch)
self.assertNotEqual(str(t.change_ids_branch),
TestSupersede.invalid_change_ids_branch)
self.invalid_change_ids_branch)
def test_invalid_commit(self):
"""Test supersede initialization with invalid commit"""
self.assertRaises(s.SupersedeError, s.Supersede,
git_object=TestSupersede.invalid_commit,
change_ids=TestSupersede.first_change_ids,
upstream_branch=TestSupersede.change_ids_branch)
git_object=self.invalid_commit,
change_ids=self.first_change_ids,
upstream_branch=self.change_ids_branch)
def test_multiple_change_id(self):
"""Test supersede initialization with multiple change ids"""
t = s.Supersede(git_object=TestSupersede.first_commit,
change_ids=TestSupersede.second_change_ids,
upstream_branch=TestSupersede.change_ids_branch)
t = s.Supersede(git_object=self.first_commit,
change_ids=self.second_change_ids,
upstream_branch=self.change_ids_branch)
self.assertEquals(str(t.commit), TestSupersede.first_commit)
self.assertNotEqual(str(t.commit), TestSupersede.second_commit)
self.assertEquals(t.commit, self.first_commit)
self.assertNotEqual(t.commit, self.second_commit)
def test_invalid_cids(self):
"""Test supersede initialization with invalid cids"""
self.assertRaises(s.SupersedeError, s.Supersede,
git_object=TestSupersede.first_commit,
change_ids=TestSupersede.invalid_change_ids,
upstream_branch=TestSupersede.change_ids_branch)
git_object=self.first_commit,
change_ids=self.invalid_change_ids,
upstream_branch=self.change_ids_branch)
def test_default_upstream_branch(self):
"""Test supersede initialization with no branch name"""
self.assertRaises(s.SupersedeError, s.Supersede,
git_object=TestSupersede.first_commit,
change_ids=TestSupersede.invalid_change_ids,
git_object=self.first_commit,
change_ids=self.invalid_change_ids,
upstream_branch=
TestSupersede.invalid_change_ids_branch)
self.invalid_change_ids_branch)
def test_no_upstream_branch(self):
"""Test supersede initialization with invalid branch name"""
self.assertRaises(s.SupersedeError, s.Supersede,
git_object=TestSupersede.first_commit,
change_ids=TestSupersede.invalid_change_ids)
git_object=self.first_commit,
change_ids=self.invalid_change_ids)
def test_mark(self):
"""Test Supersede mark"""
t = s.Supersede(git_object=TestSupersede.first_commit,
change_ids=TestSupersede.first_change_ids,
upstream_branch=TestSupersede.change_ids_branch)
t = s.Supersede(git_object=self.first_commit,
change_ids=self.first_change_ids,
upstream_branch=self.change_ids_branch)
repo = r.Repo('.')
try:
# Older git versions don't support --ignore-missing
repo.git.notes('--ref', TestSupersede.note_ref, 'remove',
TestSupersede.first_commit)
self.repo.git.notes('--ref', self.note_ref, 'remove',
self.first_commit)
except GitCommandError:
pass
t.mark()
self.assertRegexpMatches(
'^Superseded-by: %s' % TestSupersede.first_change_ids,
repo.git.notes('--ref', TestSupersede.note_ref, 'show',
TestSupersede.first_commit)
'^Superseded-by: %s' % self.first_change_ids,
self.repo.git.notes('--ref', self.note_ref, 'show',
self.first_commit)
)
repo.git.notes('--ref', TestSupersede.note_ref, 'remove',
TestSupersede.first_commit)
self.repo.git.notes('--ref', self.note_ref, 'remove',
self.first_commit)