Commit 8ef5f54e authored by maruel@chromium.org's avatar maruel@chromium.org

Cleanup the unit tests by mocking more system functions.

TEST=unit tests
BUG=none

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@31754 0039d316-1c4b-4281-b951-d872f2087c98
parent 57ec90d4
......@@ -5,29 +5,18 @@
"""Unit tests for gcl.py."""
import unittest
# Local imports
import gcl
import super_mox
from super_mox import mox
from super_mox import mox, SuperMoxTestBase
class GclTestsBase(super_mox.SuperMoxTestBase):
class GclTestsBase(SuperMoxTestBase):
"""Setups and tear downs the mocks but doesn't test anything as-is."""
def setUp(self):
super_mox.SuperMoxTestBase.setUp(self)
SuperMoxTestBase.setUp(self)
self.fake_root_dir = self.RootDir()
self.mox.StubOutWithMock(gcl, 'RunShell')
self.mox.StubOutWithMock(gcl.gclient_scm, 'CaptureSVNInfo')
self.mox.StubOutWithMock(gcl.os, 'getcwd')
self.mox.StubOutWithMock(gcl.os, 'chdir')
self.mox.StubOutWithMock(gcl.os, 'close')
self.mox.StubOutWithMock(gcl.os, 'remove')
self.mox.StubOutWithMock(gcl.os, 'write')
self.mox.StubOutWithMock(gcl.os.path, 'exists')
self.mox.StubOutWithMock(gcl.os.path, 'isdir')
self.mox.StubOutWithMock(gcl.os.path, 'isfile')
self.mox.StubOutWithMock(gcl, 'tempfile')
self.mox.StubOutWithMock(gcl.upload, 'RealMain')
# These are not tested.
......@@ -40,9 +29,9 @@ class GclUnittest(GclTestsBase):
def testMembersChanged(self):
self.mox.ReplayAll()
members = [
'CODEREVIEW_SETTINGS', 'CODEREVIEW_SETTINGS_FILE',
'CODEREVIEW_SETTINGS', 'CODEREVIEW_SETTINGS_FILE',
'Change', 'ChangeInfo', 'Changes', 'Commit',
'DEFAULT_LINT_IGNORE_REGEX', 'DEFAULT_LINT_REGEX',
'DEFAULT_LINT_IGNORE_REGEX', 'DEFAULT_LINT_REGEX',
'DeleteEmptyChangeLists', 'DoPresubmitChecks',
'ErrorExit', 'FILES_CACHE', 'FilterFlag', 'GenerateChangeName',
'GenerateDiff',
......@@ -134,7 +123,6 @@ class GclUnittest(GclTestsBase):
pass
def testHelp(self):
self.mox.StubOutWithMock(gcl.sys, 'stdout')
gcl.sys.stdout.write(mox.StrContains('GCL is a wrapper for Subversion'))
gcl.sys.stdout.write('\n')
self.mox.ReplayAll()
......@@ -364,4 +352,5 @@ class UploadCLUnittest(GclTestsBase):
if __name__ == '__main__':
import unittest
unittest.main()
This diff is collapsed.
......@@ -22,24 +22,10 @@ import __builtin__
import StringIO
import gclient
from super_mox import mox, SuperMoxTestBase
class IsOneOf(mox.Comparator):
def __init__(self, keys):
self._keys = keys
def equals(self, rhs):
return rhs in self._keys
def __repr__(self):
return '<sequence or map containing \'%s\'>' % str(self._keys)
from super_mox import mox, IsOneOf, SuperMoxTestBase
class BaseTestCase(SuperMoxTestBase):
def setUp(self):
SuperMoxTestBase.setUp(self)
# Like unittest's assertRaises, but checks for Gclient.Error.
def assertRaisesError(self, msg, fn, *args, **kwargs):
try:
......@@ -56,12 +42,6 @@ class GClientBaseTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
self.mox.StubOutWithMock(gclient.os.path, 'exists')
self.mox.StubOutWithMock(gclient.os.path, 'isfile')
self.mox.StubOutWithMock(gclient.os.path, 'isdir')
self.mox.StubOutWithMock(gclient.os, 'remove')
self.mox.StubOutWithMock(gclient.sys, 'stdout')
self.mox.StubOutWithMock(gclient.gclient_utils, 'subprocess')
# These are not tested.
self.mox.StubOutWithMock(gclient.gclient_utils, 'FileRead')
self.mox.StubOutWithMock(gclient.gclient_utils, 'FileWrite')
......@@ -69,7 +49,6 @@ class GClientBaseTestCase(BaseTestCase):
self.mox.StubOutWithMock(gclient.gclient_utils, 'RemoveDirectory')
# Mock them to be sure nothing bad happens.
self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVN')
self._CaptureSVNInfo = gclient.gclient_scm.CaptureSVNInfo
self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVNInfo')
self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVNStatus')
self.mox.StubOutWithMock(gclient.gclient_scm, 'RunSVN')
......@@ -380,10 +359,11 @@ class GClientClassTestCase(GclientTestCase):
def testLoadCurrentConfig(self):
options = self.Options()
path = gclient.os.path.realpath(self.root_dir)
gclient.os.path.exists(gclient.os.path.join(path, options.config_filename)
gclient.os.path.realpath(self.root_dir).AndReturn(self.root_dir)
gclient.os.path.exists(
gclient.os.path.join(self.root_dir, options.config_filename)
).AndReturn(True)
gclient.GClient(path, options).AndReturn(gclient.GClient)
gclient.GClient(self.root_dir, options).AndReturn(gclient.GClient)
gclient.GClient._LoadConfig()
self.mox.ReplayAll()
......@@ -1089,7 +1069,6 @@ deps = {
class SubprocessCallAndFilterTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
self.mox.StubOutWithMock(gclient.gclient_utils, 'subprocess')
self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVN')
def testSubprocessCallAndFilter(self):
......
......@@ -5,19 +5,15 @@
"""Unit tests for presubmit_support.py and presubmit_canned_checks.py."""
import exceptions
import os
import StringIO
import unittest
# Local imports
import presubmit_support as presubmit
import presubmit_canned_checks
import super_mox
from super_mox import mox
from super_mox import mox, SuperMoxTestBase
class PresubmitTestsBase(super_mox.SuperMoxTestBase):
class PresubmitTestsBase(SuperMoxTestBase):
"""Setups and tear downs the mocks but doesn't test anything as-is."""
presubmit_text = """
def CheckChangeOnUpload(input_api, output_api):
......@@ -37,33 +33,17 @@ def GetPreferredTrySlaves():
"""
def setUp(self):
super_mox.SuperMoxTestBase.setUp(self)
self.mox.StubOutWithMock(presubmit, 'warnings')
# Stub out 'os' but keep os.path.commonprefix/dirname/join/normpath/splitext
# and os.sep.
os_sep = presubmit.os.sep
os_path_commonprefix = presubmit.os.path.commonprefix
os_path_dirname = presubmit.os.path.dirname
os_path_join = presubmit.os.path.join
os_path_normpath = presubmit.os.path.normpath
os_path_splitext = presubmit.os.path.splitext
self.mox.StubOutWithMock(presubmit, 'os')
self.mox.StubOutWithMock(presubmit.os, 'path')
presubmit.os.sep = os_sep
presubmit.os.path.join = os_path_join
presubmit.os.path.dirname = os_path_dirname
presubmit.os.path.normpath = os_path_normpath
presubmit.os.path.splitext = os_path_splitext
SuperMoxTestBase.setUp(self)
self.mox.StubOutWithMock(presubmit, 'random')
self.mox.StubOutWithMock(presubmit, 'sys')
self.mox.StubOutWithMock(presubmit, 'warnings')
presubmit._ASKED_FOR_FEEDBACK = False
presubmit.os.path.commonprefix = os_path_commonprefix
self.fake_root_dir = self.RootDir()
# Special mocks.
def MockAbsPath(f):
return f
def MockChdir(f):
return None
# SuperMoxTestBase already mock these but simplify our life.
presubmit.os.path.abspath = MockAbsPath
presubmit.os.getcwd = self.RootDir
presubmit.os.chdir = MockChdir
......@@ -281,13 +261,13 @@ class PresubmitUnittest(PresubmitTestsBase):
fake_presubmit
))
self.assertRaises(exceptions.RuntimeError,
self.assertRaises(presubmit.exceptions.RuntimeError,
executer.ExecPresubmitScript,
'def CheckChangeOnCommit(input_api, output_api):\n'
' return "foo"',
fake_presubmit)
self.assertRaises(exceptions.RuntimeError,
self.assertRaises(presubmit.exceptions.RuntimeError,
executer.ExecPresubmitScript,
'def CheckChangeOnCommit(input_api, output_api):\n'
' return ["foo"]',
......@@ -424,7 +404,7 @@ def CheckChangeOnCommit(input_api, output_api):
def testDirectoryHandling(self):
files = [
['A', 'isdir'],
['A', os.path.join('isdir', 'blat.cc')],
['A', presubmit.os.path.join('isdir', 'blat.cc')],
]
isdir = presubmit.os.path.join(self.fake_root_dir, 'isdir')
blat = presubmit.os.path.join(isdir, 'blat.cc')
......@@ -501,7 +481,7 @@ def CheckChangeOnCommit(input_api, output_api):
not_list_result1 = "'foo'"
not_list_result2 = "('a', 'tuple')"
for result in starts_with_space_result, not_list_result1, not_list_result2:
self.assertRaises(exceptions.RuntimeError,
self.assertRaises(presubmit.exceptions.RuntimeError,
executer.ExecPresubmitScript,
self.presubmit_tryslave % result)
......@@ -544,6 +524,8 @@ def CheckChangeOnCommit(input_api, output_api):
output))
def testMain(self):
# OptParser calls presubmit.os.path.exists and is a pain when mocked.
self.UnMock(presubmit.os.path, 'exists')
self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks')
self.mox.StubOutWithMock(presubmit, 'ParseFiles')
presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.git')
......@@ -1477,4 +1459,5 @@ class CannedChecksUnittest(PresubmitTestsBase):
if __name__ == '__main__':
import unittest
unittest.main()
......@@ -5,25 +5,17 @@
"""Unit tests for revert.py."""
import os
import unittest
# Local imports
import revert
import super_mox
from super_mox import mox
from super_mox import mox, SuperMoxTestBase
class RevertTestsBase(super_mox.SuperMoxTestBase):
class RevertTestsBase(SuperMoxTestBase):
"""Setups and tear downs the mocks but doesn't test anything as-is."""
def setUp(self):
super_mox.SuperMoxTestBase.setUp(self)
SuperMoxTestBase.setUp(self)
self.mox.StubOutWithMock(revert, 'gcl')
self.mox.StubOutWithMock(revert, 'gclient')
self.mox.StubOutWithMock(revert, 'gclient_scm')
self.mox.StubOutWithMock(revert, 'os')
self.mox.StubOutWithMock(revert.os, 'path')
self.mox.StubOutWithMock(revert.sys, 'stdout')
# These functions are not tested.
self.mox.StubOutWithMock(revert, 'GetRepoBase')
......@@ -47,14 +39,16 @@ class RevertMainUnittest(RevertTestsBase):
def setUp(self):
RevertTestsBase.setUp(self)
self.mox.StubOutWithMock(revert, 'gcl')
self.mox.StubOutWithMock(revert, 'os')
self.mox.StubOutWithMock(revert.os, 'path')
self.mox.StubOutWithMock(revert, 'sys')
self.mox.StubOutWithMock(revert, 'Revert')
self.fake_root = '/revert/RevertMainUnittest/ShouldntExist'
def testMain(self):
revert.gcl.GetInfoDir().AndReturn('foo')
revert.os.path.exists('foo').AndReturn(True)
# OptParser calls revert.os.path.exists and is a pain when mocked.
self.UnMock(revert.os.path, 'exists')
revert.gcl.GetInfoDir().AndReturn(self.fake_root)
#revert.os.path.exists(self.fake_root).AndReturn(True)
revert.os.mkdir(self.fake_root)
revert.gcl.GetInfoDir().AndReturn(self.fake_root)
revert.Revert([42, 23], True, True, False, 'bleh', ['foo@example.com']
).AndReturn(31337)
self.mox.ReplayAll()
......@@ -113,4 +107,5 @@ M random_file
if __name__ == '__main__':
import unittest
unittest.main()
......@@ -5,13 +5,29 @@
"""Simplify unit tests based on pymox."""
import __builtin__
import os
import random
import string
from pymox import mox
import subprocess
import sys
from pymox import mox
class SuperMoxTestBase(mox.MoxTestBase):
class IsOneOf(mox.Comparator):
def __init__(self, keys):
self._keys = keys
def equals(self, rhs):
return rhs in self._keys
def __repr__(self):
return '<sequence or map containing \'%s\'>' % str(self._keys)
class SuperMoxBaseTestBase(mox.MoxTestBase):
"""Base class with some additional functionalities. People will usually want
to use SuperMoxTestBase instead."""
# Backup the separator in case it gets mocked
_OS_SEP = os.sep
_RANDOM_CHOICE = random.choice
......@@ -57,3 +73,64 @@ class SuperMoxTestBase(mox.MoxTestBase):
[i for i in expected_members if i not in actual_members])
print 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
class SuperMoxTestBase(SuperMoxBaseTestBase):
def setUp(self):
"""Patch a few functions with know side-effects."""
SuperMoxBaseTestBase.setUp(self)
#self.mox.StubOutWithMock(__builtin__, 'open')
self.mox.StubOutWithMock(os, 'chdir')
self.mox.StubOutWithMock(os, 'chown')
self.mox.StubOutWithMock(os, 'close')
#self.mox.StubOutWithMock(os, 'closerange')
self.mox.StubOutWithMock(os, 'dup')
self.mox.StubOutWithMock(os, 'dup2')
self.mox.StubOutWithMock(os, 'fchdir')
#self.mox.StubOutWithMock(os, 'fchmod')
#self.mox.StubOutWithMock(os, 'fchown')
self.mox.StubOutWithMock(os, 'fdopen')
self.mox.StubOutWithMock(os, 'getcwd')
self.mox.StubOutWithMock(os, 'getpid')
self.mox.StubOutWithMock(os, 'lseek')
self.mox.StubOutWithMock(os, 'makedirs')
self.mox.StubOutWithMock(os, 'mkdir')
self.mox.StubOutWithMock(os, 'open')
self.mox.StubOutWithMock(os, 'popen')
self.mox.StubOutWithMock(os, 'popen2')
self.mox.StubOutWithMock(os, 'popen3')
self.mox.StubOutWithMock(os, 'popen4')
self.mox.StubOutWithMock(os, 'read')
self.mox.StubOutWithMock(os, 'remove')
self.mox.StubOutWithMock(os, 'removedirs')
self.mox.StubOutWithMock(os, 'rename')
self.mox.StubOutWithMock(os, 'renames')
self.mox.StubOutWithMock(os, 'rmdir')
self.mox.StubOutWithMock(os, 'symlink')
self.mox.StubOutWithMock(os, 'system')
self.mox.StubOutWithMock(os, 'tmpfile')
self.mox.StubOutWithMock(os, 'walk')
self.mox.StubOutWithMock(os, 'write')
self.mox.StubOutWithMock(os.path, 'abspath')
self.mox.StubOutWithMock(os.path, 'exists')
self.mox.StubOutWithMock(os.path, 'getsize')
self.mox.StubOutWithMock(os.path, 'isdir')
self.mox.StubOutWithMock(os.path, 'isfile')
self.mox.StubOutWithMock(os.path, 'islink')
self.mox.StubOutWithMock(os.path, 'ismount')
self.mox.StubOutWithMock(os.path, 'lexists')
self.mox.StubOutWithMock(os.path, 'realpath')
self.mox.StubOutWithMock(os.path, 'samefile')
self.mox.StubOutWithMock(os.path, 'walk')
self.mox.StubOutWithMock(subprocess, 'call')
self.mox.StubOutWithMock(subprocess, 'Popen')
#self.mox.StubOutWithMock(sys, 'stderr')
self.mox.StubOutWithMock(sys, 'stdin')
self.mox.StubOutWithMock(sys, 'stdout')
......@@ -6,17 +6,13 @@
"""Unit tests for trychange.py."""
import optparse
import unittest
# Local imports
import gcl
import super_mox
import trychange
import upload
from super_mox import mox
from super_mox import mox, SuperMoxTestBase
class TryChangeTestsBase(super_mox.SuperMoxTestBase):
class TryChangeTestsBase(SuperMoxTestBase):
"""Setups and tear downs the mocks but doesn't test anything as-is."""
pass
......@@ -40,14 +36,15 @@ class TryChangeUnittest(TryChangeTestsBase):
class SVNUnittest(TryChangeTestsBase):
"""trychange.SVN tests."""
def setUp(self):
SuperMoxTestBase.setUp(self)
self.fake_root = '/fake_root'
self.expected_files = ['foo.txt', 'bar.txt']
change_info = gcl.ChangeInfo('test_change', 0, 0, 'desc',
[('M', f) for f in self.expected_files],
self.fake_root)
change_info = trychange.gcl.ChangeInfo(
'test_change', 0, 0, 'desc',
[('M', f) for f in self.expected_files],
self.fake_root)
self.svn = trychange.SVN(None)
self.svn.change_info = change_info
super_mox.SuperMoxTestBase.setUp(self)
def testMembersChanged(self):
members = [
......@@ -69,13 +66,13 @@ class SVNUnittest(TryChangeTestsBase):
class GITUnittest(TryChangeTestsBase):
"""trychange.GIT tests."""
def setUp(self):
self.fake_root = gcl.os.path.join(gcl.os.path.dirname(__file__),
'fake_root')
self.fake_root = trychange.os.path.join(
trychange.os.path.dirname(__file__), 'fake_root')
self.expected_files = ['foo.txt', 'bar.txt']
options = optparse.Values()
options.files = self.expected_files
self.git = trychange.GIT(options)
super_mox.SuperMoxTestBase.setUp(self)
SuperMoxTestBase.setUp(self)
def testMembersChanged(self):
members = [
......@@ -90,12 +87,14 @@ class GITUnittest(TryChangeTestsBase):
self.assertEqual(self.git.GetFileNames(), self.expected_files)
def testGetLocalRoot(self):
self.mox.StubOutWithMock(upload, 'RunShell')
upload.RunShell(['git', 'rev-parse', '--show-cdup']).AndReturn(
self.mox.StubOutWithMock(trychange.upload, 'RunShell')
trychange.upload.RunShell(['git', 'rev-parse', '--show-cdup']).AndReturn(
self.fake_root)
trychange.os.path.abspath(self.fake_root).AndReturn(self.fake_root)
self.mox.ReplayAll()
self.assertEqual(self.git.GetLocalRoot(), self.fake_root)
if __name__ == '__main__':
import unittest
unittest.main()
......@@ -5,8 +5,6 @@
"""Unit tests for watchlists.py."""
import os
import unittest
import super_mox
import watchlists
......@@ -142,8 +140,8 @@ class WatchlistsTest(super_mox.SuperMoxTestBase):
'browser': %s,
},
} """ % watchers
saved_sep = os.sep
os.sep = '\\' # to pose as win32
saved_sep = watchlists.os.sep
watchlists.os.sep = '\\' # to pose as win32
watchlists.Watchlists._HasWatchlistsFile().AndReturn(True)
watchlists.Watchlists._ContentsOfWatchlistsFile().AndReturn(contents)
self.mox.ReplayAll()
......@@ -151,9 +149,10 @@ class WatchlistsTest(super_mox.SuperMoxTestBase):
wl = watchlists.Watchlists(r'a\path')
returned_watchers = wl.GetWatchersForPaths(
[r'chrome\browser\renderer_host\render_widget_host.h'])
os.sep = saved_sep # revert back os.sep before asserts
watchlists.os.sep = saved_sep # revert back os.sep before asserts
self.assertEqual(returned_watchers, watchers)
if __name__ == '__main__':
import unittest
unittest.main()
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