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

Fix error printing in gcl.GetRepositoryRoot() and fix its unit tests.

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@16499 0039d316-1c4b-4281-b951-d872f2087c98
parent 003c269b
...@@ -101,8 +101,8 @@ def GetRepositoryRoot(): ...@@ -101,8 +101,8 @@ def GetRepositoryRoot():
repository_root = os.getcwd() repository_root = os.getcwd()
while True: while True:
parent = os.path.dirname(repository_root) parent = os.path.dirname(repository_root)
if (gclient.CaptureSVNInfo(parent).get("Repository Root") != if (gclient.CaptureSVNInfo(parent, print_error=False).get(
cur_dir_repo_root): "Repository Root") != cur_dir_repo_root):
break break
repository_root = parent repository_root = parent
return repository_root return repository_root
......
...@@ -19,13 +19,20 @@ mox = __init__.mox ...@@ -19,13 +19,20 @@ mox = __init__.mox
class GclTestsBase(unittest.TestCase): class GclTestsBase(unittest.TestCase):
"""Setups and tear downs the mocks but doesn't test anything as-is.""" """Setups and tear downs the mocks but doesn't test anything as-is."""
def setUp(self): def setUp(self):
self.mox = mox.Mox()
def RunShellMock(filename): def RunShellMock(filename):
return filename return filename
self._RunShell = gcl.RunShell self._RunShell = gcl.RunShell
gcl.RunShell = RunShellMock gcl.RunShell = RunShellMock
self._gcl_gclient_CaptureSVNInfo = gcl.gclient.CaptureSVNInfo
gcl.gclient.CaptureSVNInfo = self.mox.CreateMockAnything()
self._gcl_os_getcwd = gcl.os.getcwd
gcl.os.getcwd = self.mox.CreateMockAnything()
def tearDown(self): def tearDown(self):
gcl.RunShell = self._RunShell gcl.RunShell = self._RunShell
gcl.gclient.CaptureSVNInfo = self._gcl_gclient_CaptureSVNInfo
gcl.os.getcwd = self._gcl_os_getcwd
def compareMembers(self, object, members): def compareMembers(self, object, members):
"""If you add a member, be sure to add the relevant test!""" """If you add a member, be sure to add the relevant test!"""
...@@ -75,15 +82,32 @@ class GclUnittest(GclTestsBase): ...@@ -75,15 +82,32 @@ class GclUnittest(GclTestsBase):
finally: finally:
gcl.sys.stdout = old_stdout gcl.sys.stdout = old_stdout
def testGetRepositoryRoot(self): def testGetRepositoryRootNone(self):
try: gcl.repository_root = None
def RunShellMock(filename): gcl.os.getcwd().AndReturn("/bleh/prout")
return '<?xml version="1.0"?>\n<info>' result = {
gcl.RunShell = RunShellMock "Repository Root": ""
gcl.GetRepositoryRoot() }
except Exception,e: gcl.gclient.CaptureSVNInfo("/bleh/prout", print_error=False).AndReturn(
self.assertEquals(e.args[0], "gcl run outside of repository") result)
pass self.mox.ReplayAll()
self.assertRaises(Exception, gcl.GetRepositoryRoot)
self.mox.VerifyAll()
def testGetRepositoryRootGood(self):
gcl.repository_root = None
root_path = os.path.join('bleh', 'prout', 'pouet')
gcl.os.getcwd().AndReturn(root_path)
result1 = { "Repository Root": "Some root" }
gcl.gclient.CaptureSVNInfo(root_path, print_error=False).AndReturn(result1)
gcl.os.getcwd().AndReturn(root_path)
results2 = { "Repository Root": "A different root" }
gcl.gclient.CaptureSVNInfo(
os.path.dirname(root_path),
print_error=False).AndReturn(results2)
self.mox.ReplayAll()
self.assertEquals(gcl.GetRepositoryRoot(), root_path)
self.mox.VerifyAll()
class ChangeInfoUnittest(GclTestsBase): class ChangeInfoUnittest(GclTestsBase):
...@@ -110,13 +134,10 @@ class ChangeInfoUnittest(GclTestsBase): ...@@ -110,13 +134,10 @@ class ChangeInfoUnittest(GclTestsBase):
class UploadCLUnittest(GclTestsBase): class UploadCLUnittest(GclTestsBase):
def setUp(self): def setUp(self):
GclTestsBase.setUp(self) GclTestsBase.setUp(self)
self.mox = mox.Mox()
self._os_chdir = gcl.os.chdir self._os_chdir = gcl.os.chdir
gcl.os.chdir = self.mox.CreateMockAnything() gcl.os.chdir = self.mox.CreateMockAnything()
self._os_close = gcl.os.close self._os_close = gcl.os.close
gcl.os.close = self.mox.CreateMockAnything() gcl.os.close = self.mox.CreateMockAnything()
self._os_getcwd = gcl.os.getcwd
gcl.os.getcwd = self.mox.CreateMockAnything()
self._os_remove = gcl.os.remove self._os_remove = gcl.os.remove
gcl.os.remove = self.mox.CreateMockAnything() gcl.os.remove = self.mox.CreateMockAnything()
self._os_write = gcl.os.write self._os_write = gcl.os.write
...@@ -142,7 +163,6 @@ class UploadCLUnittest(GclTestsBase): ...@@ -142,7 +163,6 @@ class UploadCLUnittest(GclTestsBase):
GclTestsBase.tearDown(self) GclTestsBase.tearDown(self)
gcl.os.chdir = self._os_chdir gcl.os.chdir = self._os_chdir
gcl.os.close = self._os_close gcl.os.close = self._os_close
gcl.os.getcwd = self._os_getcwd
gcl.os.remove = self._os_remove gcl.os.remove = self._os_remove
gcl.os.write = self._os_write gcl.os.write = self._os_write
gcl.tempfile = self._tempfile gcl.tempfile = self._tempfile
......
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