Commit 389d6dea authored by maruel@chromium.org's avatar maruel@chromium.org

Improve tests and remove some unused GIT test.

This is needed to improve the git tests further in a later change.

BUG=54084
TEST=none

Review URL: http://codereview.chromium.org/3294017

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@58934 0039d316-1c4b-4281-b951-d872f2087c98
parent 54019f3c
This diff is collapsed.
......@@ -9,7 +9,7 @@ from shutil import rmtree
import tempfile
# Fixes include path.
from super_mox import mox, SuperMoxBaseTestBase, SuperMoxTestBase
from super_mox import mox, TestCaseUtils, SuperMoxTestBase
import scm
......@@ -46,89 +46,8 @@ class RootTestCase(BaseSCMTestCase):
self.compareMembers(scm, members)
class GitWrapperTestCase(SuperMoxBaseTestBase):
sample_git_import = """blob
mark :1
data 6
Hello
blob
mark :2
data 4
Bye
reset refs/heads/master
commit refs/heads/master
mark :3
author Bob <bob@example.com> 1253744361 -0700
committer Bob <bob@example.com> 1253744361 -0700
data 8
A and B
M 100644 :1 a
M 100644 :2 b
blob
mark :4
data 10
Hello
You
blob
mark :5
data 8
Bye
You
commit refs/heads/origin
mark :6
author Alice <alice@example.com> 1253744424 -0700
committer Alice <alice@example.com> 1253744424 -0700
data 13
Personalized
from :3
M 100644 :4 a
M 100644 :5 b
reset refs/heads/master
from :3
"""
def CreateGitRepo(self, git_import, path):
try:
scm.subprocess.Popen(['git', 'init'],
stdout=scm.subprocess.PIPE,
stderr=scm.subprocess.STDOUT,
cwd=path).communicate()
except OSError:
# git is not available, skip this test.
return False
scm.subprocess.Popen(['git', 'fast-import'],
stdin=scm.subprocess.PIPE,
stdout=scm.subprocess.PIPE,
stderr=scm.subprocess.STDOUT,
cwd=path).communicate(input=git_import)
scm.subprocess.Popen(['git', 'checkout'],
stdout=scm.subprocess.PIPE,
stderr=scm.subprocess.STDOUT,
cwd=path).communicate()
return True
def setUp(self):
SuperMoxBaseTestBase.setUp(self)
self.args = self.Args()
self.url = 'git://foo'
self.root_dir = tempfile.mkdtemp()
self.relpath = '.'
self.base_path = scm.os.path.join(self.root_dir, self.relpath)
self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path)
self.fake_root = self.Dir()
def tearDown(self):
rmtree(self.root_dir)
SuperMoxBaseTestBase.tearDown(self)
class GitWrapperTestCase(BaseSCMTestCase):
def testMembersChanged(self):
self.mox.ReplayAll()
members = [
'AssertVersion', 'Capture', 'CaptureStatus',
'FetchUpstreamTuple',
......@@ -141,20 +60,17 @@ from :3
def testGetEmail(self):
self.mox.StubOutWithMock(scm.GIT, 'Capture')
scm.GIT.Capture(['config', 'user.email'], self.fake_root, error_ok=True
scm.GIT.Capture(['config', 'user.email'], self.root_dir, error_ok=True
).AndReturn(['mini@me.com', ''])
self.mox.ReplayAll()
self.assertEqual(scm.GIT.GetEmail(self.fake_root), 'mini@me.com')
self.assertEqual(scm.GIT.GetEmail(self.root_dir), 'mini@me.com')
class SVNTestCase(BaseSCMTestCase):
def setUp(self):
BaseSCMTestCase.setUp(self)
self.root_dir = self.Dir()
self.args = self.Args()
self.url = self.Url()
self.relpath = 'asf'
self.mox.StubOutWithMock(scm.SVN, 'Capture')
self.url = self.SvnUrl()
def testMembersChanged(self):
self.mox.ReplayAll()
......
......@@ -28,7 +28,7 @@ class IsOneOf(mox.Comparator):
return '<sequence or map containing \'%s\'>' % str(self._keys)
class SuperMoxBaseTestBase(mox.MoxTestBase):
class TestCaseUtils(object):
"""Base class with some additional functionalities. People will usually want
to use SuperMoxTestBase instead."""
# Backup the separator in case it gets mocked
......@@ -56,7 +56,7 @@ class SuperMoxBaseTestBase(mox.MoxTestBase):
return (self._RANDOM_CHOICE((self._OS_SEP, '')) +
self._DirElts(max_elt_count, max_elt_length))
def Url(self, max_elt_count=4, max_elt_length=8):
def SvnUrl(self, max_elt_count=4, max_elt_length=8):
return ('svn://random_host:port/a' +
self._DirElts(max_elt_count, max_elt_length
).replace(self._OS_SEP, '/'))
......@@ -64,11 +64,11 @@ class SuperMoxBaseTestBase(mox.MoxTestBase):
def RootDir(self, max_elt_count=4, max_elt_length=8):
return self._OS_SEP + self._DirElts(max_elt_count, max_elt_length)
def compareMembers(self, object, members):
def compareMembers(self, obj, members):
"""If you add a member, be sure to add the relevant test!"""
# Skip over members starting with '_' since they are usually not meant to
# be for public use.
actual_members = [x for x in sorted(dir(object))
actual_members = [x for x in sorted(dir(obj))
if not x.startswith('_')]
expected_members = sorted(members)
if actual_members != expected_members:
......@@ -77,18 +77,20 @@ class SuperMoxBaseTestBase(mox.MoxTestBase):
print>>sys.stderr, diff
self.assertEqual(actual_members, expected_members)
def UnMock(self, object, name):
"""Restore an object inside a test."""
for (parent, old_child, child_name) in self.mox.stubs.cache:
if parent == object and child_name == name:
setattr(parent, child_name, old_child)
break
def setUp(self):
self.root_dir = self.Dir()
self.args = self.Args()
self.relpath = self.String(200)
def tearDown(self):
pass
class SuperMoxTestBase(SuperMoxBaseTestBase):
class SuperMoxTestBase(TestCaseUtils, mox.MoxTestBase):
def setUp(self):
"""Patch a few functions with know side-effects."""
SuperMoxBaseTestBase.setUp(self)
TestCaseUtils.setUp(self)
mox.MoxTestBase.setUp(self)
#self.mox.StubOutWithMock(__builtin__, 'open')
os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2',
'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'getpid', 'lseek',
......@@ -104,6 +106,10 @@ class SuperMoxTestBase(SuperMoxBaseTestBase):
# Don't mock stderr since it confuses unittests.
self.MockList(sys, ('stdin', 'stdout'))
def tearDown(self):
TestCaseUtils.tearDown(self)
mox.MoxTestBase.tearDown(self)
def MockList(self, parent, items_to_mock):
for item in items_to_mock:
# Skip over items not present because of OS-specific implementation,
......@@ -113,3 +119,10 @@ class SuperMoxTestBase(SuperMoxBaseTestBase):
self.mox.StubOutWithMock(parent, item)
except TypeError:
raise TypeError('Couldn\'t mock %s in %s' % (item, parent.__name__))
def UnMock(self, object, name):
"""Restore an object inside a test."""
for (parent, old_child, child_name) in self.mox.stubs.cache:
if parent == object and child_name == name:
setattr(parent, child_name, old_child)
break
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment