| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Unit tests for gclient_scm.py.""" | 6 """Unit tests for gclient_scm.py.""" |
| 7 | 7 |
| 8 # pylint: disable=E1103 | 8 # pylint: disable=E1103 |
| 9 | 9 |
| 10 # Import before super_mox to keep valid references. | 10 # Import before super_mox to keep valid references. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 60 |
| 61 class BaseTestCase(GCBaseTestCase, SuperMoxTestBase): | 61 class BaseTestCase(GCBaseTestCase, SuperMoxTestBase): |
| 62 def setUp(self): | 62 def setUp(self): |
| 63 SuperMoxTestBase.setUp(self) | 63 SuperMoxTestBase.setUp(self) |
| 64 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'CheckCallAndFilter') | 64 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'CheckCallAndFilter') |
| 65 self.mox.StubOutWithMock(gclient_scm.gclient_utils, | 65 self.mox.StubOutWithMock(gclient_scm.gclient_utils, |
| 66 'CheckCallAndFilterAndHeader') | 66 'CheckCallAndFilterAndHeader') |
| 67 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead') | 67 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead') |
| 68 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite') | 68 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite') |
| 69 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'rmtree') | 69 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'rmtree') |
| 70 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'Capture') | |
| 71 self.mox.StubOutWithMock(gclient_scm.scm.SVN, '_CaptureInfo') | |
| 72 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureStatus') | |
| 73 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'RunAndGetFileList') | |
| 74 self.mox.StubOutWithMock(subprocess2, 'communicate') | 70 self.mox.StubOutWithMock(subprocess2, 'communicate') |
| 75 self.mox.StubOutWithMock(subprocess2, 'Popen') | 71 self.mox.StubOutWithMock(subprocess2, 'Popen') |
| 76 self._scm_wrapper = gclient_scm.CreateSCM | 72 self._scm_wrapper = gclient_scm.CreateSCM |
| 77 gclient_scm.scm.SVN.current_version = None | |
| 78 self._original_SVNBinaryExists = gclient_scm.SVNWrapper.BinaryExists | |
| 79 self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists | 73 self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists |
| 80 gclient_scm.SVNWrapper.BinaryExists = staticmethod(lambda : True) | |
| 81 gclient_scm.GitWrapper.BinaryExists = staticmethod(lambda : True) | 74 gclient_scm.GitWrapper.BinaryExists = staticmethod(lambda : True) |
| 82 # Absolute path of the fake checkout directory. | 75 # Absolute path of the fake checkout directory. |
| 83 self.base_path = join(self.root_dir, self.relpath) | 76 self.base_path = join(self.root_dir, self.relpath) |
| 84 | 77 |
| 85 def tearDown(self): | 78 def tearDown(self): |
| 86 SuperMoxTestBase.tearDown(self) | 79 SuperMoxTestBase.tearDown(self) |
| 87 gclient_scm.SVNWrapper.BinaryExists = self._original_SVNBinaryExists | |
| 88 gclient_scm.GitWrapper.BinaryExists = self._original_GitBinaryExists | 80 gclient_scm.GitWrapper.BinaryExists = self._original_GitBinaryExists |
| 89 | 81 |
| 90 | 82 |
| 91 class BasicTests(SuperMoxTestBase): | 83 class BasicTests(SuperMoxTestBase): |
| 92 def setUp(self): | 84 def setUp(self): |
| 93 SuperMoxTestBase.setUp(self) | 85 SuperMoxTestBase.setUp(self) |
| 94 | 86 |
| 95 def testGetFirstRemoteUrl(self): | 87 def testGetFirstRemoteUrl(self): |
| 96 REMOTE_STRINGS = [('remote.origin.url E:\\foo\\bar', 'E:\\foo\\bar'), | 88 REMOTE_STRINGS = [('remote.origin.url E:\\foo\\bar', 'E:\\foo\\bar'), |
| 97 ('remote.origin.url /b/foo/bar', '/b/foo/bar'), | 89 ('remote.origin.url /b/foo/bar', '/b/foo/bar'), |
| (...skipping 18 matching lines...) Expand all Loading... |
| 116 | 108 |
| 117 | 109 |
| 118 class BaseGitWrapperTestCase(GCBaseTestCase, StdoutCheck, TestCaseUtils, | 110 class BaseGitWrapperTestCase(GCBaseTestCase, StdoutCheck, TestCaseUtils, |
| 119 unittest.TestCase): | 111 unittest.TestCase): |
| 120 """This class doesn't use pymox.""" | 112 """This class doesn't use pymox.""" |
| 121 class OptionsObject(object): | 113 class OptionsObject(object): |
| 122 def __init__(self, verbose=False, revision=None): | 114 def __init__(self, verbose=False, revision=None): |
| 123 self.auto_rebase = False | 115 self.auto_rebase = False |
| 124 self.verbose = verbose | 116 self.verbose = verbose |
| 125 self.revision = revision | 117 self.revision = revision |
| 126 self.manually_grab_svn_rev = True | |
| 127 self.deps_os = None | 118 self.deps_os = None |
| 128 self.force = False | 119 self.force = False |
| 129 self.reset = False | 120 self.reset = False |
| 130 self.nohooks = False | 121 self.nohooks = False |
| 131 self.no_history = False | 122 self.no_history = False |
| 132 self.upstream = False | 123 self.upstream = False |
| 133 self.cache_dir = None | 124 self.cache_dir = None |
| 134 self.merge = False | 125 self.merge = False |
| 135 self.jobs = 1 | 126 self.jobs = 1 |
| 136 self.break_repo_locks = False | 127 self.break_repo_locks = False |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 unittest.TestCase.setUp(self) | 233 unittest.TestCase.setUp(self) |
| 243 self.url = 'git://foo' | 234 self.url = 'git://foo' |
| 244 # The .git suffix allows gclient_scm to recognize the dir as a git repo | 235 # The .git suffix allows gclient_scm to recognize the dir as a git repo |
| 245 # when cloning it locally | 236 # when cloning it locally |
| 246 self.root_dir = tempfile.mkdtemp('.git') | 237 self.root_dir = tempfile.mkdtemp('.git') |
| 247 self.relpath = '.' | 238 self.relpath = '.' |
| 248 self.base_path = join(self.root_dir, self.relpath) | 239 self.base_path = join(self.root_dir, self.relpath) |
| 249 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) | 240 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) |
| 250 StdoutCheck.setUp(self) | 241 StdoutCheck.setUp(self) |
| 251 self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists | 242 self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists |
| 252 self._original_SVNBinaryExists = gclient_scm.SVNWrapper.BinaryExists | |
| 253 gclient_scm.GitWrapper.BinaryExists = staticmethod(lambda : True) | 243 gclient_scm.GitWrapper.BinaryExists = staticmethod(lambda : True) |
| 254 gclient_scm.SVNWrapper.BinaryExists = staticmethod(lambda : True) | |
| 255 | 244 |
| 256 def tearDown(self): | 245 def tearDown(self): |
| 257 try: | 246 try: |
| 258 rmtree(self.root_dir) | 247 rmtree(self.root_dir) |
| 259 StdoutCheck.tearDown(self) | 248 StdoutCheck.tearDown(self) |
| 260 TestCaseUtils.tearDown(self) | 249 TestCaseUtils.tearDown(self) |
| 261 unittest.TestCase.tearDown(self) | 250 unittest.TestCase.tearDown(self) |
| 262 finally: | 251 finally: |
| 263 # TODO(maruel): Use auto_stub.TestCase. | 252 # TODO(maruel): Use auto_stub.TestCase. |
| 264 gclient_scm.GitWrapper.BinaryExists = self._original_GitBinaryExists | 253 gclient_scm.GitWrapper.BinaryExists = self._original_GitBinaryExists |
| 265 gclient_scm.SVNWrapper.BinaryExists = self._original_SVNBinaryExists | |
| 266 | 254 |
| 267 | 255 |
| 268 class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): | 256 class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): |
| 269 | 257 |
| 270 def testRevertMissing(self): | 258 def testRevertMissing(self): |
| 271 if not self.enabled: | 259 if not self.enabled: |
| 272 return | 260 return |
| 273 options = self.Options() | 261 options = self.Options() |
| 274 file_path = join(self.base_path, 'a') | 262 file_path = join(self.base_path, 'a') |
| 275 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 263 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 def tearDown(self): | 588 def tearDown(self): |
| 601 BaseTestCase.tearDown(self) | 589 BaseTestCase.tearDown(self) |
| 602 | 590 |
| 603 def testGetUsableRevGit(self): | 591 def testGetUsableRevGit(self): |
| 604 # pylint: disable=E1101 | 592 # pylint: disable=E1101 |
| 605 options = self.Options(verbose=True) | 593 options = self.Options(verbose=True) |
| 606 | 594 |
| 607 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsValidRevision', True) | 595 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsValidRevision', True) |
| 608 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=self.fake_hash_1 | 596 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=self.fake_hash_1 |
| 609 ).AndReturn(True) | 597 ).AndReturn(True) |
| 598 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev='1' |
| 599 ).AndReturn(False) |
| 600 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev='1' |
| 601 ).AndReturn(False) |
| 610 | 602 |
| 611 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsGitSvn', True) | 603 self.mox.StubOutWithMock(gclient_scm.GitWrapper, '_Fetch', True) |
| 612 gclient_scm.scm.GIT.IsGitSvn(cwd=self.base_path).MultipleTimes( | 604 # pylint: disable=no-value-for-parameter |
| 613 ).AndReturn(False) | 605 gclient_scm.GitWrapper._Fetch(options).AndReturn(None) |
| 614 | 606 |
| 615 gclient_scm.scm.os.path.isdir(self.base_path).AndReturn(True) | 607 gclient_scm.scm.os.path.isdir(self.base_path).AndReturn(True) |
| 616 gclient_scm.os.path.isdir(self.base_path).AndReturn(True) | 608 gclient_scm.os.path.isdir(self.base_path).AndReturn(True) |
| 617 | 609 |
| 618 self.mox.ReplayAll() | 610 self.mox.ReplayAll() |
| 619 | 611 |
| 620 git_scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 612 git_scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 621 relpath=self.relpath) | 613 relpath=self.relpath) |
| 622 # A [fake] git sha1 with a git repo should work (this is in the case that | 614 # A [fake] git sha1 with a git repo should work (this is in the case that |
| 623 # the LKGR gets flipped to git sha1's some day). | 615 # the LKGR gets flipped to git sha1's some day). |
| 624 self.assertEquals(git_scm.GetUsableRev(self.fake_hash_1, options), | 616 self.assertEquals(git_scm.GetUsableRev(self.fake_hash_1, options), |
| 625 self.fake_hash_1) | 617 self.fake_hash_1) |
| 626 # An SVN rev with an existing purely git repo should raise an exception. | 618 # An SVN rev with an existing purely git repo should raise an exception. |
| 627 self.assertRaises(gclient_scm.gclient_utils.Error, | 619 self.assertRaises(gclient_scm.gclient_utils.Error, |
| 628 git_scm.GetUsableRev, '1', options) | 620 git_scm.GetUsableRev, '1', options) |
| 629 | 621 |
| 630 def testGetUsableRevGitSvn(self): | |
| 631 # pylint: disable=E1101 | |
| 632 options = self.Options() | |
| 633 too_big = str(1e7) | |
| 634 | |
| 635 # Pretend like the git-svn repo's HEAD is at r2. | |
| 636 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'GetGitSvnHeadRev', True) | |
| 637 gclient_scm.scm.GIT.GetGitSvnHeadRev(cwd=self.base_path).MultipleTimes( | |
| 638 ).AndReturn(2) | |
| 639 | |
| 640 self.mox.StubOutWithMock( | |
| 641 gclient_scm.scm.GIT, 'GetBlessedSha1ForSvnRev', True) | |
| 642 # r1 -> first fake hash, r3 -> second fake hash. | |
| 643 gclient_scm.scm.GIT.GetBlessedSha1ForSvnRev(cwd=self.base_path, rev='1' | |
| 644 ).AndReturn(self.fake_hash_1) | |
| 645 gclient_scm.scm.GIT.GetBlessedSha1ForSvnRev(cwd=self.base_path, rev='3' | |
| 646 ).MultipleTimes().AndReturn(self.fake_hash_2) | |
| 647 | |
| 648 # Ensure that we call git svn fetch if our LKGR is > the git-svn HEAD rev. | |
| 649 self.mox.StubOutWithMock(gclient_scm.GitWrapper, '_Fetch', True) | |
| 650 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'Capture', True) | |
| 651 gclient_scm.scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'], | |
| 652 cwd=self.base_path).AndReturn('blah') | |
| 653 # pylint: disable=E1120 | |
| 654 gclient_scm.scm.GIT.Capture(['svn', 'fetch'], cwd=self.base_path) | |
| 655 error = subprocess2.CalledProcessError(1, 'cmd', '/cwd', 'stdout', 'stderr') | |
| 656 gclient_scm.scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'], | |
| 657 cwd=self.base_path).AndRaise(error) | |
| 658 gclient_scm.GitWrapper._Fetch(options) | |
| 659 gclient_scm.scm.GIT.Capture(['svn', 'fetch'], cwd=self.base_path) | |
| 660 gclient_scm.GitWrapper._Fetch(options) | |
| 661 | |
| 662 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsGitSvn', True) | |
| 663 gclient_scm.scm.GIT.IsGitSvn(cwd=self.base_path).MultipleTimes( | |
| 664 ).AndReturn(True) | |
| 665 | |
| 666 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsValidRevision', True) | |
| 667 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=self.fake_hash_1 | |
| 668 ).AndReturn(True) | |
| 669 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=too_big | |
| 670 ).MultipleTimes(2).AndReturn(False) | |
| 671 | |
| 672 gclient_scm.os.path.isdir(self.base_path).AndReturn(False) | |
| 673 gclient_scm.os.path.isdir(self.base_path).MultipleTimes().AndReturn(True) | |
| 674 | |
| 675 self.mox.ReplayAll() | |
| 676 | |
| 677 git_svn_scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
| 678 relpath=self.relpath) | |
| 679 # Without an existing checkout, this should fail. | |
| 680 # TODO(dbeam) Fix this. http://crbug.com/109184 | |
| 681 self.assertRaises(gclient_scm.gclient_utils.Error, | |
| 682 git_svn_scm.GetUsableRev, '1', options) | |
| 683 # Given an SVN revision with a git-svn checkout, it should be translated to | |
| 684 # a git sha1 and be usable. | |
| 685 self.assertEquals(git_svn_scm.GetUsableRev('1', options), | |
| 686 self.fake_hash_1) | |
| 687 # Our fake HEAD rev is r2, so this should call git fetch and git svn fetch | |
| 688 # to get more revs (pymox will complain if this doesn't happen). We mock an | |
| 689 # optimized checkout the first time, so this run should call git fetch. | |
| 690 self.assertEquals(git_svn_scm.GetUsableRev('3', options), | |
| 691 self.fake_hash_2) | |
| 692 # The time we pretend we're not optimized, so no git fetch should fire. | |
| 693 self.assertEquals(git_svn_scm.GetUsableRev('3', options), | |
| 694 self.fake_hash_2) | |
| 695 # Given a git sha1 with a git-svn checkout, it should be used as is. | |
| 696 self.assertEquals(git_svn_scm.GetUsableRev(self.fake_hash_1, options), | |
| 697 self.fake_hash_1) | |
| 698 # We currently check for seemingly valid SVN revisions by assuming 6 digit | |
| 699 # numbers, so assure that numeric revs >= 1000000 don't work. | |
| 700 self.assertRaises(gclient_scm.gclient_utils.Error, | |
| 701 git_svn_scm.GetUsableRev, too_big, options) | |
| 702 | |
| 703 def testUpdateNoDotGit(self): | 622 def testUpdateNoDotGit(self): |
| 704 options = self.Options() | 623 options = self.Options() |
| 705 | 624 |
| 706 gclient_scm.os.path.isdir( | 625 gclient_scm.os.path.isdir( |
| 707 os.path.join(self.base_path, '.git', 'hooks')).AndReturn(False) | 626 os.path.join(self.base_path, '.git', 'hooks')).AndReturn(False) |
| 708 gclient_scm.os.path.exists(self.backup_base_path).AndReturn(False) | 627 gclient_scm.os.path.exists(self.backup_base_path).AndReturn(False) |
| 709 gclient_scm.os.path.exists(self.base_path).AndReturn(True) | 628 gclient_scm.os.path.exists(self.base_path).AndReturn(True) |
| 710 gclient_scm.os.path.isdir(self.base_path).AndReturn(True) | 629 gclient_scm.os.path.isdir(self.base_path).AndReturn(True) |
| 711 gclient_scm.os.path.exists(os.path.join(self.base_path, '.git') | 630 gclient_scm.os.path.exists(os.path.join(self.base_path, '.git') |
| 712 ).AndReturn(False) | 631 ).AndReturn(False) |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 971 | 890 |
| 972 if __name__ == '__main__': | 891 if __name__ == '__main__': |
| 973 level = logging.DEBUG if '-v' in sys.argv else logging.FATAL | 892 level = logging.DEBUG if '-v' in sys.argv else logging.FATAL |
| 974 logging.basicConfig( | 893 logging.basicConfig( |
| 975 level=level, | 894 level=level, |
| 976 format='%(asctime).19s %(levelname)s %(filename)s:' | 895 format='%(asctime).19s %(levelname)s %(filename)s:' |
| 977 '%(lineno)s %(message)s') | 896 '%(lineno)s %(message)s') |
| 978 unittest.main() | 897 unittest.main() |
| 979 | 898 |
| 980 # vim: ts=2:sw=2:tw=80:et: | 899 # vim: ts=2:sw=2:tw=80:et: |
| OLD | NEW |