OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 86 |
87 def Options(self, *args, **kwargs): | 87 def Options(self, *args, **kwargs): |
88 return self.OptionsObject(*args, **kwargs) | 88 return self.OptionsObject(*args, **kwargs) |
89 | 89 |
90 def setUp(self): | 90 def setUp(self): |
91 BaseTestCase.setUp(self) | 91 BaseTestCase.setUp(self) |
92 self.url = self.SvnUrl() | 92 self.url = self.SvnUrl() |
93 | 93 |
94 def testDir(self): | 94 def testDir(self): |
95 members = [ | 95 members = [ |
96 'FullUrlForRelativeUrl', 'GetRevisionDate', 'RunCommand', | 96 'FullUrlForRelativeUrl', |
97 'cleanup', 'diff', 'pack', 'relpath', 'revert', | 97 'GetRevisionDate', |
98 'revinfo', 'runhooks', 'status', 'update', | 98 'GetUsableRev', |
99 'updatesingle', 'url', | 99 'RunCommand', |
| 100 'cleanup', |
| 101 'diff', |
| 102 'pack', |
| 103 'relpath', |
| 104 'revert', |
| 105 'revinfo', |
| 106 'runhooks', |
| 107 'status', |
| 108 'update', |
| 109 'updatesingle', |
| 110 'url', |
100 ] | 111 ] |
101 | 112 |
102 # If you add a member, be sure to add the relevant test! | 113 # If you add a member, be sure to add the relevant test! |
103 self.compareMembers(self._scm_wrapper('svn://a'), members) | 114 self.compareMembers(self._scm_wrapper('svn://a'), members) |
104 | 115 |
105 def testUnsupportedSCM(self): | 116 def testUnsupportedSCM(self): |
106 args = ['gopher://foo', self.root_dir, self.relpath] | 117 args = ['gopher://foo', self.root_dir, self.relpath] |
107 exception_msg = 'No SCM found for url gopher://foo' | 118 exception_msg = 'No SCM found for url gopher://foo' |
108 self.assertRaisesError(exception_msg, self._scm_wrapper, *args) | 119 self.assertRaisesError(exception_msg, self._scm_wrapper, *args) |
109 | 120 |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 gclient_scm.os.path.exists(join(self.base_path, '.hg')).AndReturn(True) | 548 gclient_scm.os.path.exists(join(self.base_path, '.hg')).AndReturn(True) |
538 | 549 |
539 self.mox.ReplayAll() | 550 self.mox.ReplayAll() |
540 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | 551 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
541 relpath=self.relpath) | 552 relpath=self.relpath) |
542 file_list = [] | 553 file_list = [] |
543 scm.update(options, self.args, file_list) | 554 scm.update(options, self.args, file_list) |
544 self.checkstdout( | 555 self.checkstdout( |
545 ('________ found .hg directory; skipping %s\n' % self.relpath)) | 556 ('________ found .hg directory; skipping %s\n' % self.relpath)) |
546 | 557 |
| 558 def testGetUsableRevSVN(self): |
| 559 # pylint: disable=E1101 |
| 560 options = self.Options(verbose=True) |
| 561 |
| 562 # Mock SVN revision validity checking. |
| 563 self.mox.StubOutWithMock( |
| 564 gclient_scm.scm.SVN, 'IsValidRevision', True) |
| 565 gclient_scm.scm.SVN.IsValidRevision(url='%s@%s' % (self.url, 1) |
| 566 ).AndReturn(True) |
| 567 gclient_scm.scm.SVN.IsValidRevision(url='%s@%s' % (self.url, 'fake') |
| 568 ).AndReturn(False) |
| 569 |
| 570 self.mox.ReplayAll() |
| 571 |
| 572 svn_scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir) |
| 573 # With an SVN checkout, 1 an example of a valid usable rev. |
| 574 self.assertEquals(svn_scm.GetUsableRev(1, options), 1) |
| 575 # With an SVN checkout, a fake or unknown rev should raise an excpetion. |
| 576 self.assertRaises(gclient_scm.gclient_utils.Error, |
| 577 svn_scm.GetUsableRev, 'fake', options) |
547 | 578 |
548 class BaseGitWrapperTestCase(GCBaseTestCase, StdoutCheck, TestCaseUtils, | 579 class BaseGitWrapperTestCase(GCBaseTestCase, StdoutCheck, TestCaseUtils, |
549 unittest.TestCase): | 580 unittest.TestCase): |
550 """This class doesn't use pymox.""" | 581 """This class doesn't use pymox.""" |
551 class OptionsObject(object): | 582 class OptionsObject(object): |
552 def __init__(self, verbose=False, revision=None): | 583 def __init__(self, verbose=False, revision=None): |
553 self.verbose = verbose | 584 self.verbose = verbose |
554 self.revision = revision | 585 self.revision = revision |
555 self.manually_grab_svn_rev = True | 586 self.manually_grab_svn_rev = True |
556 self.deps_os = None | 587 self.deps_os = None |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 self.base_path = join(self.root_dir, self.relpath) | 670 self.base_path = join(self.root_dir, self.relpath) |
640 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) | 671 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) |
641 StdoutCheck.setUp(self) | 672 StdoutCheck.setUp(self) |
642 | 673 |
643 def tearDown(self): | 674 def tearDown(self): |
644 StdoutCheck.tearDown(self) | 675 StdoutCheck.tearDown(self) |
645 TestCaseUtils.tearDown(self) | 676 TestCaseUtils.tearDown(self) |
646 unittest.TestCase.tearDown(self) | 677 unittest.TestCase.tearDown(self) |
647 rmtree(self.root_dir) | 678 rmtree(self.root_dir) |
648 | 679 |
649 | |
650 class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): | 680 class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): |
651 def testDir(self): | 681 def testDir(self): |
652 members = [ | 682 members = [ |
653 'FullUrlForRelativeUrl', 'GetRevisionDate', 'RunCommand', | 683 'FullUrlForRelativeUrl', |
654 'cleanup', 'diff', 'pack', 'relpath', 'revert', | 684 'GetRevisionDate', |
655 'revinfo', 'runhooks', 'status', 'update', 'url', | 685 'GetUsableRev', |
| 686 'RunCommand', |
| 687 'cleanup', |
| 688 'diff', |
| 689 'pack', |
| 690 'relpath', |
| 691 'revert', |
| 692 'revinfo', |
| 693 'runhooks', |
| 694 'status', |
| 695 'update', |
| 696 'url', |
656 ] | 697 ] |
657 | 698 |
658 # If you add a member, be sure to add the relevant test! | 699 # If you add a member, be sure to add the relevant test! |
659 self.compareMembers(gclient_scm.CreateSCM(url=self.url), members) | 700 self.compareMembers(gclient_scm.CreateSCM(url=self.url), members) |
660 | 701 |
661 def testRevertMissing(self): | 702 def testRevertMissing(self): |
662 if not self.enabled: | 703 if not self.enabled: |
663 return | 704 return |
664 options = self.Options() | 705 options = self.Options() |
665 file_path = join(self.base_path, 'a') | 706 file_path = join(self.base_path, 'a') |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
922 def testRevinfo(self): | 963 def testRevinfo(self): |
923 if not self.enabled: | 964 if not self.enabled: |
924 return | 965 return |
925 options = self.Options() | 966 options = self.Options() |
926 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 967 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
927 relpath=self.relpath) | 968 relpath=self.relpath) |
928 rev_info = scm.revinfo(options, (), None) | 969 rev_info = scm.revinfo(options, (), None) |
929 self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') | 970 self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') |
930 | 971 |
931 | 972 |
| 973 class ManagedGitWrapperTestCaseMox(BaseTestCase): |
| 974 class OptionsObject(object): |
| 975 def __init__(self, verbose=False, revision=None, force=False): |
| 976 self.verbose = verbose |
| 977 self.revision = revision |
| 978 self.deps_os = None |
| 979 self.force = force |
| 980 self.reset = False |
| 981 self.nohooks = False |
| 982 # TODO(maruel): Test --jobs > 1. |
| 983 self.jobs = 1 |
| 984 |
| 985 def Options(self, *args, **kwargs): |
| 986 return self.OptionsObject(*args, **kwargs) |
| 987 |
| 988 def setUp(self): |
| 989 BaseTestCase.setUp(self) |
| 990 self.fake_hash_1 = 't0ta11yf4k3' |
| 991 self.fake_hash_2 = '3v3nf4k3r' |
| 992 self.url = 'git://foo' |
| 993 self.root_dir = '/tmp' |
| 994 self.relpath = 'fake' |
| 995 self.base_path = os.path.join(self.root_dir, self.relpath) |
| 996 |
| 997 def tearDown(self): |
| 998 BaseTestCase.tearDown(self) |
| 999 |
| 1000 def testGetUsableRevGit(self): |
| 1001 # pylint: disable=E1101 |
| 1002 options = self.Options(verbose=True) |
| 1003 |
| 1004 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsValidRevision', True) |
| 1005 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=self.fake_hash_1 |
| 1006 ).AndReturn(True) |
| 1007 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev='1' |
| 1008 ).AndReturn(False) |
| 1009 |
| 1010 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsGitSvn', True) |
| 1011 gclient_scm.scm.GIT.IsGitSvn(cwd=self.base_path).MultipleTimes( |
| 1012 ).AndReturn(False) |
| 1013 |
| 1014 self.mox.ReplayAll() |
| 1015 |
| 1016 git_scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 1017 relpath=self.relpath) |
| 1018 # A [fake] git sha1 with a git repo should work (this is in the case that |
| 1019 # the LKGR gets flipped to git sha1's some day). |
| 1020 self.assertEquals(git_scm.GetUsableRev(self.fake_hash_1, options), |
| 1021 self.fake_hash_1) |
| 1022 # An SVN rev with a purely git repo should raise an exception. |
| 1023 self.assertRaises(gclient_scm.gclient_utils.Error, |
| 1024 git_scm.GetUsableRev, '1', options) |
| 1025 |
| 1026 def testGetUsableRevGitSvn(self): |
| 1027 # pylint: disable=E1101 |
| 1028 options = self.Options() |
| 1029 too_big = str(1e7) |
| 1030 |
| 1031 # Pretend like the git-svn repo's HEAD is at r2. |
| 1032 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'GetGitSvnHeadRev', True) |
| 1033 gclient_scm.scm.GIT.GetGitSvnHeadRev(cwd=self.base_path).MultipleTimes( |
| 1034 ).AndReturn(2) |
| 1035 |
| 1036 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'GetSha1ForSvnRev', True) |
| 1037 # r1 -> first fake hash, r3 -> second fake hash. |
| 1038 gclient_scm.scm.GIT.GetSha1ForSvnRev(cwd=self.base_path, rev='1' |
| 1039 ).AndReturn(self.fake_hash_1) |
| 1040 gclient_scm.scm.GIT.GetSha1ForSvnRev(cwd=self.base_path, rev='3' |
| 1041 ).AndReturn(self.fake_hash_2) |
| 1042 |
| 1043 # Ensure that we call git svn fetch if our LKGR is > the git-svn HEAD rev. |
| 1044 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'Capture', True) |
| 1045 gclient_scm.scm.GIT.Capture(['svn', 'fetch'], cwd=self.base_path) |
| 1046 |
| 1047 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsGitSvn', True) |
| 1048 gclient_scm.scm.GIT.IsGitSvn(cwd=self.base_path).MultipleTimes( |
| 1049 ).AndReturn(True) |
| 1050 |
| 1051 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsValidRevision', True) |
| 1052 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=self.fake_hash_1 |
| 1053 ).AndReturn(True) |
| 1054 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=too_big |
| 1055 ).AndReturn(False) |
| 1056 |
| 1057 self.mox.ReplayAll() |
| 1058 |
| 1059 git_svn_scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 1060 relpath=self.relpath) |
| 1061 # Given an SVN revision with a git-svn checkout, it should be translated to |
| 1062 # a git sha1 and be usable. |
| 1063 self.assertEquals(git_svn_scm.GetUsableRev('1', options), |
| 1064 self.fake_hash_1) |
| 1065 # Our fake HEAD rev is r2, so this should call git svn fetch to get more |
| 1066 # revs (pymox will complain if this doesn't happen). |
| 1067 self.assertEquals(git_svn_scm.GetUsableRev('3', options), |
| 1068 self.fake_hash_2) |
| 1069 # Given a git sha1 with a git-svn checkout, it should be used as is. |
| 1070 self.assertEquals(git_svn_scm.GetUsableRev(self.fake_hash_1, options), |
| 1071 self.fake_hash_1) |
| 1072 # We currently check for seemingly valid SVN revisions by assuming 6 digit |
| 1073 # numbers, so assure that numeric revs >= 1000000 don't work. |
| 1074 self.assertRaises(gclient_scm.gclient_utils.Error, |
| 1075 git_svn_scm.GetUsableRev, too_big, options) |
| 1076 |
| 1077 |
932 class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): | 1078 class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): |
933 def testUpdateCheckout(self): | 1079 def testUpdateCheckout(self): |
934 if not self.enabled: | 1080 if not self.enabled: |
935 return | 1081 return |
936 options = self.Options(verbose=True) | 1082 options = self.Options(verbose=True) |
937 root_dir = gclient_scm.os.path.realpath(tempfile.mkdtemp()) | 1083 root_dir = gclient_scm.os.path.realpath(tempfile.mkdtemp()) |
938 relpath = 'foo' | 1084 relpath = 'foo' |
939 base_path = join(root_dir, relpath) | 1085 base_path = join(root_dir, relpath) |
940 url = join(self.base_path, '.git') | 1086 url = join(self.base_path, '.git') |
941 try: | 1087 try: |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 | 1137 |
992 if __name__ == '__main__': | 1138 if __name__ == '__main__': |
993 if '-v' in sys.argv: | 1139 if '-v' in sys.argv: |
994 logging.basicConfig( | 1140 logging.basicConfig( |
995 level=logging.DEBUG, | 1141 level=logging.DEBUG, |
996 format='%(asctime).19s %(levelname)s %(filename)s:' | 1142 format='%(asctime).19s %(levelname)s %(filename)s:' |
997 '%(lineno)s %(message)s') | 1143 '%(lineno)s %(message)s') |
998 unittest.main() | 1144 unittest.main() |
999 | 1145 |
1000 # vim: ts=2:sw=2:tw=80:et: | 1146 # vim: ts=2:sw=2:tw=80:et: |
OLD | NEW |