Chromium Code Reviews| 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 scm.py.""" | 6 """Unit tests for scm.py.""" |
| 7 | 7 |
| 8 from __future__ import with_statement | 8 from __future__ import with_statement |
| 9 import logging | 9 import logging |
| 10 import os | 10 import os |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 self.mox.StubOutWithMock(scm.gclient_utils, 'CheckCallAndFilter') | 41 self.mox.StubOutWithMock(scm.gclient_utils, 'CheckCallAndFilter') |
| 42 self.mox.StubOutWithMock(scm.gclient_utils, 'CheckCallAndFilterAndHeader') | 42 self.mox.StubOutWithMock(scm.gclient_utils, 'CheckCallAndFilterAndHeader') |
| 43 self.mox.StubOutWithMock(subprocess2, 'Popen') | 43 self.mox.StubOutWithMock(subprocess2, 'Popen') |
| 44 self.mox.StubOutWithMock(subprocess2, 'communicate') | 44 self.mox.StubOutWithMock(subprocess2, 'communicate') |
| 45 | 45 |
| 46 | 46 |
| 47 class RootTestCase(BaseSCMTestCase): | 47 class RootTestCase(BaseSCMTestCase): |
| 48 def testMembersChanged(self): | 48 def testMembersChanged(self): |
| 49 self.mox.ReplayAll() | 49 self.mox.ReplayAll() |
| 50 members = [ | 50 members = [ |
| 51 'ElementTree', 'GetCasedPath', 'GenFakeDiff', 'GIT', 'SVN', | 51 'cStringIO', |
| 52 'determine_scm', | |
| 53 'ElementTree', | |
| 54 'gclient_utils', | |
| 55 'GenFakeDiff', | |
| 56 'GetCasedPath', | |
| 57 'GIT', | |
| 58 'glob', | |
| 59 'logging', | |
| 60 'only_int', | |
| 61 'os', | |
| 62 're', | |
| 63 'subprocess2', | |
| 64 'SVN', | |
| 65 'sys', | |
| 66 'tempfile', | |
| 67 'time', | |
| 52 'ValidateEmail', | 68 'ValidateEmail', |
| 53 'cStringIO', 'determine_scm', 'gclient_utils', 'glob', 'logging', | |
| 54 'only_int', 'os', 're', 'subprocess2', 'sys', 'tempfile', 'time', | |
| 55 ] | 69 ] |
| 56 # If this test fails, you should add the relevant test. | 70 # If this test fails, you should add the relevant test. |
| 57 self.compareMembers(scm, members) | 71 self.compareMembers(scm, members) |
| 58 | 72 |
| 59 | 73 |
| 60 class GitWrapperTestCase(BaseSCMTestCase): | 74 class GitWrapperTestCase(BaseSCMTestCase): |
| 61 def testMembersChanged(self): | 75 def testMembersChanged(self): |
| 62 members = [ | 76 members = [ |
| 63 'AssertVersion', 'Capture', 'CaptureStatus', | 77 'AssertVersion', |
| 78 'Capture', | |
| 79 'CaptureStatus', | |
| 80 'current_version', | |
| 64 'FetchUpstreamTuple', | 81 'FetchUpstreamTuple', |
| 65 'GenerateDiff', 'GetBranch', 'GetBranchRef', 'GetCheckoutRoot', | 82 'GenerateDiff', |
| 66 'GetDifferentFiles', 'GetEmail', 'GetPatchName', 'GetSVNBranch', | 83 'GetBranch', |
| 67 'GetUpstreamBranch', 'IsGitSvn', 'MatchSvnGlob', 'ShortBranchName', | 84 'GetBranchRef', |
| 68 'current_version', | 85 'GetCheckoutRoot', |
| 86 'GetDifferentFiles', | |
| 87 'GetEmail', | |
| 88 'GetGitSVNHeadRev', | |
| 89 'GetPatchName', | |
| 90 'GetSha1ForSVNRev', | |
| 91 'GetSVNBranch', | |
| 92 'GetUpstreamBranch', | |
| 93 'IsGitSvn', | |
| 94 'IsValidRevision', | |
| 95 'MatchSvnGlob', | |
| 96 'ShortBranchName', | |
| 69 ] | 97 ] |
| 70 # If this test fails, you should add the relevant test. | 98 # If this test fails, you should add the relevant test. |
| 71 self.compareMembers(scm.GIT, members) | 99 self.compareMembers(scm.GIT, members) |
| 72 | 100 |
| 73 def testGetEmail(self): | 101 def testGetEmail(self): |
| 74 self.mox.StubOutWithMock(scm.GIT, 'Capture') | 102 self.mox.StubOutWithMock(scm.GIT, 'Capture') |
| 75 scm.GIT.Capture(['config', 'user.email'], cwd=self.root_dir | 103 scm.GIT.Capture(['config', 'user.email'], cwd=self.root_dir |
| 76 ).AndReturn('mini@me.com') | 104 ).AndReturn('mini@me.com') |
| 77 self.mox.ReplayAll() | 105 self.mox.ReplayAll() |
| 78 self.assertEqual(scm.GIT.GetEmail(self.root_dir), 'mini@me.com') | 106 self.assertEqual(scm.GIT.GetEmail(self.root_dir), 'mini@me.com') |
| 79 | 107 |
| 80 def testMatchSvnGlob(self): | 108 def testMatchSvnGlob(self): |
| 81 self.assertEquals(scm.GIT.MatchSvnGlob( | 109 self.assertEquals(scm.GIT.MatchSvnGlob( |
| 82 'svn://svn.chromium.org/chrome/trunk/src', | 110 'svn://svn.chromium.org/chrome/trunk/src', |
| 83 'svn://svn.chromium.org/chrome', | 111 'svn://svn.chromium.org/chrome', |
| 84 'trunk/src:refs/remotes/origin/trunk', | 112 'trunk/src:refs/remotes/origin/trunk', |
| 85 False), 'refs/remotes/origin/trunk') | 113 False), 'refs/remotes/origin/trunk') |
| 86 self.assertEquals(scm.GIT.MatchSvnGlob( | 114 self.assertEquals(scm.GIT.MatchSvnGlob( |
| 87 'https://v8.googlecode.com/svn/branches/bleeding_edge', | 115 'https://v8.googlecode.com/svn/branches/bleeding_edge', |
| 88 'https://v8.googlecode.com/svn', | 116 'https://v8.googlecode.com/svn', |
| 89 'branches/*:refs/remotes/*', | 117 'branches/*:refs/remotes/*', |
| 90 True), 'refs/remotes/bleeding_edge') | 118 True), 'refs/remotes/bleeding_edge') |
| 91 | 119 |
|
M-A Ruel
2011/12/16 15:03:31
add another line
Dan Beam
2011/12/16 18:05:53
Done.
| |
| 120 class RealGitTest(fake_repos.FakeReposTestBase): | |
| 121 def setUp(self): | |
| 122 super(RealGitTest, self).setUp() | |
| 123 self.enabled = self.FAKE_REPOS.set_up_git() | |
| 124 if self.enabled: | |
| 125 self.clone_dir = scm.os.path.join(self.FAKE_REPOS.git_root, 'repo_1') | |
| 126 | |
| 127 def testIsValidRevision(self): | |
| 128 if not self.enabled: | |
| 129 return | |
| 130 # Sha1's are [0-9a-z]{32}, so starting with a 'z' or 'r' should always fail. | |
| 131 self.assertFalse(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev='zebra')) | |
| 132 self.assertFalse(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev='r123456')) | |
| 133 # Valid cases | |
| 134 first_rev = self.githash('repo_1', 1) | |
| 135 self.assertTrue(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev=first_rev)) | |
| 136 self.assertTrue(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev='HEAD')) | |
| 137 | |
|
M-A Ruel
2011/12/16 15:03:31
add another line here
Dan Beam
2011/12/16 18:05:53
Done.
| |
| 138 class RealGitSVNTest(fake_repos.FakeReposTestBase): | |
| 139 def setUp(self): | |
| 140 super(RealGitSVNTest, self).setUp() | |
| 141 self.enabled = self.FAKE_REPOS.set_up_git() and self.FAKE_REPOS.set_up_svn() | |
| 142 if self.enabled: | |
| 143 self.tree_name = 'git-svn' | |
| 144 self.svn_url = scm.os.path.join(self.FAKE_REPOS.svn_base, 'trunk') | |
| 145 self.clone_dir = scm.os.path.join(self.FAKE_REPOS.git_root, | |
| 146 self.tree_name) | |
| 147 scm.gclient_utils.rmtree(self.clone_dir) | |
| 148 scm.os.makedirs(self.clone_dir) | |
| 149 self._capture(['svn', 'clone', '-q', '-q', self.svn_url, self.clone_dir]) | |
| 150 hashes = self._capture(['rev-list', 'HEAD']).splitlines() | |
|
M-A Ruel
2011/12/16 15:03:31
# We insert a null value at 0 to do 1-based indexi
Dan Beam
2011/12/16 18:05:53
Done. (yay, knew there had to be a way to do this!
| |
| 151 # git rev-list gives revisions in reverse chronological order. | |
| 152 hashes.reverse() | |
| 153 # We insert a null value at 0 to do 1-based indexing, not 0-based, as SVN | |
| 154 # revisions are 1-based (i.e. they start at r1, not r0). | |
| 155 hashes.insert(0, None) | |
| 156 self.git_hashes = hashes | |
| 157 | |
| 158 def _capture(self, cmd, **kwargs): | |
| 159 kwargs.setdefault('cwd', self.clone_dir) | |
| 160 return scm.GIT.Capture(cmd, **kwargs) | |
| 161 | |
| 162 def testGetGitSVNHeadRev(self): | |
| 163 if not self.enabled: | |
| 164 return | |
| 165 self.assertEquals(scm.GIT.GetGitSVNHeadRev(cwd=self.clone_dir), 2) | |
| 166 self._capture(['reset', '--hard', 'HEAD^']) | |
| 167 self.assertEquals(scm.GIT.GetGitSVNHeadRev(cwd=self.clone_dir), 1) | |
| 168 | |
| 169 def testGetGetSha1ForSVNRev(self): | |
| 170 if not self.enabled: | |
| 171 return | |
| 172 self.assertEquals(scm.GIT.GetSha1ForSVNRev(cwd=self.clone_dir, rev=1), | |
| 173 self.git_hashes[1]) | |
| 174 self.assertEquals(scm.GIT.GetSha1ForSVNRev(cwd=self.clone_dir, rev=2), | |
| 175 self.git_hashes[2]) | |
| 176 | |
| 92 class SVNTestCase(BaseSCMTestCase): | 177 class SVNTestCase(BaseSCMTestCase): |
| 93 def setUp(self): | 178 def setUp(self): |
| 94 BaseSCMTestCase.setUp(self) | 179 BaseSCMTestCase.setUp(self) |
| 95 self.mox.StubOutWithMock(scm.SVN, 'Capture') | 180 self.mox.StubOutWithMock(scm.SVN, 'Capture') |
| 96 self.url = self.SvnUrl() | 181 self.url = self.SvnUrl() |
| 97 | 182 |
| 98 def testMembersChanged(self): | 183 def testMembersChanged(self): |
| 99 self.mox.ReplayAll() | 184 self.mox.ReplayAll() |
| 100 members = [ | 185 members = [ |
| 101 'AssertVersion', 'Capture', 'CaptureRevision', 'CaptureLocalInfo', | 186 'AssertVersion', |
| 187 'Capture', | |
| 188 'CaptureLocalInfo', | |
| 102 'CaptureRemoteInfo', | 189 'CaptureRemoteInfo', |
| 103 'CaptureStatus', 'current_version', 'DiffItem', 'GenerateDiff', | 190 'CaptureRevision', |
| 104 'GetCheckoutRoot', 'GetEmail', 'GetFileProperty', 'IsMoved', | 191 'CaptureStatus', |
| 105 'IsMovedInfo', 'ReadSimpleAuth', 'Revert', 'RunAndGetFileList', | 192 'current_version', |
| 193 'DiffItem', | |
| 194 'GenerateDiff', | |
| 195 'GetCheckoutRoot', | |
| 196 'GetEmail', | |
| 197 'GetFileProperty', | |
| 198 'IsMoved', | |
| 199 'IsMovedInfo', | |
| 200 'IsValidRevision', | |
| 201 'ReadSimpleAuth', | |
| 202 'Revert', | |
| 203 'RunAndGetFileList', | |
| 106 ] | 204 ] |
| 107 # If this test fails, you should add the relevant test. | 205 # If this test fails, you should add the relevant test. |
| 108 self.compareMembers(scm.SVN, members) | 206 self.compareMembers(scm.SVN, members) |
| 109 | 207 |
| 110 def testGetCheckoutRoot(self): | 208 def testGetCheckoutRoot(self): |
| 111 # pylint: disable=E1103 | 209 # pylint: disable=E1103 |
| 112 self.mox.StubOutWithMock(scm.SVN, '_CaptureInfo') | 210 self.mox.StubOutWithMock(scm.SVN, '_CaptureInfo') |
| 113 self.mox.StubOutWithMock(scm, 'GetCasedPath') | 211 self.mox.StubOutWithMock(scm, 'GetCasedPath') |
| 114 scm.os.path.abspath = lambda x: x | 212 scm.os.path.abspath = lambda x: x |
| 115 scm.GetCasedPath = lambda x: x | 213 scm.GetCasedPath = lambda x: x |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 # Asserting the tree is not sufficient, svn status must come out clear too. | 440 # Asserting the tree is not sufficient, svn status must come out clear too. |
| 343 self.assertEquals('', self._capture(['status'])) | 441 self.assertEquals('', self._capture(['status'])) |
| 344 | 442 |
| 345 | 443 |
| 346 if __name__ == '__main__': | 444 if __name__ == '__main__': |
| 347 if '-v' in sys.argv: | 445 if '-v' in sys.argv: |
| 348 logging.basicConfig(level=logging.DEBUG) | 446 logging.basicConfig(level=logging.DEBUG) |
| 349 unittest.main() | 447 unittest.main() |
| 350 | 448 |
| 351 # vim: ts=2:sw=2:tw=80:et: | 449 # vim: ts=2:sw=2:tw=80:et: |
| OLD | NEW |