Commit 2c3024ac authored by maruel@chromium.org's avatar maruel@chromium.org

Refactor the unit tests to remove a lot of duplicate code.

Remove old unused git-cl test files.

TEST=unit tests
BUG=none

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@18177 0039d316-1c4b-4281-b951-d872f2087c98
parent 07bbc214
......@@ -4,35 +4,3 @@
# found in the LICENSE file.
"""Unit tests for depot_tools."""
mox = None
def OnTestsLoad():
import os
import sys
old_path = sys.path
global mox
try:
directory, _file = os.path.split(__file__)
sys.path.append(os.path.abspath(os.path.join(directory, 'pymox')))
sys.path.append(os.path.abspath(os.path.join(directory, '..')))
try:
import mox as Mox
mox = Mox
except ImportError:
print "Trying to automatically checkout pymox."
import subprocess
subprocess.call(['svn', 'co', 'http://pymox.googlecode.com/svn/trunk',
os.path.join(directory, 'pymox')],
shell=True)
try:
import mox as Mox
mox = Mox
except ImportError:
print >> sys.stderr, ("\nError, failed to load pymox\n")
raise
finally:
# Restore the path
sys.path = old_path
OnTestsLoad()
#!/bin/bash
# Check that abandoning a branch also abandons its issue.
set -e
. ./test-lib.sh
setup_initsvn
setup_gitsvn
(
set -e
cd git-svn
git config rietveld.server localhost:8080
# Create a branch and give it an issue.
git checkout -q -b abandoned
echo "some work done on a branch" >> test
git add test; git commit -q -m "branch work"
export EDITOR=/bin/true
test_expect_success "upload succeeds" \
"$GIT_CL upload -m test master... | grep -q 'Issue created'"
# Switch back to master, delete the branch.
git checkout master
git branch -D abandoned
# Verify that "status" doesn't know about it anymore.
# The "exit" trickiness is inverting the exit status of grep.
test_expect_success "git-cl status dropped abandoned branch" \
"$GIT_CL status | grep -q abandoned && exit 1 || exit 0"
)
SUCCESS=$?
cleanup
if [ $SUCCESS == 0 ]; then
echo PASS
fi
#!/bin/bash
set -e
. ./test-lib.sh
setup_initsvn
setup_gitsvn
(
set -e
cd git-svn
git checkout -q -b work
echo "some work done on a branch" >> test
git add test; git commit -q -m "branch work"
echo "some other work done on a branch" >> test
git add test; git commit -q -m "branch work"
test_expect_success "git-cl upload wants a server" \
"$GIT_CL upload 2>&1 | grep -q 'You must configure'"
git config rietveld.server localhost:8080
test_expect_success "git-cl status has no issue" \
"$GIT_CL status | grep -q 'no issue'"
# Prevent the editor from coming up when you upload.
export EDITOR=/bin/true
test_expect_success "upload succeeds (needs a server running on localhost)" \
"$GIT_CL upload -m test master... | grep -q 'Issue created'"
test_expect_success "git-cl status now knows the issue" \
"$GIT_CL status | grep -q 'Issue number'"
# Push a description to this URL.
URL=$($GIT_CL status | sed -ne '/Issue number/s/[^(]*(\(.*\))/\1/p')
curl --cookie dev_appserver_login="test@example.com:False" \
--data-urlencode subject="test" \
--data-urlencode description="foo-quux" \
$URL/edit
test_expect_success "git-cl dcommits ok" \
"$GIT_CL dcommit -f"
git checkout -q master
git svn -q rebase >/dev/null 2>&1
test_expect_success "dcommitted code has proper description" \
"git show | grep -q 'foo-quux'"
test_expect_success "issue no longer has a branch" \
"git cl status | grep -q 'work: None'"
test_expect_success "upstream svn has our commit" \
"svn log $REPO_URL 2>/dev/null | grep -q 'foo-quux'"
)
SUCCESS=$?
cleanup
if [ $SUCCESS == 0 ]; then
echo PASS
fi
......@@ -8,33 +8,20 @@
import unittest
# Local imports
import __init__
import gcl
mox = __init__.mox
import super_mox
from super_mox import mox
class GclTestsBase(mox.MoxTestBase):
class GclTestsBase(super_mox.SuperMoxTestBase):
"""Setups and tear downs the mocks but doesn't test anything as-is."""
def setUp(self):
mox.MoxTestBase.setUp(self)
super_mox.SuperMoxTestBase.setUp(self)
self.mox.StubOutWithMock(gcl, 'RunShell')
self.mox.StubOutWithMock(gcl.gclient, 'CaptureSVNInfo')
self.mox.StubOutWithMock(gcl, 'os')
self.mox.StubOutWithMock(gcl.os, 'getcwd')
def compareMembers(self, object, 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))
if not x.startswith('_')]
expected_members = sorted(members)
if actual_members != expected_members:
diff = ([i for i in actual_members if i not in expected_members] +
[i for i in expected_members if i not in actual_members])
print diff
self.assertEqual(actual_members, expected_members)
class GclUnittest(GclTestsBase):
"""General gcl.py tests."""
......
......@@ -19,52 +19,28 @@
__author__ = 'stephen5.ng@gmail.com (Stephen Ng)'
import __builtin__
import copy
import os
import random
import string
import StringIO
import subprocess
import sys
import unittest
import __init__
import gclient
mox = __init__.mox
import super_mox
from super_mox import mox
## Some utilities for generating arbitrary arguments.
def String(max_length):
return ''.join([random.choice(string.letters)
for x in xrange(random.randint(1, max_length))])
def Strings(max_arg_count, max_arg_length):
return [String(max_arg_length) for x in xrange(max_arg_count)]
def Args(max_arg_count=8, max_arg_length=16):
return Strings(max_arg_count, random.randint(1, max_arg_length))
def _DirElts(max_elt_count=4, max_elt_length=8):
return os.sep.join(Strings(max_elt_count, max_elt_length))
def Dir(max_elt_count=4, max_elt_length=8):
return random.choice((os.sep, '')) + _DirElts(max_elt_count, max_elt_length)
def Url(max_elt_count=4, max_elt_length=8):
return ('svn://random_host:port/a' +
_DirElts(max_elt_count, max_elt_length).replace(os.sep, '/'))
def RootDir(max_elt_count=4, max_elt_length=8):
return os.sep + _DirElts(max_elt_count, max_elt_length)
class BaseTestCase(super_mox.SuperMoxTestBase):
def setUp(self):
super_mox.SuperMoxTestBase.setUp(self)
self.mox.StubOutWithMock(gclient.os.path, 'exists')
self.mox.StubOutWithMock(gclient.os.path, 'isdir')
self.mox.StubOutWithMock(gclient.sys, 'stdout')
self.mox.StubOutWithMock(gclient, 'subprocess')
# These are not tested.
self.mox.StubOutWithMock(gclient, 'FileRead')
self.mox.StubOutWithMock(gclient, 'FileWrite')
self.mox.StubOutWithMock(gclient, 'SubprocessCall')
self.mox.StubOutWithMock(gclient, 'RemoveDirectory')
class BaseTestCase(mox.MoxTestBase):
# Like unittest's assertRaises, but checks for Gclient.Error.
def assertRaisesError(self, msg, fn, *args, **kwargs):
try:
......@@ -74,19 +50,6 @@ class BaseTestCase(mox.MoxTestBase):
else:
self.fail('%s not raised' % msg)
def compareMembers(self, object, 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))
if not x.startswith('_')]
expected_members = sorted(members)
if actual_members != expected_members:
diff = ([i for i in actual_members if i not in expected_members] +
[i for i in expected_members if i not in actual_members])
print diff
self.assertEqual(actual_members, expected_members)
class GClientBaseTestCase(BaseTestCase):
def Options(self, *args, **kwargs):
......@@ -95,45 +58,18 @@ class GClientBaseTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
# Mock them to be sure nothing bad happens.
self._CaptureSVN = gclient.CaptureSVN
gclient.CaptureSVN = self.mox.CreateMockAnything()
self.mox.StubOutWithMock(gclient, 'CaptureSVN')
self._CaptureSVNInfo = gclient.CaptureSVNInfo
gclient.CaptureSVNInfo = self.mox.CreateMockAnything()
self._CaptureSVNStatus = gclient.CaptureSVNStatus
gclient.CaptureSVNStatus = self.mox.CreateMockAnything()
self._FileRead = gclient.FileRead
gclient.FileRead = self.mox.CreateMockAnything()
self._FileWrite = gclient.FileWrite
gclient.FileWrite = self.mox.CreateMockAnything()
self._RemoveDirectory = gclient.RemoveDirectory
gclient.RemoveDirectory = self.mox.CreateMockAnything()
self._RunSVN = gclient.RunSVN
gclient.RunSVN = self.mox.CreateMockAnything()
self._RunSVNAndGetFileList = gclient.RunSVNAndGetFileList
gclient.RunSVNAndGetFileList = self.mox.CreateMockAnything()
self._sys_stdout = gclient.sys.stdout
gclient.sys.stdout = self.mox.CreateMock(self._sys_stdout)
self._subprocess = gclient.subprocess
gclient.subprocess = self.mox.CreateMock(self._subprocess)
self._os_path_exists = gclient.os.path.exists
gclient.os.path.exists = self.mox.CreateMockAnything()
self.mox.StubOutWithMock(gclient, 'CaptureSVNInfo')
self.mox.StubOutWithMock(gclient, 'CaptureSVNStatus')
self.mox.StubOutWithMock(gclient, 'RunSVN')
self.mox.StubOutWithMock(gclient, 'RunSVNAndGetFileList')
self._gclient_gclient = gclient.GClient
gclient.GClient = self.mox.CreateMockAnything()
self._scm_wrapper = gclient.SCMWrapper
gclient.SCMWrapper = self.mox.CreateMockAnything()
def tearDown(self):
gclient.CaptureSVN = self._CaptureSVN
gclient.CaptureSVNInfo = self._CaptureSVNInfo
gclient.CaptureSVNStatus = self._CaptureSVNStatus
gclient.FileRead = self._FileRead
gclient.FileWrite = self._FileWrite
gclient.RemoveDirectory = self._RemoveDirectory
gclient.RunSVN = self._RunSVN
gclient.RunSVNAndGetFileList = self._RunSVNAndGetFileList
gclient.sys.stdout = self._sys_stdout
gclient.subprocess = self._subprocess
gclient.os.path.exists = self._os_path_exists
gclient.GClient = self._gclient_gclient
gclient.SCMWrapper = self._scm_wrapper
BaseTestCase.tearDown(self)
......@@ -163,9 +99,9 @@ class GclientTestCase(GClientBaseTestCase):
GClientBaseTestCase.setUp(self)
self.platform = 'darwin'
self.args = Args()
self.root_dir = Dir()
self.url = Url()
self.args = self.Args()
self.root_dir = self.Dir()
self.url = self.Url()
class GClientCommandsTestCase(GClientBaseTestCase):
......@@ -1061,16 +997,10 @@ class SCMWrapperTestCase(GClientBaseTestCase):
def setUp(self):
GClientBaseTestCase.setUp(self)
self.root_dir = Dir()
self.args = Args()
self.url = Url()
self.root_dir = self.Dir()
self.args = self.Args()
self.url = self.Url()
self.relpath = 'asf'
self._os_path_isdir = gclient.os.path.isdir
gclient.os.path.isdir = self.mox.CreateMockAnything()
def tearDown(self):
gclient.os.path.isdir = self._os_path_isdir
GClientBaseTestCase.tearDown(self)
def testDir(self):
members = [
......@@ -1307,15 +1237,6 @@ class SCMWrapperTestCase(GClientBaseTestCase):
class RunSVNTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
self._OldSubprocessCall = gclient.SubprocessCall
gclient.SubprocessCall = self.mox.CreateMockAnything()
def tearDown(self):
gclient.SubprocessCall = self._OldSubprocessCall
BaseTestCase.tearDown(self)
def testRunSVN(self):
param2 = 'bleh'
gclient.SubprocessCall(['svn', 'foo', 'bar'], param2).AndReturn(None)
......@@ -1326,18 +1247,7 @@ class RunSVNTestCase(BaseTestCase):
class SubprocessCallAndCaptureTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
self._sys_stdout = gclient.sys.stdout
gclient.sys.stdout = self.mox.CreateMock(self._sys_stdout)
self._subprocess_Popen = gclient.subprocess.Popen
gclient.subprocess.Popen = self.mox.CreateMockAnything()
self._CaptureSVN = gclient.CaptureSVN
gclient.CaptureSVN = self.mox.CreateMockAnything()
def tearDown(self):
gclient.sys.stdout = self._sys_stdout
gclient.subprocess.Popen = self._subprocess_Popen
gclient.CaptureSVN = self._CaptureSVN
BaseTestCase.tearDown(self)
self.mox.StubOutWithMock(gclient, 'CaptureSVN')
def testSubprocessCallAndCapture(self):
command = ['boo', 'foo', 'bar']
......@@ -1354,7 +1264,7 @@ class SubprocessCallAndCaptureTestCase(BaseTestCase):
for i in test_string:
gclient.sys.stdout.write(i)
gclient.subprocess.Popen(command, bufsize=0, cwd=in_directory,
shell=(sys.platform == 'win32'),
shell=(gclient.sys.platform == 'win32'),
stdout=gclient.subprocess.PIPE).AndReturn(kid)
self.mox.ReplayAll()
capture_list = []
......
......@@ -6,43 +6,17 @@
"""Unit tests for presubmit_support.py and presubmit_canned_checks.py."""
import exceptions
import random
import string
import StringIO
import unittest
# Local imports
import __init__
import presubmit_support as presubmit
import presubmit_canned_checks
mox = __init__.mox
import super_mox
from super_mox import mox
def String(max_length):
return ''.join([random.choice(string.letters)
for x in xrange(random.randint(1, max_length))])
def Strings(max_arg_count, max_arg_length):
return [String(max_arg_length) for x in xrange(max_arg_count)]
def Args(max_arg_count=8, max_arg_length=16):
return Strings(max_arg_count, random.randint(1, max_arg_length))
def _DirElts(max_elt_count=4, max_elt_length=8):
return presubmit.os.sep.join(Strings(max_elt_count, max_elt_length))
def Dir(max_elt_count=4, max_elt_length=8):
return (random.choice((presubmit_support.os.sep, '')) +
_DirElts(max_elt_count, max_elt_length))
def Url(max_elt_count=4, max_elt_length=8):
return ('svn://random_host:port/a' +
_DirElts(max_elt_count, max_elt_length).replace(os.sep, '/'))
def RootDir(max_elt_count=4, max_elt_length=8):
return presubmit.os.sep + _DirElts(max_elt_count, max_elt_length)
class PresubmitTestsBase(mox.MoxTestBase):
class PresubmitTestsBase(super_mox.SuperMoxTestBase):
"""Setups and tear downs the mocks but doesn't test anything as-is."""
presubmit_text = """
def CheckChangeOnUpload(input_api, output_api):
......@@ -58,7 +32,7 @@ def CheckChangeOnUpload(input_api, output_api):
"""
def setUp(self):
mox.MoxTestBase.setUp(self)
super_mox.SuperMoxTestBase.setUp(self)
self.mox.StubOutWithMock(presubmit, 'warnings')
# Stub out 'os' but keep os.path.dirname/join/normpath/splitext and os.sep.
os_sep = presubmit.os.sep
......@@ -79,7 +53,7 @@ def CheckChangeOnUpload(input_api, output_api):
return f
presubmit.os.path.abspath = MockAbsPath
self.mox.StubOutWithMock(presubmit.gcl, 'GetRepositoryRoot')
fake_root_dir = RootDir()
fake_root_dir = self.RootDir()
self.fake_root_dir = fake_root_dir
def MockGetRepositoryRoot():
return fake_root_dir
......@@ -88,14 +62,6 @@ def CheckChangeOnUpload(input_api, output_api):
self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty')
self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile')
def compareMembers(self, object, 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))
if not x.startswith('_')]
self.assertEqual(actual_members, sorted(members))
def MakeBasicChange(self, name, description, root=None):
ci = presubmit.gcl.ChangeInfo(name, 0, 0, description, None)
if root is None:
......
......@@ -10,28 +10,13 @@ import unittest
# Local imports
import revert
import super_mox
from super_mox import mox
class RevertTestsBase(unittest.TestCase):
class RevertTestsBase(super_mox.SuperMoxTestBase):
"""Setups and tear downs the mocks but doesn't test anything as-is."""
def setUp(self):
pass
def tearDown(self):
pass
def compareMembers(self, object, 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))
if not x.startswith('_')]
expected_members = sorted(members)
if actual_members != expected_members:
diff = ([i for i in actual_members if i not in expected_members] +
[i for i in expected_members if i not in actual_members])
print diff
self.assertEqual(actual_members, expected_members)
pass
class RevertUnittest(RevertTestsBase):
......
#!/bin/bash
# Abort on error.
set -e
PWD=`pwd`
REPO_URL=file://$PWD/svnrepo
GIT_CL=$PWD/../git-cl
# Set up an SVN repo that has a few commits to trunk.
setup_initsvn() {
echo "Setting up test SVN repo..."
rm -rf svnrepo
svnadmin create svnrepo
rm -rf svn
svn co -q $REPO_URL svn
(
cd svn
echo "test" > test
svn add -q test
svn commit -q -m "initial commit"
echo "test2" >> test
svn commit -q -m "second commit"
)
}
# Set up a git-svn checkout of the repo.
setup_gitsvn() {
echo "Setting up test git-svn repo..."
rm -rf git-svn
# There appears to be no way to make git-svn completely shut up, so we
# redirect its output.
git svn -q clone $REPO_URL git-svn >/dev/null 2>&1
}
cleanup() {
rm -rf svnrepo svn git-svn
}
# Usage: test_expect_success "description of test" "test code".
test_expect_success() {
echo "TESTING: $1"
exit_code=0
sh -c "$2" || exit_code=$?
if [ $exit_code != 0 ]; then
echo "FAILURE: $1"
return $exit_code
fi
}
......@@ -9,29 +9,14 @@ import os
import unittest
# Local imports
import super_mox
import trychange
from super_mox import mox
class TryChangeTestsBase(unittest.TestCase):
class TryChangeTestsBase(super_mox.SuperMoxTestBase):
"""Setups and tear downs the mocks but doesn't test anything as-is."""
def setUp(self):
pass
def tearDown(self):
pass
def compareMembers(self, object, 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))
if not x.startswith('_')]
expected_members = sorted(members)
if actual_members != expected_members:
diff = ([i for i in actual_members if i not in expected_members] +
[i for i in expected_members if i not in actual_members])
print diff
self.assertEqual(actual_members, expected_members)
pass
class TryChangeUnittest(TryChangeTestsBase):
......
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