[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') |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 36 | self._CaptureSVNInfo = gclient_scm.scm.SVN.CaptureInfo |
| 37 | self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'Capture') |
| 38 | self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureInfo') |
| 39 | self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureStatus') |
| 40 | self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'Run') |
| 41 | self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'RunAndGetFileList') |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 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 = [ |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 67 | 'COMMAND', 'Capture', 'CaptureHeadRevision', 'CaptureInfo', |
[email protected] | c532e17 | 2009-12-15 17:18:32 | [diff] [blame^] | 68 | 'CaptureStatus', 'DiffItem', 'GetEmail', |
[email protected] | 37c629a | 2009-11-24 23:34:43 | [diff] [blame] | 69 | 'GetFileProperty', 'IsMoved', 'ReadSimpleAuth', 'Run', |
[email protected] | c78f246 | 2009-11-21 01:20:57 | [diff] [blame] | 70 | 'RunAndFilterOutput', 'RunAndGetFileList', |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 71 | 'RunCommand', 'cleanup', 'diff', 'export', 'pack', 'relpath', 'revert', |
| 72 | 'revinfo', 'runhooks', 'scm_name', 'status', 'update', 'url', |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 73 | ] |
| 74 | |
| 75 | # If you add a member, be sure to add the relevant test! |
| 76 | self.compareMembers(self._scm_wrapper(), members) |
| 77 | |
| 78 | def testUnsupportedSCM(self): |
| 79 | args = [self.url, self.root_dir, self.relpath] |
| 80 | kwargs = {'scm_name' : 'foo'} |
| 81 | exception_msg = 'Unsupported scm %(scm_name)s' % kwargs |
| 82 | self.assertRaisesError(exception_msg, self._scm_wrapper, *args, **kwargs) |
| 83 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 84 | def testRunCommandException(self): |
| 85 | options = self.Options(verbose=False) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 86 | file_path = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') |
| 87 | gclient_scm.os.path.exists(file_path).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 88 | |
| 89 | self.mox.ReplayAll() |
| 90 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 91 | relpath=self.relpath) |
| 92 | exception = "Unsupported argument(s): %s" % ','.join(self.args) |
| 93 | self.assertRaisesError(exception, scm.RunCommand, |
| 94 | 'update', options, self.args) |
| 95 | |
| 96 | def testRunCommandUnknown(self): |
| 97 | # TODO(maruel): if ever used. |
| 98 | pass |
| 99 | |
| 100 | def testRevertMissing(self): |
| 101 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 102 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 103 | gclient_scm.os.path.isdir(base_path).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 104 | # It'll to a checkout instead. |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 105 | gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git') |
| 106 | ).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 107 | print("\n_____ %s is missing, synching instead" % self.relpath) |
| 108 | # Checkout. |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 109 | gclient_scm.os.path.exists(base_path).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 110 | files_list = self.mox.CreateMockAnything() |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 111 | gclient_scm.scm.SVN.RunAndGetFileList(options, |
| 112 | ['checkout', self.url, base_path], |
| 113 | self.root_dir, files_list) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 114 | |
| 115 | self.mox.ReplayAll() |
| 116 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 117 | relpath=self.relpath) |
| 118 | scm.revert(options, self.args, files_list) |
| 119 | |
| 120 | def testRevertNone(self): |
| 121 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 122 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 123 | gclient_scm.os.path.isdir(base_path).AndReturn(True) |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 124 | gclient_scm.scm.SVN.CaptureStatus(base_path).AndReturn([]) |
| 125 | gclient_scm.scm.SVN.RunAndGetFileList(options, |
| 126 | ['update', '--revision', 'BASE'], |
| 127 | base_path, mox.IgnoreArg()) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 128 | |
| 129 | self.mox.ReplayAll() |
| 130 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 131 | relpath=self.relpath) |
| 132 | file_list = [] |
| 133 | scm.revert(options, self.args, file_list) |
| 134 | |
| 135 | def testRevert2Files(self): |
| 136 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 137 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 138 | gclient_scm.os.path.isdir(base_path).AndReturn(True) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 139 | items = [ |
| 140 | ('M ', 'a'), |
| 141 | ('A ', 'b'), |
| 142 | ] |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 143 | file_path1 = gclient_scm.os.path.join(base_path, 'a') |
| 144 | file_path2 = gclient_scm.os.path.join(base_path, 'b') |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 145 | gclient_scm.scm.SVN.CaptureStatus(base_path).AndReturn(items) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 146 | gclient_scm.os.path.exists(file_path1).AndReturn(True) |
| 147 | gclient_scm.os.path.isfile(file_path1).AndReturn(True) |
| 148 | gclient_scm.os.remove(file_path1) |
| 149 | gclient_scm.os.path.exists(file_path2).AndReturn(True) |
| 150 | gclient_scm.os.path.isfile(file_path2).AndReturn(True) |
| 151 | gclient_scm.os.remove(file_path2) |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 152 | gclient_scm.scm.SVN.RunAndGetFileList(options, |
| 153 | ['update', '--revision', 'BASE'], |
| 154 | base_path, mox.IgnoreArg()) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 155 | print(gclient_scm.os.path.join(base_path, 'a')) |
| 156 | print(gclient_scm.os.path.join(base_path, 'b')) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 157 | |
| 158 | self.mox.ReplayAll() |
| 159 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 160 | relpath=self.relpath) |
| 161 | file_list = [] |
| 162 | scm.revert(options, self.args, file_list) |
| 163 | |
| 164 | def testRevertDirectory(self): |
| 165 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 166 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 167 | gclient_scm.os.path.isdir(base_path).AndReturn(True) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 168 | items = [ |
| 169 | ('~ ', 'a'), |
| 170 | ] |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 171 | gclient_scm.scm.SVN.CaptureStatus(base_path).AndReturn(items) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 172 | file_path = gclient_scm.os.path.join(base_path, 'a') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 173 | print(file_path) |
| 174 | gclient_scm.os.path.exists(file_path).AndReturn(True) |
| 175 | gclient_scm.os.path.isfile(file_path).AndReturn(False) |
| 176 | gclient_scm.os.path.isdir(file_path).AndReturn(True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 177 | gclient_scm.gclient_utils.RemoveDirectory(file_path) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 178 | file_list1 = [] |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 179 | gclient_scm.scm.SVN.RunAndGetFileList(options, |
| 180 | ['update', '--revision', 'BASE'], |
| 181 | base_path, mox.IgnoreArg()) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 182 | |
| 183 | self.mox.ReplayAll() |
| 184 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 185 | relpath=self.relpath) |
| 186 | file_list2 = [] |
| 187 | scm.revert(options, self.args, file_list2) |
| 188 | |
| 189 | def testStatus(self): |
| 190 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 191 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 192 | gclient_scm.os.path.isdir(base_path).AndReturn(True) |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 193 | gclient_scm.scm.SVN.RunAndGetFileList(options, |
| 194 | ['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] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 219 | gclient_scm.scm.SVN.RunAndGetFileList(options, |
| 220 | ['checkout', self.url, base_path], |
| 221 | self.root_dir, files_list) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 222 | self.mox.ReplayAll() |
| 223 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 224 | relpath=self.relpath) |
| 225 | scm.update(options, (), files_list) |
| 226 | |
| 227 | def testUpdateUpdate(self): |
| 228 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 229 | base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 230 | options.force = True |
| 231 | options.nohooks = False |
| 232 | file_info = { |
| 233 | 'Repository Root': 'blah', |
| 234 | 'URL': self.url, |
| 235 | 'UUID': 'ABC', |
| 236 | 'Revision': 42, |
| 237 | } |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 238 | gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git') |
| 239 | ).AndReturn(False) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 240 | # Checkout or update. |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 241 | gclient_scm.os.path.exists(base_path).AndReturn(True) |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 242 | gclient_scm.scm.SVN.CaptureInfo( |
| 243 | gclient_scm.os.path.join(base_path, "."), '.' |
| 244 | ).AndReturn(file_info) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 245 | # Cheat a bit here. |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 246 | gclient_scm.scm.SVN.CaptureInfo(file_info['URL'], '.').AndReturn(file_info) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 247 | additional_args = [] |
| 248 | if options.manually_grab_svn_rev: |
| 249 | additional_args = ['--revision', str(file_info['Revision'])] |
| 250 | files_list = [] |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 251 | gclient_scm.scm.SVN.RunAndGetFileList( |
| 252 | options, |
| 253 | ['update', base_path] + additional_args, |
| 254 | self.root_dir, files_list) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 255 | |
| 256 | self.mox.ReplayAll() |
| 257 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 258 | relpath=self.relpath) |
| 259 | scm.update(options, (), files_list) |
| 260 | |
| 261 | def testUpdateGit(self): |
| 262 | options = self.Options(verbose=True) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 263 | file_path = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') |
| 264 | gclient_scm.os.path.exists(file_path).AndReturn(True) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 265 | print("________ found .git directory; skipping %s" % self.relpath) |
| 266 | |
| 267 | self.mox.ReplayAll() |
| 268 | scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 269 | relpath=self.relpath) |
| 270 | file_list = [] |
| 271 | scm.update(options, self.args, file_list) |
| 272 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 273 | |
[email protected] | 5bde485 | 2009-12-14 16:47:12 | [diff] [blame] | 274 | class GitWrapperTestCase(BaseTestCase): |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 275 | """This class doesn't use pymox.""" |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 276 | class OptionsObject(object): |
| 277 | def __init__(self, test_case, verbose=False, revision=None): |
| 278 | self.verbose = verbose |
| 279 | self.revision = revision |
| 280 | self.manually_grab_svn_rev = True |
| 281 | self.deps_os = None |
| 282 | self.force = False |
| 283 | self.nohooks = False |
| 284 | |
| 285 | sample_git_import = """blob |
| 286 | mark :1 |
| 287 | data 6 |
| 288 | Hello |
| 289 | |
| 290 | blob |
| 291 | mark :2 |
| 292 | data 4 |
| 293 | Bye |
| 294 | |
| 295 | reset refs/heads/master |
| 296 | commit refs/heads/master |
| 297 | mark :3 |
| 298 | author Bob <[email protected]> 1253744361 -0700 |
| 299 | committer Bob <[email protected]> 1253744361 -0700 |
| 300 | data 8 |
| 301 | A and B |
| 302 | M 100644 :1 a |
| 303 | M 100644 :2 b |
| 304 | |
| 305 | blob |
| 306 | mark :4 |
| 307 | data 10 |
| 308 | Hello |
| 309 | You |
| 310 | |
| 311 | blob |
| 312 | mark :5 |
| 313 | data 8 |
| 314 | Bye |
| 315 | You |
| 316 | |
| 317 | commit refs/heads/origin |
| 318 | mark :6 |
| 319 | author Alice <[email protected]> 1253744424 -0700 |
| 320 | committer Alice <[email protected]> 1253744424 -0700 |
| 321 | data 13 |
| 322 | Personalized |
| 323 | from :3 |
| 324 | M 100644 :4 a |
| 325 | M 100644 :5 b |
| 326 | |
| 327 | reset refs/heads/master |
| 328 | from :3 |
| 329 | """ |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 330 | def Options(self, *args, **kwargs): |
| 331 | return self.OptionsObject(self, *args, **kwargs) |
| 332 | |
| 333 | def CreateGitRepo(self, git_import, path): |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 334 | """Do it for real.""" |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 335 | try: |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 336 | Popen(['git', 'init'], stdout=PIPE, stderr=STDOUT, |
| 337 | cwd=path).communicate() |
| 338 | except OSError: |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 339 | # git is not available, skip this test. |
| 340 | return False |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 341 | Popen(['git', 'fast-import'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, |
| 342 | cwd=path).communicate(input=git_import) |
| 343 | Popen(['git', 'checkout'], stdout=PIPE, stderr=STDOUT, |
| 344 | cwd=path).communicate() |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 345 | return True |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 346 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 347 | def setUp(self): |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 348 | self.args = self.Args() |
| 349 | self.url = 'git://foo' |
| 350 | self.root_dir = tempfile.mkdtemp() |
| 351 | self.relpath = '.' |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 352 | self.base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 353 | self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 354 | SuperMoxBaseTestBase.setUp(self) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 355 | |
| 356 | def tearDown(self): |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 357 | SuperMoxBaseTestBase.tearDown(self) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 358 | shutil.rmtree(self.root_dir) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 359 | |
| 360 | def testDir(self): |
| 361 | members = [ |
[email protected] | c532e17 | 2009-12-15 17:18:32 | [diff] [blame^] | 362 | 'COMMAND', 'Capture', 'CaptureStatus', 'GetEmail', |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 363 | 'RunCommand', 'cleanup', 'diff', 'export', 'relpath', 'revert', |
| 364 | 'revinfo', 'runhooks', 'scm_name', 'status', 'update', 'url', |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 365 | ] |
| 366 | |
| 367 | # If you add a member, be sure to add the relevant test! |
| 368 | self.compareMembers(gclient_scm.CreateSCM(url=self.url), members) |
| 369 | |
| 370 | def testRevertMissing(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 371 | if not self.enabled: |
| 372 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 373 | options = self.Options() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 374 | file_path = gclient_scm.os.path.join(self.base_path, 'a') |
| 375 | gclient_scm.os.remove(file_path) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 376 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 377 | relpath=self.relpath) |
| 378 | file_list = [] |
| 379 | scm.revert(options, self.args, file_list) |
| 380 | self.assertEquals(file_list, [file_path]) |
| 381 | file_list = [] |
| 382 | scm.diff(options, self.args, file_list) |
| 383 | self.assertEquals(file_list, []) |
| 384 | |
| 385 | def testRevertNone(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 386 | if not self.enabled: |
| 387 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 388 | options = self.Options() |
| 389 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 390 | relpath=self.relpath) |
| 391 | file_list = [] |
| 392 | scm.revert(options, self.args, file_list) |
| 393 | self.assertEquals(file_list, []) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 394 | self.assertEquals(scm.revinfo(options, self.args, None), |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 395 | '069c602044c5388d2d15c3f875b057c852003458') |
| 396 | |
| 397 | |
| 398 | def testRevertModified(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 399 | if not self.enabled: |
| 400 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 401 | options = self.Options() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 402 | file_path = gclient_scm.os.path.join(self.base_path, 'a') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 403 | open(file_path, 'a').writelines('touched\n') |
| 404 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 405 | relpath=self.relpath) |
| 406 | file_list = [] |
| 407 | scm.revert(options, self.args, file_list) |
| 408 | self.assertEquals(file_list, [file_path]) |
| 409 | file_list = [] |
| 410 | scm.diff(options, self.args, file_list) |
| 411 | self.assertEquals(file_list, []) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 412 | self.assertEquals(scm.revinfo(options, self.args, None), |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 413 | '069c602044c5388d2d15c3f875b057c852003458') |
| 414 | |
| 415 | def testRevertNew(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 416 | if not self.enabled: |
| 417 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 418 | options = self.Options() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 419 | file_path = gclient_scm.os.path.join(self.base_path, 'c') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 420 | f = open(file_path, 'w') |
| 421 | f.writelines('new\n') |
| 422 | f.close() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 423 | Popen(['git', 'add', 'c'], stdout=PIPE, |
| 424 | stderr=STDOUT, cwd=self.base_path).communicate() |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 425 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 426 | relpath=self.relpath) |
| 427 | file_list = [] |
| 428 | scm.revert(options, self.args, file_list) |
| 429 | self.assertEquals(file_list, [file_path]) |
| 430 | file_list = [] |
| 431 | scm.diff(options, self.args, file_list) |
| 432 | self.assertEquals(file_list, []) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 433 | self.assertEquals(scm.revinfo(options, self.args, None), |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 434 | '069c602044c5388d2d15c3f875b057c852003458') |
| 435 | |
| 436 | def testStatusNew(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 437 | if not self.enabled: |
| 438 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 439 | options = self.Options() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 440 | file_path = gclient_scm.os.path.join(self.base_path, 'a') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 441 | open(file_path, 'a').writelines('touched\n') |
| 442 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 443 | relpath=self.relpath) |
| 444 | file_list = [] |
| 445 | scm.status(options, self.args, file_list) |
| 446 | self.assertEquals(file_list, [file_path]) |
| 447 | |
| 448 | def testStatus2New(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 449 | if not self.enabled: |
| 450 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 451 | options = self.Options() |
| 452 | expected_file_list = [] |
| 453 | for f in ['a', 'b']: |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 454 | file_path = gclient_scm.os.path.join(self.base_path, f) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 455 | open(file_path, 'a').writelines('touched\n') |
| 456 | expected_file_list.extend([file_path]) |
| 457 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 458 | relpath=self.relpath) |
| 459 | file_list = [] |
| 460 | scm.status(options, self.args, file_list) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 461 | expected_file_list = [gclient_scm.os.path.join(self.base_path, x) |
| 462 | for x in ['a', 'b']] |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 463 | self.assertEquals(sorted(file_list), expected_file_list) |
| 464 | |
| 465 | def testUpdateCheckout(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 466 | if not self.enabled: |
| 467 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 468 | options = self.Options(verbose=True) |
| 469 | root_dir = tempfile.mkdtemp() |
| 470 | relpath = 'foo' |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 471 | base_path = gclient_scm.os.path.join(root_dir, relpath) |
| 472 | url = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 473 | try: |
| 474 | scm = gclient_scm.CreateSCM(url=url, root_dir=root_dir, |
| 475 | relpath=relpath) |
| 476 | file_list = [] |
| 477 | scm.update(options, (), file_list) |
| 478 | self.assertEquals(len(file_list), 2) |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 479 | self.assert_(gclient_scm.os.path.isfile( |
| 480 | gclient_scm.os.path.join(base_path, 'a'))) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 481 | self.assertEquals(scm.revinfo(options, (), None), |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 482 | '069c602044c5388d2d15c3f875b057c852003458') |
| 483 | finally: |
| 484 | shutil.rmtree(root_dir) |
| 485 | |
| 486 | def testUpdateUpdate(self): |
[email protected] | ea6c2c5 | 2009-10-09 20:38:14 | [diff] [blame] | 487 | if not self.enabled: |
| 488 | return |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 489 | options = self.Options() |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 490 | expected_file_list = [gclient_scm.os.path.join(self.base_path, x) |
| 491 | for x in ['a', 'b']] |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 492 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 493 | relpath=self.relpath) |
| 494 | file_list = [] |
| 495 | scm.update(options, (), file_list) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 496 | self.assertEquals(file_list, expected_file_list) |
| 497 | self.assertEquals(scm.revinfo(options, (), None), |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 498 | 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
| 499 | |
[email protected] | 5bde485 | 2009-12-14 16:47:12 | [diff] [blame] | 500 | def testUpdateConflict(self): |
| 501 | if not self.enabled: |
| 502 | return |
| 503 | options = self.Options() |
| 504 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 505 | relpath=self.relpath) |
| 506 | file_path = gclient_scm.os.path.join(self.base_path, 'b') |
| 507 | f = open(file_path, 'w').writelines('conflict\n') |
| 508 | scm._Run(['commit', '-am', 'test']) |
| 509 | exception = \ |
| 510 | '\n____ .\n' \ |
| 511 | '\nConflict while rebasing this branch.\n' \ |
| 512 | 'Fix the conflict and run gclient again.\n' \ |
| 513 | 'See man git-rebase for details.\n' |
| 514 | self.assertRaisesError(exception, scm.update, options, (), []) |
| 515 | exception = \ |
| 516 | '\n____ .\n' \ |
| 517 | '\tAlready in a conflict, i.e. (no branch).\n' \ |
| 518 | '\tFix the conflict and run gclient again.\n' \ |
| 519 | '\tOr to abort run:\n\t\tgit-rebase --abort\n' \ |
| 520 | '\tSee man git-rebase for details.\n' |
| 521 | self.assertRaisesError(exception, scm.update, options, (), []) |
| 522 | |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 523 | def testRevinfo(self): |
| 524 | if not self.enabled: |
| 525 | return |
| 526 | options = self.Options() |
| 527 | scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 528 | relpath=self.relpath) |
| 529 | rev_info = scm.revinfo(options, (), None) |
| 530 | self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') |
| 531 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 532 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 533 | if __name__ == '__main__': |
[email protected] | 8ef5f54 | 2009-11-12 02:05:02 | [diff] [blame] | 534 | import unittest |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 535 | unittest.main() |
| 536 | |
| 537 | # vim: ts=2:sw=2:tw=80:et: |