[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2008-2009 Google Inc. All Rights Reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """Unit tests for gclient_scm.py.""" |
| 18 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 19 | import shutil |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 20 | # Import it before super_mox to keep a valid reference. |
| 21 | from subprocess import Popen, PIPE, STDOUT |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 22 | import tempfile |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 23 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 24 | import gclient_scm |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 25 | from gclient_test import BaseTestCase as GCBaseTestCase |
| 26 | from super_mox import mox, SuperMoxBaseTestBase |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 27 | |
| 28 | |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 29 | class BaseTestCase(GCBaseTestCase): |
| 30 | def setUp(self): |
| 31 | GCBaseTestCase.setUp(self) |
| 32 | self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead') |
| 33 | self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite') |
| 34 | self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'SubprocessCall') |
| 35 | self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'RemoveDirectory') |
| 36 | self._CaptureSVNInfo = gclient_scm.CaptureSVNInfo |
| 37 | self.mox.StubOutWithMock(gclient_scm, 'CaptureSVN') |
| 38 | self.mox.StubOutWithMock(gclient_scm, 'CaptureSVNInfo') |
| 39 | self.mox.StubOutWithMock(gclient_scm, 'CaptureSVNStatus') |
| 40 | self.mox.StubOutWithMock(gclient_scm, 'RunSVN') |
| 41 | self.mox.StubOutWithMock(gclient_scm, 'RunSVNAndGetFileList') |
| 42 | self._scm_wrapper = gclient_scm.CreateSCM |
| 43 | |
| 44 | |
| 45 | class SVNWrapperTestCase(BaseTestCase): |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 46 | class OptionsObject(object): |
| 47 | def __init__(self, test_case, verbose=False, revision=None): |
| 48 | self.verbose = verbose |
| 49 | self.revision = revision |
| 50 | self.manually_grab_svn_rev = True |
| 51 | self.deps_os = None |
| 52 | self.force = False |
| 53 | self.nohooks = False |
| 54 | |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 55 | def Options(self, *args, **kwargs): |
| 56 | return self.OptionsObject(self, *args, **kwargs) |
| 57 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 58 | def setUp(self): |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 59 | BaseTestCase.setUp(self) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 60 | self.root_dir = self.Dir() |
| 61 | self.args = self.Args() |
| 62 | self.url = self.Url() |
| 63 | self.relpath = 'asf' |
| 64 | |
| 65 | def testDir(self): |
| 66 | members = [ |
| 67 | 'FullUrlForRelativeUrl', 'RunCommand', 'cleanup', 'diff', 'export', |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 68 | 'pack', 'relpath', 'revert', 'revinfo', 'runhooks', 'scm_name', 'status', |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 69 | 'update', 'url', |
| 70 | ] |
| 71 | |
| 72 | # If you add a member, be sure to add the relevant test! |
| 73 | self.compareMembers(self._scm_wrapper(), members) |
| 74 | |
| 75 | def testUnsupportedSCM(self): |
| 76 | args = [self.url, self.root_dir, self.relpath] |
| 77 | kwargs = {'scm_name' : 'foo'} |
| 78 | exception_msg = 'Unsupported scm %(scm_name)s' % kwargs |
| 79 | self.assertRaisesError(exception_msg, self._scm_wrapper, *args, **kwargs) |
| 80 | |
| 81 | def testFullUrlForRelativeUrl(self): |
| 82 | self.url = 'svn://a/b/c/d' |
| 83 | |
| 84 | self.mox.ReplayAll() |
| 85 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 86 | relpath=self.relpath) |
| 87 | self.assertEqual(scm.FullUrlForRelativeUrl('/crap'), 'svn://a/b/crap') |
| 88 | |
| 89 | def testRunCommandException(self): |
| 90 | options = self.Options(verbose=False) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 91 | file_path = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') |
| 92 | gclient_scm.os.path.exists(file_path).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 93 | |
| 94 | self.mox.ReplayAll() |
| 95 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 96 | relpath=self.relpath) |
| 97 | exception = "Unsupported argument(s): %s" % ','.join(self.args) |
| 98 | self.assertRaisesError(exception, scm.RunCommand, |
| 99 | 'update', options, self.args) |
| 100 | |
| 101 | def testRunCommandUnknown(self): |
| 102 | # TODO(maruel): if ever used. |
| 103 | pass |
| 104 | |
| 105 | def testRevertMissing(self): |
| 106 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 107 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 108 | gclient_scm.os.path.isdir(base_path).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 109 | # It'll to a checkout instead. |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 110 | gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git') |
| 111 | ).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 112 | print("\n_____ %s is missing, synching instead" % self.relpath) |
| 113 | # Checkout. |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 114 | gclient_scm.os.path.exists(base_path).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 115 | files_list = self.mox.CreateMockAnything() |
[email protected] | 22e29d4 | 2009-10-28 00:48:26 | [diff] [blame] | 116 | gclient_scm.RunSVNAndGetFileList(options, ['checkout', self.url, base_path], |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 117 | self.root_dir, files_list) |
| 118 | |
| 119 | self.mox.ReplayAll() |
| 120 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 121 | relpath=self.relpath) |
| 122 | scm.revert(options, self.args, files_list) |
| 123 | |
| 124 | def testRevertNone(self): |
| 125 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 126 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 127 | gclient_scm.os.path.isdir(base_path).AndReturn(True) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 128 | gclient_scm.CaptureSVNStatus(base_path).AndReturn([]) |
[email protected] | 22e29d4 | 2009-10-28 00:48:26 | [diff] [blame] | 129 | gclient_scm.RunSVNAndGetFileList(options, ['update', '--revision', 'BASE'], |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 130 | base_path, mox.IgnoreArg()) |
| 131 | |
| 132 | self.mox.ReplayAll() |
| 133 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 134 | relpath=self.relpath) |
| 135 | file_list = [] |
| 136 | scm.revert(options, self.args, file_list) |
| 137 | |
| 138 | def testRevert2Files(self): |
| 139 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 140 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 141 | gclient_scm.os.path.isdir(base_path).AndReturn(True) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 142 | items = [ |
| 143 | ('M ', 'a'), |
| 144 | ('A ', 'b'), |
| 145 | ] |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 146 | file_path1 = gclient_scm.os.path.join(base_path, 'a') |
| 147 | file_path2 = gclient_scm.os.path.join(base_path, 'b') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 148 | gclient_scm.CaptureSVNStatus(base_path).AndReturn(items) |
| 149 | gclient_scm.os.path.exists(file_path1).AndReturn(True) |
| 150 | gclient_scm.os.path.isfile(file_path1).AndReturn(True) |
| 151 | gclient_scm.os.remove(file_path1) |
| 152 | gclient_scm.os.path.exists(file_path2).AndReturn(True) |
| 153 | gclient_scm.os.path.isfile(file_path2).AndReturn(True) |
| 154 | gclient_scm.os.remove(file_path2) |
[email protected] | 22e29d4 | 2009-10-28 00:48:26 | [diff] [blame] | 155 | gclient_scm.RunSVNAndGetFileList(options, ['update', '--revision', 'BASE'], |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 156 | base_path, mox.IgnoreArg()) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 157 | print(gclient_scm.os.path.join(base_path, 'a')) |
| 158 | print(gclient_scm.os.path.join(base_path, 'b')) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 159 | |
| 160 | self.mox.ReplayAll() |
| 161 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 162 | relpath=self.relpath) |
| 163 | file_list = [] |
| 164 | scm.revert(options, self.args, file_list) |
| 165 | |
| 166 | def testRevertDirectory(self): |
| 167 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 168 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 169 | gclient_scm.os.path.isdir(base_path).AndReturn(True) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 170 | items = [ |
| 171 | ('~ ', 'a'), |
| 172 | ] |
| 173 | gclient_scm.CaptureSVNStatus(base_path).AndReturn(items) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 174 | file_path = gclient_scm.os.path.join(base_path, 'a') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 175 | print(file_path) |
| 176 | gclient_scm.os.path.exists(file_path).AndReturn(True) |
| 177 | gclient_scm.os.path.isfile(file_path).AndReturn(False) |
| 178 | gclient_scm.os.path.isdir(file_path).AndReturn(True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 179 | gclient_scm.gclient_utils.RemoveDirectory(file_path) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 180 | file_list1 = [] |
[email protected] | 22e29d4 | 2009-10-28 00:48:26 | [diff] [blame] | 181 | gclient_scm.RunSVNAndGetFileList(options, ['update', '--revision', 'BASE'], |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 182 | base_path, mox.IgnoreArg()) |
| 183 | |
| 184 | self.mox.ReplayAll() |
| 185 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 186 | relpath=self.relpath) |
| 187 | file_list2 = [] |
| 188 | scm.revert(options, self.args, file_list2) |
| 189 | |
| 190 | def testStatus(self): |
| 191 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 192 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 193 | gclient_scm.os.path.isdir(base_path).AndReturn(True) |
[email protected] | 22e29d4 | 2009-10-28 00:48:26 | [diff] [blame] | 194 | gclient_scm.RunSVNAndGetFileList(options, ['status'] + self.args, |
| 195 | base_path, []).AndReturn(None) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 196 | |
| 197 | self.mox.ReplayAll() |
| 198 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 199 | relpath=self.relpath) |
| 200 | file_list = [] |
| 201 | self.assertEqual(scm.status(options, self.args, file_list), None) |
| 202 | |
| 203 | |
| 204 | # TODO(maruel): TEST REVISIONS!!! |
| 205 | # TODO(maruel): TEST RELOCATE!!! |
| 206 | def testUpdateCheckout(self): |
| 207 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 208 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 209 | file_info = gclient_scm.gclient_utils.PrintableObject() |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 210 | file_info.root = 'blah' |
| 211 | file_info.url = self.url |
| 212 | file_info.uuid = 'ABC' |
| 213 | file_info.revision = 42 |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 214 | gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git') |
| 215 | ).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 216 | # Checkout. |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 217 | gclient_scm.os.path.exists(base_path).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 218 | files_list = self.mox.CreateMockAnything() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 219 | gclient_scm.RunSVNAndGetFileList(options, ['checkout', self.url, |
[email protected] | 22e29d4 | 2009-10-28 00:48:26 | [diff] [blame] | 220 | base_path], self.root_dir, files_list) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 221 | self.mox.ReplayAll() |
| 222 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 223 | relpath=self.relpath) |
| 224 | scm.update(options, (), files_list) |
| 225 | |
| 226 | def testUpdateUpdate(self): |
| 227 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 228 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 229 | options.force = True |
| 230 | options.nohooks = False |
| 231 | file_info = { |
| 232 | 'Repository Root': 'blah', |
| 233 | 'URL': self.url, |
| 234 | 'UUID': 'ABC', |
| 235 | 'Revision': 42, |
| 236 | } |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 237 | gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git') |
| 238 | ).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 239 | # Checkout or update. |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 240 | gclient_scm.os.path.exists(base_path).AndReturn(True) |
| 241 | gclient_scm.CaptureSVNInfo(gclient_scm.os.path.join(base_path, "."), '.' |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 242 | ).AndReturn(file_info) |
| 243 | # Cheat a bit here. |
| 244 | gclient_scm.CaptureSVNInfo(file_info['URL'], '.').AndReturn(file_info) |
| 245 | additional_args = [] |
| 246 | if options.manually_grab_svn_rev: |
| 247 | additional_args = ['--revision', str(file_info['Revision'])] |
| 248 | files_list = [] |
[email protected] | 22e29d4 | 2009-10-28 00:48:26 | [diff] [blame] | 249 | gclient_scm.RunSVNAndGetFileList(options, |
| 250 | ['update', base_path] + additional_args, |
[email protected] | 7753d24 | 2009-10-07 17:40:24 | [diff] [blame] | 251 | self.root_dir, files_list) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 252 | |
| 253 | self.mox.ReplayAll() |
| 254 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 255 | relpath=self.relpath) |
| 256 | scm.update(options, (), files_list) |
| 257 | |
| 258 | def testUpdateGit(self): |
| 259 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 260 | file_path = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') |
| 261 | gclient_scm.os.path.exists(file_path).AndReturn(True) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 262 | print("________ found .git directory; skipping %s" % self.relpath) |
| 263 | |
| 264 | self.mox.ReplayAll() |
| 265 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 266 | relpath=self.relpath) |
| 267 | file_list = [] |
| 268 | scm.update(options, self.args, file_list) |
| 269 | |
| 270 | def testGetSVNFileInfo(self): |
| 271 | xml_text = r"""<?xml version="1.0"?> |
| 272 | <info> |
| 273 | <entry kind="file" path="%s" revision="14628"> |
| 274 | <url>http://src.chromium.org/svn/trunk/src/chrome/app/d</url> |
| 275 | <repository><root>http://src.chromium.org/svn</root></repository> |
| 276 | <wc-info> |
| 277 | <schedule>add</schedule> |
| 278 | <depth>infinity</depth> |
| 279 | <copy-from-url>http://src.chromium.org/svn/trunk/src/chrome/app/DEPS</copy-from-url> |
| 280 | <copy-from-rev>14628</copy-from-rev> |
| 281 | <checksum>369f59057ba0e6d9017e28f8bdfb1f43</checksum> |
| 282 | </wc-info> |
| 283 | </entry> |
| 284 | </info> |
| 285 | """ % self.url |
| 286 | gclient_scm.CaptureSVN(['info', '--xml', self.url], |
| 287 | '.', True).AndReturn(xml_text) |
| 288 | expected = { |
| 289 | 'URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/d', |
| 290 | 'UUID': None, |
| 291 | 'Repository Root': 'http://src.chromium.org/svn', |
| 292 | 'Schedule': 'add', |
| 293 | 'Copied From URL': |
| 294 | 'http://src.chromium.org/svn/trunk/src/chrome/app/DEPS', |
| 295 | 'Copied From Rev': '14628', |
| 296 | 'Path': self.url, |
| 297 | 'Revision': 14628, |
| 298 | 'Node Kind': 'file', |
| 299 | } |
| 300 | self.mox.ReplayAll() |
| 301 | file_info = self._CaptureSVNInfo(self.url, '.', True) |
| 302 | self.assertEquals(sorted(file_info.items()), sorted(expected.items())) |
| 303 | |
| 304 | def testCaptureSvnInfo(self): |
| 305 | xml_text = """<?xml version="1.0"?> |
| 306 | <info> |
| 307 | <entry |
| 308 | kind="dir" |
| 309 | path="." |
| 310 | revision="35"> |
| 311 | <url>%s</url> |
| 312 | <repository> |
| 313 | <root>%s</root> |
| 314 | <uuid>7b9385f5-0452-0410-af26-ad4892b7a1fb</uuid> |
| 315 | </repository> |
| 316 | <wc-info> |
| 317 | <schedule>normal</schedule> |
| 318 | <depth>infinity</depth> |
| 319 | </wc-info> |
| 320 | <commit |
| 321 | revision="35"> |
| 322 | <author>maruel</author> |
| 323 | <date>2008-12-04T20:12:19.685120Z</date> |
| 324 | </commit> |
| 325 | </entry> |
| 326 | </info> |
| 327 | """ % (self.url, self.root_dir) |
| 328 | gclient_scm.CaptureSVN(['info', '--xml', |
| 329 | self.url], '.', True).AndReturn(xml_text) |
| 330 | self.mox.ReplayAll() |
| 331 | file_info = self._CaptureSVNInfo(self.url, '.', True) |
| 332 | expected = { |
| 333 | 'URL': self.url, |
| 334 | 'UUID': '7b9385f5-0452-0410-af26-ad4892b7a1fb', |
| 335 | 'Revision': 35, |
| 336 | 'Repository Root': self.root_dir, |
| 337 | 'Schedule': 'normal', |
| 338 | 'Copied From URL': None, |
| 339 | 'Copied From Rev': None, |
| 340 | 'Path': '.', |
| 341 | 'Node Kind': 'dir', |
| 342 | } |
| 343 | self.assertEqual(file_info, expected) |
| 344 | |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 345 | def testRevinfo(self): |
| 346 | options = self.Options(verbose=False) |
| 347 | xml_text = """<?xml version="1.0"?> |
| 348 | <info> |
| 349 | <entry |
| 350 | kind="dir" |
| 351 | path="." |
| 352 | revision="35"> |
| 353 | <url>%s</url> |
| 354 | <repository> |
| 355 | <root>%s</root> |
| 356 | <uuid>7b9385f5-0452-0410-af26-ad4892b7a1fb</uuid> |
| 357 | </repository> |
| 358 | <wc-info> |
| 359 | <schedule>normal</schedule> |
| 360 | <depth>infinity</depth> |
| 361 | </wc-info> |
| 362 | <commit |
| 363 | revision="35"> |
| 364 | <author>maruel</author> |
| 365 | <date>2008-12-04T20:12:19.685120Z</date> |
| 366 | </commit> |
| 367 | </entry> |
| 368 | </info> |
| 369 | """ % (self.url, self.root_dir) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 370 | gclient_scm.os.getcwd().AndReturn('bleh') |
| 371 | gclient_scm.CaptureSVN(['info', '--xml', self.url], 'bleh' |
| 372 | ).AndReturn(xml_text) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 373 | self.mox.ReplayAll() |
| 374 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 375 | relpath=self.relpath) |
| 376 | rev_info = scm.revinfo(options, self.args, None) |
| 377 | self.assertEqual(rev_info, '35') |
| 378 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 379 | |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 380 | class GitWrapperTestCase(SuperMoxBaseTestBase): |
| 381 | """This class doesn't use pymox.""" |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 382 | class OptionsObject(object): |
| 383 | def __init__(self, test_case, verbose=False, revision=None): |
| 384 | self.verbose = verbose |
| 385 | self.revision = revision |
| 386 | self.manually_grab_svn_rev = True |
| 387 | self.deps_os = None |
| 388 | self.force = False |
| 389 | self.nohooks = False |
| 390 | |
| 391 | sample_git_import = """blob |
| 392 | mark :1 |
| 393 | data 6 |
| 394 | Hello |
| 395 | |
| 396 | blob |
| 397 | mark :2 |
| 398 | data 4 |
| 399 | Bye |
| 400 | |
| 401 | reset refs/heads/master |
| 402 | commit refs/heads/master |
| 403 | mark :3 |
| 404 | author Bob <[email protected]> 1253744361 -0700 |
| 405 | committer Bob <[email protected]> 1253744361 -0700 |
| 406 | data 8 |
| 407 | A and B |
| 408 | M 100644 :1 a |
| 409 | M 100644 :2 b |
| 410 | |
| 411 | blob |
| 412 | mark :4 |
| 413 | data 10 |
| 414 | Hello |
| 415 | You |
| 416 | |
| 417 | blob |
| 418 | mark :5 |
| 419 | data 8 |
| 420 | Bye |
| 421 | You |
| 422 | |
| 423 | commit refs/heads/origin |
| 424 | mark :6 |
| 425 | author Alice <[email protected]> 1253744424 -0700 |
| 426 | committer Alice <[email protected]> 1253744424 -0700 |
| 427 | data 13 |
| 428 | Personalized |
| 429 | from :3 |
| 430 | M 100644 :4 a |
| 431 | M 100644 :5 b |
| 432 | |
| 433 | reset refs/heads/master |
| 434 | from :3 |
| 435 | """ |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 436 | def Options(self, *args, **kwargs): |
| 437 | return self.OptionsObject(self, *args, **kwargs) |
| 438 | |
| 439 | def CreateGitRepo(self, git_import, path): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 440 | try: |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 441 | Popen(['git', 'init'], stdout=PIPE, stderr=STDOUT, |
| 442 | cwd=path).communicate() |
| 443 | except OSError: |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 444 | # git is not available, skip this test. |
| 445 | return False |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 446 | Popen(['git', 'fast-import'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, |
| 447 | cwd=path).communicate(input=git_import) |
| 448 | Popen(['git', 'checkout'], stdout=PIPE, stderr=STDOUT, |
| 449 | cwd=path).communicate() |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 450 | return True |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 451 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 452 | def setUp(self): |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 453 | self.args = self.Args() |
| 454 | self.url = 'git://foo' |
| 455 | self.root_dir = tempfile.mkdtemp() |
| 456 | self.relpath = '.' |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 457 | self.base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 458 | self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 459 | SuperMoxBaseTestBase.setUp(self) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 460 | |
| 461 | def tearDown(self): |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 462 | SuperMoxBaseTestBase.tearDown(self) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 463 | shutil.rmtree(self.root_dir) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 464 | |
| 465 | def testDir(self): |
| 466 | members = [ |
| 467 | 'FullUrlForRelativeUrl', 'RunCommand', 'cleanup', 'diff', 'export', |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 468 | 'relpath', 'revert', 'revinfo', 'runhooks', 'scm_name', 'status', |
| 469 | 'update', 'url', |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 470 | ] |
| 471 | |
| 472 | # If you add a member, be sure to add the relevant test! |
| 473 | self.compareMembers(gclient_scm.CreateSCM(url=self.url), members) |
| 474 | |
| 475 | def testRevertMissing(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 476 | if not self.enabled: |
| 477 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 478 | options = self.Options() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 479 | file_path = gclient_scm.os.path.join(self.base_path, 'a') |
| 480 | gclient_scm.os.remove(file_path) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 481 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 482 | relpath=self.relpath) |
| 483 | file_list = [] |
| 484 | scm.revert(options, self.args, file_list) |
| 485 | self.assertEquals(file_list, [file_path]) |
| 486 | file_list = [] |
| 487 | scm.diff(options, self.args, file_list) |
| 488 | self.assertEquals(file_list, []) |
| 489 | |
| 490 | def testRevertNone(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 491 | if not self.enabled: |
| 492 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 493 | options = self.Options() |
| 494 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 495 | relpath=self.relpath) |
| 496 | file_list = [] |
| 497 | scm.revert(options, self.args, file_list) |
| 498 | self.assertEquals(file_list, []) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 499 | self.assertEquals(scm.revinfo(options, self.args, None), |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 500 | '069c602044c5388d2d15c3f875b057c852003458') |
| 501 | |
| 502 | |
| 503 | def testRevertModified(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 504 | if not self.enabled: |
| 505 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 506 | options = self.Options() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 507 | file_path = gclient_scm.os.path.join(self.base_path, 'a') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 508 | open(file_path, 'a').writelines('touched\n') |
| 509 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 510 | relpath=self.relpath) |
| 511 | file_list = [] |
| 512 | scm.revert(options, self.args, file_list) |
| 513 | self.assertEquals(file_list, [file_path]) |
| 514 | file_list = [] |
| 515 | scm.diff(options, self.args, file_list) |
| 516 | self.assertEquals(file_list, []) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 517 | self.assertEquals(scm.revinfo(options, self.args, None), |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 518 | '069c602044c5388d2d15c3f875b057c852003458') |
| 519 | |
| 520 | def testRevertNew(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 521 | if not self.enabled: |
| 522 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 523 | options = self.Options() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 524 | file_path = gclient_scm.os.path.join(self.base_path, 'c') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 525 | f = open(file_path, 'w') |
| 526 | f.writelines('new\n') |
| 527 | f.close() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 528 | Popen(['git', 'add', 'c'], stdout=PIPE, |
| 529 | stderr=STDOUT, cwd=self.base_path).communicate() |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 530 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 531 | relpath=self.relpath) |
| 532 | file_list = [] |
| 533 | scm.revert(options, self.args, file_list) |
| 534 | self.assertEquals(file_list, [file_path]) |
| 535 | file_list = [] |
| 536 | scm.diff(options, self.args, file_list) |
| 537 | self.assertEquals(file_list, []) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 538 | self.assertEquals(scm.revinfo(options, self.args, None), |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 539 | '069c602044c5388d2d15c3f875b057c852003458') |
| 540 | |
| 541 | def testStatusNew(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 542 | if not self.enabled: |
| 543 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 544 | options = self.Options() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 545 | file_path = gclient_scm.os.path.join(self.base_path, 'a') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 546 | open(file_path, 'a').writelines('touched\n') |
| 547 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 548 | relpath=self.relpath) |
| 549 | file_list = [] |
| 550 | scm.status(options, self.args, file_list) |
| 551 | self.assertEquals(file_list, [file_path]) |
| 552 | |
| 553 | def testStatus2New(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 554 | if not self.enabled: |
| 555 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 556 | options = self.Options() |
| 557 | expected_file_list = [] |
| 558 | for f in ['a', 'b']: |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 559 | file_path = gclient_scm.os.path.join(self.base_path, f) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 560 | open(file_path, 'a').writelines('touched\n') |
| 561 | expected_file_list.extend([file_path]) |
| 562 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 563 | relpath=self.relpath) |
| 564 | file_list = [] |
| 565 | scm.status(options, self.args, file_list) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 566 | expected_file_list = [gclient_scm.os.path.join(self.base_path, x) |
| 567 | for x in ['a', 'b']] |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 568 | self.assertEquals(sorted(file_list), expected_file_list) |
| 569 | |
| 570 | def testUpdateCheckout(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 571 | if not self.enabled: |
| 572 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 573 | options = self.Options(verbose=True) |
| 574 | root_dir = tempfile.mkdtemp() |
| 575 | relpath = 'foo' |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 576 | base_path = gclient_scm.os.path.join(root_dir, relpath) |
| 577 | url = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 578 | try: |
| 579 | scm = gclient_scm.CreateSCM(url=url, root_dir=root_dir, |
| 580 | relpath=relpath) |
| 581 | file_list = [] |
| 582 | scm.update(options, (), file_list) |
| 583 | self.assertEquals(len(file_list), 2) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 584 | self.assert_(gclient_scm.os.path.isfile( |
| 585 | gclient_scm.os.path.join(base_path, 'a'))) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 586 | self.assertEquals(scm.revinfo(options, (), None), |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 587 | '069c602044c5388d2d15c3f875b057c852003458') |
| 588 | finally: |
| 589 | shutil.rmtree(root_dir) |
| 590 | |
| 591 | def testUpdateUpdate(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 592 | if not self.enabled: |
| 593 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 594 | options = self.Options() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 595 | expected_file_list = [gclient_scm.os.path.join(self.base_path, x) |
| 596 | for x in ['a', 'b']] |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 597 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 598 | relpath=self.relpath) |
| 599 | file_list = [] |
| 600 | scm.update(options, (), file_list) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 601 | self.assertEquals(file_list, expected_file_list) |
| 602 | self.assertEquals(scm.revinfo(options, (), None), |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 603 | 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
| 604 | |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 605 | def testRevinfo(self): |
| 606 | if not self.enabled: |
| 607 | return |
| 608 | options = self.Options() |
| 609 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 610 | relpath=self.relpath) |
| 611 | rev_info = scm.revinfo(options, (), None) |
| 612 | self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') |
| 613 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 614 | |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 615 | class RunSVNTestCase(BaseTestCase): |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 616 | def testRunSVN(self): |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 617 | self.UnMock(gclient_scm, 'RunSVN') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 618 | param2 = 'bleh' |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 619 | gclient_scm.gclient_utils.SubprocessCall(['svn', 'foo', 'bar'], |
| 620 | param2).AndReturn(None) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 621 | self.mox.ReplayAll() |
| 622 | gclient_scm.RunSVN(['foo', 'bar'], param2) |
| 623 | |
| 624 | |
| 625 | if __name__ == '__main__': |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame^] | 626 | import unittest |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 627 | unittest.main() |
| 628 | |
| 629 | # vim: ts=2:sw=2:tw=80:et: |