diff --git a/tests/gcl_unittest.py b/tests/gcl_unittest.py index f4dbf34ad..739840bb7 100755 --- a/tests/gcl_unittest.py +++ b/tests/gcl_unittest.py @@ -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() diff --git a/tests/gclient_scm_test.py b/tests/gclient_scm_test.py index 761c3b1eb..e61101073 100644 --- a/tests/gclient_scm_test.py +++ b/tests/gclient_scm_test.py @@ -16,20 +16,33 @@ """Unit tests for gclient_scm.py.""" -import os import shutil -import subprocess +# Import it before super_mox to keep a valid reference. +from subprocess import Popen, PIPE, STDOUT import tempfile -import unittest -import gclient import gclient_scm -import gclient_test -import gclient_utils -from super_mox import mox +from gclient_test import BaseTestCase as GCBaseTestCase +from super_mox import mox, SuperMoxBaseTestBase -class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): +class BaseTestCase(GCBaseTestCase): + def setUp(self): + GCBaseTestCase.setUp(self) + self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead') + self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite') + self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'SubprocessCall') + self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'RemoveDirectory') + self._CaptureSVNInfo = gclient_scm.CaptureSVNInfo + self.mox.StubOutWithMock(gclient_scm, 'CaptureSVN') + self.mox.StubOutWithMock(gclient_scm, 'CaptureSVNInfo') + self.mox.StubOutWithMock(gclient_scm, 'CaptureSVNStatus') + self.mox.StubOutWithMock(gclient_scm, 'RunSVN') + self.mox.StubOutWithMock(gclient_scm, 'RunSVNAndGetFileList') + self._scm_wrapper = gclient_scm.CreateSCM + + +class SVNWrapperTestCase(BaseTestCase): class OptionsObject(object): def __init__(self, test_case, verbose=False, revision=None): self.verbose = verbose @@ -39,8 +52,11 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): self.force = False self.nohooks = False + def Options(self, *args, **kwargs): + return self.OptionsObject(self, *args, **kwargs) + def setUp(self): - gclient_test.GClientBaseTestCase.setUp(self) + BaseTestCase.setUp(self) self.root_dir = self.Dir() self.args = self.Args() self.url = self.Url() @@ -72,8 +88,8 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): def testRunCommandException(self): options = self.Options(verbose=False) - file_path = os.path.join(self.root_dir, self.relpath, '.git') - gclient.os.path.exists(file_path).AndReturn(False) + file_path = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') + gclient_scm.os.path.exists(file_path).AndReturn(False) self.mox.ReplayAll() scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, @@ -88,13 +104,14 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): def testRevertMissing(self): options = self.Options(verbose=True) - base_path = os.path.join(self.root_dir, self.relpath) - gclient.os.path.isdir(base_path).AndReturn(False) + base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) + gclient_scm.os.path.isdir(base_path).AndReturn(False) # It'll to a checkout instead. - gclient.os.path.exists(os.path.join(base_path, '.git')).AndReturn(False) + gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git') + ).AndReturn(False) print("\n_____ %s is missing, synching instead" % self.relpath) # Checkout. - gclient.os.path.exists(base_path).AndReturn(False) + gclient_scm.os.path.exists(base_path).AndReturn(False) files_list = self.mox.CreateMockAnything() gclient_scm.RunSVNAndGetFileList(options, ['checkout', self.url, base_path], self.root_dir, files_list) @@ -106,8 +123,8 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): def testRevertNone(self): options = self.Options(verbose=True) - base_path = os.path.join(self.root_dir, self.relpath) - gclient.os.path.isdir(base_path).AndReturn(True) + base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) + gclient_scm.os.path.isdir(base_path).AndReturn(True) gclient_scm.CaptureSVNStatus(base_path).AndReturn([]) gclient_scm.RunSVNAndGetFileList(options, ['update', '--revision', 'BASE'], base_path, mox.IgnoreArg()) @@ -120,14 +137,14 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): def testRevert2Files(self): options = self.Options(verbose=True) - base_path = os.path.join(self.root_dir, self.relpath) - gclient.os.path.isdir(base_path).AndReturn(True) + base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) + gclient_scm.os.path.isdir(base_path).AndReturn(True) items = [ ('M ', 'a'), ('A ', 'b'), ] - file_path1 = os.path.join(base_path, 'a') - file_path2 = os.path.join(base_path, 'b') + file_path1 = gclient_scm.os.path.join(base_path, 'a') + file_path2 = gclient_scm.os.path.join(base_path, 'b') gclient_scm.CaptureSVNStatus(base_path).AndReturn(items) gclient_scm.os.path.exists(file_path1).AndReturn(True) gclient_scm.os.path.isfile(file_path1).AndReturn(True) @@ -137,8 +154,8 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): gclient_scm.os.remove(file_path2) gclient_scm.RunSVNAndGetFileList(options, ['update', '--revision', 'BASE'], base_path, mox.IgnoreArg()) - print(os.path.join(base_path, 'a')) - print(os.path.join(base_path, 'b')) + print(gclient_scm.os.path.join(base_path, 'a')) + print(gclient_scm.os.path.join(base_path, 'b')) self.mox.ReplayAll() scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, @@ -148,18 +165,18 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): def testRevertDirectory(self): options = self.Options(verbose=True) - base_path = os.path.join(self.root_dir, self.relpath) - gclient.os.path.isdir(base_path).AndReturn(True) + base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) + gclient_scm.os.path.isdir(base_path).AndReturn(True) items = [ ('~ ', 'a'), ] gclient_scm.CaptureSVNStatus(base_path).AndReturn(items) - file_path = os.path.join(base_path, 'a') + file_path = gclient_scm.os.path.join(base_path, 'a') print(file_path) gclient_scm.os.path.exists(file_path).AndReturn(True) gclient_scm.os.path.isfile(file_path).AndReturn(False) gclient_scm.os.path.isdir(file_path).AndReturn(True) - gclient_utils.RemoveDirectory(file_path) + gclient_scm.gclient_utils.RemoveDirectory(file_path) file_list1 = [] gclient_scm.RunSVNAndGetFileList(options, ['update', '--revision', 'BASE'], base_path, mox.IgnoreArg()) @@ -172,8 +189,8 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): def testStatus(self): options = self.Options(verbose=True) - base_path = os.path.join(self.root_dir, self.relpath) - gclient.os.path.isdir(base_path).AndReturn(True) + base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) + gclient_scm.os.path.isdir(base_path).AndReturn(True) gclient_scm.RunSVNAndGetFileList(options, ['status'] + self.args, base_path, []).AndReturn(None) @@ -188,17 +205,18 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): # TODO(maruel): TEST RELOCATE!!! def testUpdateCheckout(self): options = self.Options(verbose=True) - base_path = os.path.join(self.root_dir, self.relpath) - file_info = gclient_utils.PrintableObject() + base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) + file_info = gclient_scm.gclient_utils.PrintableObject() file_info.root = 'blah' file_info.url = self.url file_info.uuid = 'ABC' file_info.revision = 42 - gclient.os.path.exists(os.path.join(base_path, '.git')).AndReturn(False) + gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git') + ).AndReturn(False) # Checkout. - gclient.os.path.exists(base_path).AndReturn(False) + gclient_scm.os.path.exists(base_path).AndReturn(False) files_list = self.mox.CreateMockAnything() - gclient_scm.RunSVNAndGetFileList(options, ['checkout', self.url, + gclient_scm.RunSVNAndGetFileList(options, ['checkout', self.url, base_path], self.root_dir, files_list) self.mox.ReplayAll() scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, @@ -207,7 +225,7 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): def testUpdateUpdate(self): options = self.Options(verbose=True) - base_path = os.path.join(self.root_dir, self.relpath) + base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) options.force = True options.nohooks = False file_info = { @@ -216,10 +234,11 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): 'UUID': 'ABC', 'Revision': 42, } - gclient.os.path.exists(os.path.join(base_path, '.git')).AndReturn(False) + gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git') + ).AndReturn(False) # Checkout or update. - gclient.os.path.exists(base_path).AndReturn(True) - gclient_scm.CaptureSVNInfo(os.path.join(base_path, "."), '.' + gclient_scm.os.path.exists(base_path).AndReturn(True) + gclient_scm.CaptureSVNInfo(gclient_scm.os.path.join(base_path, "."), '.' ).AndReturn(file_info) # Cheat a bit here. gclient_scm.CaptureSVNInfo(file_info['URL'], '.').AndReturn(file_info) @@ -238,8 +257,8 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): def testUpdateGit(self): options = self.Options(verbose=True) - file_path = os.path.join(self.root_dir, self.relpath, '.git') - gclient.os.path.exists(file_path).AndReturn(True) + file_path = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') + gclient_scm.os.path.exists(file_path).AndReturn(True) print("________ found .git directory; skipping %s" % self.relpath) self.mox.ReplayAll() @@ -348,8 +367,9 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): """ % (self.url, self.root_dir) - gclient_scm.CaptureSVN(['info', '--xml', - self.url], os.getcwd()).AndReturn(xml_text) + gclient_scm.os.getcwd().AndReturn('bleh') + gclient_scm.CaptureSVN(['info', '--xml', self.url], 'bleh' + ).AndReturn(xml_text) self.mox.ReplayAll() scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, relpath=self.relpath) @@ -357,7 +377,8 @@ class SVNWrapperTestCase(gclient_test.GClientBaseTestCase): self.assertEqual(rev_info, '35') -class GitWrapperTestCase(gclient_test.GClientBaseTestCase): +class GitWrapperTestCase(SuperMoxBaseTestBase): + """This class doesn't use pymox.""" class OptionsObject(object): def __init__(self, test_case, verbose=False, revision=None): self.verbose = verbose @@ -417,30 +438,29 @@ from :3 def CreateGitRepo(self, git_import, path): try: - subprocess.Popen(['git', 'init'], stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, cwd=path).communicate() - except WindowsError: + Popen(['git', 'init'], stdout=PIPE, stderr=STDOUT, + cwd=path).communicate() + except OSError: # git is not available, skip this test. return False - subprocess.Popen(['git', 'fast-import'], stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - cwd=path).communicate(input=git_import) - subprocess.Popen(['git', 'checkout'], stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, cwd=path).communicate() + Popen(['git', 'fast-import'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, + cwd=path).communicate(input=git_import) + Popen(['git', 'checkout'], stdout=PIPE, stderr=STDOUT, + cwd=path).communicate() return True def setUp(self): - gclient_test.BaseTestCase.setUp(self) self.args = self.Args() self.url = 'git://foo' self.root_dir = tempfile.mkdtemp() self.relpath = '.' - self.base_path = os.path.join(self.root_dir, self.relpath) + self.base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) + SuperMoxBaseTestBase.setUp(self) def tearDown(self): + SuperMoxBaseTestBase.tearDown(self) shutil.rmtree(self.root_dir) - gclient_test.BaseTestCase.tearDown(self) def testDir(self): members = [ @@ -456,8 +476,8 @@ from :3 if not self.enabled: return options = self.Options() - file_path = os.path.join(self.base_path, 'a') - os.remove(file_path) + file_path = gclient_scm.os.path.join(self.base_path, 'a') + gclient_scm.os.remove(file_path) scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, relpath=self.relpath) file_list = [] @@ -484,7 +504,7 @@ from :3 if not self.enabled: return options = self.Options() - file_path = os.path.join(self.base_path, 'a') + file_path = gclient_scm.os.path.join(self.base_path, 'a') open(file_path, 'a').writelines('touched\n') scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, relpath=self.relpath) @@ -501,12 +521,12 @@ from :3 if not self.enabled: return options = self.Options() - file_path = os.path.join(self.base_path, 'c') + file_path = gclient_scm.os.path.join(self.base_path, 'c') f = open(file_path, 'w') f.writelines('new\n') f.close() - subprocess.Popen(['git', 'add', 'c'], stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, cwd=self.base_path).communicate() + Popen(['git', 'add', 'c'], stdout=PIPE, + stderr=STDOUT, cwd=self.base_path).communicate() scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, relpath=self.relpath) file_list = [] @@ -522,7 +542,7 @@ from :3 if not self.enabled: return options = self.Options() - file_path = os.path.join(self.base_path, 'a') + file_path = gclient_scm.os.path.join(self.base_path, 'a') open(file_path, 'a').writelines('touched\n') scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, relpath=self.relpath) @@ -536,14 +556,15 @@ from :3 options = self.Options() expected_file_list = [] for f in ['a', 'b']: - file_path = os.path.join(self.base_path, f) + file_path = gclient_scm.os.path.join(self.base_path, f) open(file_path, 'a').writelines('touched\n') expected_file_list.extend([file_path]) scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, relpath=self.relpath) file_list = [] scm.status(options, self.args, file_list) - expected_file_list = [os.path.join(self.base_path, x) for x in ['a', 'b']] + expected_file_list = [gclient_scm.os.path.join(self.base_path, x) + for x in ['a', 'b']] self.assertEquals(sorted(file_list), expected_file_list) def testUpdateCheckout(self): @@ -552,15 +573,16 @@ from :3 options = self.Options(verbose=True) root_dir = tempfile.mkdtemp() relpath = 'foo' - base_path = os.path.join(root_dir, relpath) - url = os.path.join(self.root_dir, self.relpath, '.git') + base_path = gclient_scm.os.path.join(root_dir, relpath) + url = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') try: scm = gclient_scm.CreateSCM(url=url, root_dir=root_dir, relpath=relpath) file_list = [] scm.update(options, (), file_list) self.assertEquals(len(file_list), 2) - self.assert_(os.path.isfile(os.path.join(base_path, 'a'))) + self.assert_(gclient_scm.os.path.isfile( + gclient_scm.os.path.join(base_path, 'a'))) self.assertEquals(scm.revinfo(options, (), None), '069c602044c5388d2d15c3f875b057c852003458') finally: @@ -570,7 +592,8 @@ from :3 if not self.enabled: return options = self.Options() - expected_file_list = [os.path.join(self.base_path, x) for x in ['a', 'b']] + expected_file_list = [gclient_scm.os.path.join(self.base_path, x) + for x in ['a', 'b']] scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, relpath=self.relpath) file_list = [] @@ -589,16 +612,18 @@ from :3 self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') -class RunSVNTestCase(gclient_test.BaseTestCase): +class RunSVNTestCase(BaseTestCase): def testRunSVN(self): + self.UnMock(gclient_scm, 'RunSVN') param2 = 'bleh' - self.mox.StubOutWithMock(gclient_utils, 'SubprocessCall') - gclient_utils.SubprocessCall(['svn', 'foo', 'bar'], param2).AndReturn(None) + gclient_scm.gclient_utils.SubprocessCall(['svn', 'foo', 'bar'], + param2).AndReturn(None) self.mox.ReplayAll() gclient_scm.RunSVN(['foo', 'bar'], param2) if __name__ == '__main__': + import unittest unittest.main() # vim: ts=2:sw=2:tw=80:et: diff --git a/tests/gclient_test.py b/tests/gclient_test.py index aa4e0e0ea..d335d4266 100755 --- a/tests/gclient_test.py +++ b/tests/gclient_test.py @@ -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 '' % 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): diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index 132f6aee0..3149c9738 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -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() diff --git a/tests/revert_unittest.py b/tests/revert_unittest.py index 477eb2ada..9296d8586 100644 --- a/tests/revert_unittest.py +++ b/tests/revert_unittest.py @@ -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() diff --git a/tests/super_mox.py b/tests/super_mox.py index fdd123c92..3da065394 100644 --- a/tests/super_mox.py +++ b/tests/super_mox.py @@ -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 '' % 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') diff --git a/tests/trychange_unittest.py b/tests/trychange_unittest.py index 947f7227e..24550dda3 100644 --- a/tests/trychange_unittest.py +++ b/tests/trychange_unittest.py @@ -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() diff --git a/tests/watchlists_unittest.py b/tests/watchlists_unittest.py index 968de038f..22afe2feb 100755 --- a/tests/watchlists_unittest.py +++ b/tests/watchlists_unittest.py @@ -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()