blob: 4f8b656fb124f9e0de5b6bf7919908d1c583b76b [file] [log] [blame]
msb@chromium.orge28e4982009-09-25 20:51:45 +00001#!/usr/bin/python
maruel@chromium.orgba551772010-02-03 18:21:42 +00002# Copyright (c) 2010 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
msb@chromium.orge28e4982009-09-25 20:51:45 +00005
6"""Unit tests for gclient_scm.py."""
7
maruel@chromium.orgba551772010-02-03 18:21:42 +00008# Import before super_mox to keep valid references.
9from os import rename
10from shutil import rmtree
maruel@chromium.org8ef5f542009-11-12 02:05:02 +000011from subprocess import Popen, PIPE, STDOUT
msb@chromium.orge28e4982009-09-25 20:51:45 +000012import tempfile
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +000013import __builtin__
msb@chromium.orge28e4982009-09-25 20:51:45 +000014
maruel@chromium.orgba551772010-02-03 18:21:42 +000015# Fixes include path.
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +000016from super_mox import mox, SuperMoxBaseTestBase, SuperMoxTestBase
maruel@chromium.orgba551772010-02-03 18:21:42 +000017
msb@chromium.orge28e4982009-09-25 20:51:45 +000018import gclient_scm
maruel@chromium.org96913eb2010-06-01 16:22:47 +000019
20
21class GCBaseTestCase(SuperMoxTestBase):
22 # Like unittest's assertRaises, but checks for Gclient.Error.
23 def assertRaisesError(self, msg, fn, *args, **kwargs):
24 try:
25 fn(*args, **kwargs)
26 except gclient_scm.gclient_utils.Error, e:
27 self.assertEquals(e.args[0], msg)
28 else:
29 self.fail('%s not raised' % msg)
msb@chromium.orge28e4982009-09-25 20:51:45 +000030
31
maruel@chromium.org8ef5f542009-11-12 02:05:02 +000032class BaseTestCase(GCBaseTestCase):
33 def setUp(self):
34 GCBaseTestCase.setUp(self)
35 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead')
36 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite')
37 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'SubprocessCall')
38 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'RemoveDirectory')
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000039 self._CaptureSVNInfo = gclient_scm.scm.SVN.CaptureInfo
40 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'Capture')
41 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureInfo')
42 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureStatus')
43 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'Run')
44 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'RunAndGetFileList')
maruel@chromium.org8ef5f542009-11-12 02:05:02 +000045 self._scm_wrapper = gclient_scm.CreateSCM
46
47
48class SVNWrapperTestCase(BaseTestCase):
msb@chromium.orge28e4982009-09-25 20:51:45 +000049 class OptionsObject(object):
50 def __init__(self, test_case, verbose=False, revision=None):
51 self.verbose = verbose
52 self.revision = revision
53 self.manually_grab_svn_rev = True
54 self.deps_os = None
55 self.force = False
davemoore@chromium.org8bf27312010-02-19 17:29:44 +000056 self.reset = False
msb@chromium.orge28e4982009-09-25 20:51:45 +000057 self.nohooks = False
58
maruel@chromium.org8ef5f542009-11-12 02:05:02 +000059 def Options(self, *args, **kwargs):
60 return self.OptionsObject(self, *args, **kwargs)
61
msb@chromium.orge28e4982009-09-25 20:51:45 +000062 def setUp(self):
maruel@chromium.org8ef5f542009-11-12 02:05:02 +000063 BaseTestCase.setUp(self)
msb@chromium.orge28e4982009-09-25 20:51:45 +000064 self.root_dir = self.Dir()
65 self.args = self.Args()
66 self.url = self.Url()
67 self.relpath = 'asf'
68
69 def testDir(self):
70 members = [
maruel@chromium.org55e724e2010-03-11 19:36:49 +000071 'FullUrlForRelativeUrl', 'RunCommand',
72 'cleanup', 'diff', 'export', 'pack', 'relpath', 'revert',
tony@chromium.org4b5b1772010-04-08 01:52:56 +000073 'revinfo', 'runhooks', 'scm_name', 'status', 'update',
74 'updatesingle', 'url',
msb@chromium.orge28e4982009-09-25 20:51:45 +000075 ]
76
77 # If you add a member, be sure to add the relevant test!
78 self.compareMembers(self._scm_wrapper(), members)
79
80 def testUnsupportedSCM(self):
81 args = [self.url, self.root_dir, self.relpath]
82 kwargs = {'scm_name' : 'foo'}
83 exception_msg = 'Unsupported scm %(scm_name)s' % kwargs
84 self.assertRaisesError(exception_msg, self._scm_wrapper, *args, **kwargs)
85
msb@chromium.orge6f78352010-01-13 17:05:33 +000086 def testSVNFullUrlForRelativeUrl(self):
87 self.url = 'svn://a/b/c/d'
88
89 self.mox.ReplayAll()
90 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
91 relpath=self.relpath)
92 self.assertEqual(scm.FullUrlForRelativeUrl('/crap'), 'svn://a/b/crap')
93
94 def testGITFullUrlForRelativeUrl(self):
95 self.url = 'git://a/b/c/d'
96
97 self.mox.ReplayAll()
98 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
99 relpath=self.relpath)
100 self.assertEqual(scm.FullUrlForRelativeUrl('/crap'), 'git://a/b/c/crap')
101
msb@chromium.orge28e4982009-09-25 20:51:45 +0000102 def testRunCommandException(self):
103 options = self.Options(verbose=False)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000104 file_path = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git')
105 gclient_scm.os.path.exists(file_path).AndReturn(False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000106
107 self.mox.ReplayAll()
108 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
109 relpath=self.relpath)
110 exception = "Unsupported argument(s): %s" % ','.join(self.args)
111 self.assertRaisesError(exception, scm.RunCommand,
112 'update', options, self.args)
113
114 def testRunCommandUnknown(self):
115 # TODO(maruel): if ever used.
116 pass
117
118 def testRevertMissing(self):
119 options = self.Options(verbose=True)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000120 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
121 gclient_scm.os.path.isdir(base_path).AndReturn(False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000122 # It'll to a checkout instead.
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000123 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git')
124 ).AndReturn(False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000125 print("\n_____ %s is missing, synching instead" % self.relpath)
126 # Checkout.
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000127 gclient_scm.os.path.exists(base_path).AndReturn(False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000128 files_list = self.mox.CreateMockAnything()
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000129 gclient_scm.scm.SVN.RunAndGetFileList(options,
130 ['checkout', self.url, base_path],
131 self.root_dir, files_list)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000132
133 self.mox.ReplayAll()
134 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
135 relpath=self.relpath)
136 scm.revert(options, self.args, files_list)
137
138 def testRevertNone(self):
139 options = self.Options(verbose=True)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000140 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
141 gclient_scm.os.path.isdir(base_path).AndReturn(True)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000142 gclient_scm.scm.SVN.CaptureStatus(base_path).AndReturn([])
143 gclient_scm.scm.SVN.RunAndGetFileList(options,
144 ['update', '--revision', 'BASE'],
145 base_path, mox.IgnoreArg())
msb@chromium.orge28e4982009-09-25 20:51:45 +0000146
147 self.mox.ReplayAll()
148 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
149 relpath=self.relpath)
150 file_list = []
151 scm.revert(options, self.args, file_list)
152
153 def testRevert2Files(self):
154 options = self.Options(verbose=True)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000155 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
156 gclient_scm.os.path.isdir(base_path).AndReturn(True)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000157 items = [
158 ('M ', 'a'),
159 ('A ', 'b'),
160 ]
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000161 file_path1 = gclient_scm.os.path.join(base_path, 'a')
162 file_path2 = gclient_scm.os.path.join(base_path, 'b')
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000163 gclient_scm.scm.SVN.CaptureStatus(base_path).AndReturn(items)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000164 gclient_scm.os.path.exists(file_path1).AndReturn(True)
165 gclient_scm.os.path.isfile(file_path1).AndReturn(True)
166 gclient_scm.os.remove(file_path1)
167 gclient_scm.os.path.exists(file_path2).AndReturn(True)
168 gclient_scm.os.path.isfile(file_path2).AndReturn(True)
169 gclient_scm.os.remove(file_path2)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000170 gclient_scm.scm.SVN.RunAndGetFileList(options,
171 ['update', '--revision', 'BASE'],
172 base_path, mox.IgnoreArg())
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000173 print(gclient_scm.os.path.join(base_path, 'a'))
174 print(gclient_scm.os.path.join(base_path, 'b'))
msb@chromium.orge28e4982009-09-25 20:51:45 +0000175
176 self.mox.ReplayAll()
177 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
178 relpath=self.relpath)
179 file_list = []
180 scm.revert(options, self.args, file_list)
181
182 def testRevertDirectory(self):
183 options = self.Options(verbose=True)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000184 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
185 gclient_scm.os.path.isdir(base_path).AndReturn(True)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000186 items = [
187 ('~ ', 'a'),
188 ]
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000189 gclient_scm.scm.SVN.CaptureStatus(base_path).AndReturn(items)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000190 file_path = gclient_scm.os.path.join(base_path, 'a')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000191 print(file_path)
192 gclient_scm.os.path.exists(file_path).AndReturn(True)
193 gclient_scm.os.path.isfile(file_path).AndReturn(False)
msb@chromium.org32906d12010-01-12 18:22:09 +0000194 gclient_scm.os.path.islink(file_path).AndReturn(False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000195 gclient_scm.os.path.isdir(file_path).AndReturn(True)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000196 gclient_scm.gclient_utils.RemoveDirectory(file_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000197 file_list1 = []
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000198 gclient_scm.scm.SVN.RunAndGetFileList(options,
199 ['update', '--revision', 'BASE'],
200 base_path, mox.IgnoreArg())
msb@chromium.orge28e4982009-09-25 20:51:45 +0000201
202 self.mox.ReplayAll()
203 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
204 relpath=self.relpath)
205 file_list2 = []
206 scm.revert(options, self.args, file_list2)
207
208 def testStatus(self):
209 options = self.Options(verbose=True)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000210 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
211 gclient_scm.os.path.isdir(base_path).AndReturn(True)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000212 gclient_scm.scm.SVN.RunAndGetFileList(options,
213 ['status'] + self.args,
214 base_path, []).AndReturn(None)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000215
216 self.mox.ReplayAll()
217 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
218 relpath=self.relpath)
219 file_list = []
220 self.assertEqual(scm.status(options, self.args, file_list), None)
221
222
223 # TODO(maruel): TEST REVISIONS!!!
224 # TODO(maruel): TEST RELOCATE!!!
225 def testUpdateCheckout(self):
226 options = self.Options(verbose=True)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000227 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
228 file_info = gclient_scm.gclient_utils.PrintableObject()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000229 file_info.root = 'blah'
230 file_info.url = self.url
231 file_info.uuid = 'ABC'
232 file_info.revision = 42
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000233 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git')
234 ).AndReturn(False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000235 # Checkout.
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000236 gclient_scm.os.path.exists(base_path).AndReturn(False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000237 files_list = self.mox.CreateMockAnything()
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000238 gclient_scm.scm.SVN.RunAndGetFileList(options,
239 ['checkout', self.url, base_path],
240 self.root_dir, files_list)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000241 self.mox.ReplayAll()
242 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
243 relpath=self.relpath)
244 scm.update(options, (), files_list)
245
246 def testUpdateUpdate(self):
247 options = self.Options(verbose=True)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000248 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000249 options.force = True
250 options.nohooks = False
251 file_info = {
252 'Repository Root': 'blah',
253 'URL': self.url,
254 'UUID': 'ABC',
255 'Revision': 42,
256 }
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000257 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git')
258 ).AndReturn(False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000259 # Checkout or update.
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000260 gclient_scm.os.path.exists(base_path).AndReturn(True)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000261 gclient_scm.scm.SVN.CaptureInfo(
262 gclient_scm.os.path.join(base_path, "."), '.'
263 ).AndReturn(file_info)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000264 # Cheat a bit here.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000265 gclient_scm.scm.SVN.CaptureInfo(file_info['URL'], '.').AndReturn(file_info)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000266 additional_args = []
267 if options.manually_grab_svn_rev:
268 additional_args = ['--revision', str(file_info['Revision'])]
269 files_list = []
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000270 gclient_scm.scm.SVN.RunAndGetFileList(
271 options,
272 ['update', base_path] + additional_args,
273 self.root_dir, files_list)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000274
275 self.mox.ReplayAll()
276 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
277 relpath=self.relpath)
278 scm.update(options, (), files_list)
279
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000280 def testUpdateSingleCheckout(self):
281 options = self.Options(verbose=True)
282 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
283 file_info = {
284 'URL': self.url,
285 'Revision': 42,
286 }
tony@chromium.org57564662010-04-14 02:35:12 +0000287
288 # Checks to make sure that we support svn co --depth.
289 gclient_scm.scm.SVN.current_version = None
290 gclient_scm.scm.SVN.Capture(['--version']
291 ).AndReturn('svn, version 1.5.1 (r32289)')
292 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.svn')
293 ).AndReturn(False)
294 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, 'DEPS')
295 ).AndReturn(False)
296
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000297 # When checking out a single file, we issue an svn checkout and svn update.
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000298 files_list = self.mox.CreateMockAnything()
299 gclient_scm.scm.SVN.Run(
300 ['checkout', '--depth', 'empty', self.url, base_path], self.root_dir)
301 gclient_scm.scm.SVN.RunAndGetFileList(options, ['update', 'DEPS'],
302 gclient_scm.os.path.join(self.root_dir, self.relpath), files_list)
303
304 # Now we fall back on scm.update().
305 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git')
tony@chromium.org57564662010-04-14 02:35:12 +0000306 ).AndReturn(False)
307 gclient_scm.os.path.exists(base_path).AndReturn(True)
308 gclient_scm.scm.SVN.CaptureInfo(
309 gclient_scm.os.path.join(base_path, "."), '.'
310 ).AndReturn(file_info)
311 gclient_scm.scm.SVN.CaptureInfo(file_info['URL'], '.').AndReturn(file_info)
312 print("\n_____ %s at 42" % self.relpath)
313
314 self.mox.ReplayAll()
315 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
316 relpath=self.relpath)
317 scm.updatesingle(options, ['DEPS'], files_list)
318
319 def testUpdateSingleCheckoutSVN14(self):
320 options = self.Options(verbose=True)
321 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
322 file_info = {
323 'URL': self.url,
324 'Revision': 42,
325 }
326
327 # Checks to make sure that we support svn co --depth.
328 gclient_scm.scm.SVN.current_version = None
329 gclient_scm.scm.SVN.Capture(['--version']
330 ).AndReturn('svn, version 1.4.4 (r25188)')
331 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path)
332 ).AndReturn(True)
333
334 # When checking out a single file with svn 1.4, we use svn export
335 files_list = self.mox.CreateMockAnything()
336 gclient_scm.scm.SVN.Run(
337 ['export', gclient_scm.os.path.join(self.url, 'DEPS'),
338 gclient_scm.os.path.join(base_path, 'DEPS')], self.root_dir)
339
340 self.mox.ReplayAll()
341 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
342 relpath=self.relpath)
343 scm.updatesingle(options, ['DEPS'], files_list)
344
345 def testUpdateSingleCheckoutSVNUpgrade(self):
346 options = self.Options(verbose=True)
347 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
348 file_info = {
349 'URL': self.url,
350 'Revision': 42,
351 }
352
353 # Checks to make sure that we support svn co --depth.
354 gclient_scm.scm.SVN.current_version = None
355 gclient_scm.scm.SVN.Capture(['--version']
356 ).AndReturn('svn, version 1.5.1 (r32289)')
357 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.svn')
358 ).AndReturn(False)
359 # If DEPS already exists, assume we're upgrading from svn1.4, so delete
360 # the old DEPS file.
361 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, 'DEPS')
362 ).AndReturn(True)
363 gclient_scm.os.remove(gclient_scm.os.path.join(base_path, 'DEPS'))
364
365 # When checking out a single file, we issue an svn checkout and svn update.
366 files_list = self.mox.CreateMockAnything()
367 gclient_scm.scm.SVN.Run(
368 ['checkout', '--depth', 'empty', self.url, base_path], self.root_dir)
369 gclient_scm.scm.SVN.RunAndGetFileList(options, ['update', 'DEPS'],
370 gclient_scm.os.path.join(self.root_dir, self.relpath), files_list)
371
372 # Now we fall back on scm.update().
373 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git')
374 ).AndReturn(False)
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000375 gclient_scm.os.path.exists(base_path).AndReturn(True)
376 gclient_scm.scm.SVN.CaptureInfo(
377 gclient_scm.os.path.join(base_path, "."), '.'
378 ).AndReturn(file_info)
379 gclient_scm.scm.SVN.CaptureInfo(file_info['URL'], '.').AndReturn(file_info)
380 print("\n_____ %s at 42" % self.relpath)
381
382 self.mox.ReplayAll()
383 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
384 relpath=self.relpath)
385 scm.updatesingle(options, ['DEPS'], files_list)
386
387 def testUpdateSingleUpdate(self):
388 options = self.Options(verbose=True)
389 base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
390 file_info = {
391 'URL': self.url,
392 'Revision': 42,
393 }
tony@chromium.org57564662010-04-14 02:35:12 +0000394 # Checks to make sure that we support svn co --depth.
395 gclient_scm.scm.SVN.current_version = None
396 gclient_scm.scm.SVN.Capture(['--version']
397 ).AndReturn('svn, version 1.5.1 (r32289)')
398 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.svn')
399 ).AndReturn(True)
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000400
401 # Now we fall back on scm.update().
402 files_list = self.mox.CreateMockAnything()
403 gclient_scm.os.path.exists(gclient_scm.os.path.join(base_path, '.git')
404 ).AndReturn(False)
405 gclient_scm.os.path.exists(base_path).AndReturn(True)
406 gclient_scm.scm.SVN.CaptureInfo(
407 gclient_scm.os.path.join(base_path, "."), '.'
408 ).AndReturn(file_info)
409 gclient_scm.scm.SVN.CaptureInfo(file_info['URL'], '.').AndReturn(file_info)
410 print("\n_____ %s at 42" % self.relpath)
411
412 self.mox.ReplayAll()
413 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
414 relpath=self.relpath)
415 scm.updatesingle(options, ['DEPS'], files_list)
416
msb@chromium.orge28e4982009-09-25 20:51:45 +0000417 def testUpdateGit(self):
418 options = self.Options(verbose=True)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000419 file_path = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git')
420 gclient_scm.os.path.exists(file_path).AndReturn(True)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000421 print("________ found .git directory; skipping %s" % self.relpath)
422
423 self.mox.ReplayAll()
424 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
425 relpath=self.relpath)
426 file_list = []
427 scm.update(options, self.args, file_list)
428
msb@chromium.orge28e4982009-09-25 20:51:45 +0000429
msb@chromium.org5bde4852009-12-14 16:47:12 +0000430class GitWrapperTestCase(BaseTestCase):
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000431 """This class doesn't use pymox."""
msb@chromium.orge28e4982009-09-25 20:51:45 +0000432 class OptionsObject(object):
433 def __init__(self, test_case, verbose=False, revision=None):
434 self.verbose = verbose
435 self.revision = revision
436 self.manually_grab_svn_rev = True
437 self.deps_os = None
438 self.force = False
davemoore@chromium.org8bf27312010-02-19 17:29:44 +0000439 self.reset = False
msb@chromium.orge28e4982009-09-25 20:51:45 +0000440 self.nohooks = False
441
442 sample_git_import = """blob
443mark :1
444data 6
445Hello
446
447blob
448mark :2
449data 4
450Bye
451
452reset refs/heads/master
453commit refs/heads/master
454mark :3
455author Bob <bob@example.com> 1253744361 -0700
456committer Bob <bob@example.com> 1253744361 -0700
457data 8
458A and B
459M 100644 :1 a
460M 100644 :2 b
461
462blob
463mark :4
464data 10
465Hello
466You
467
468blob
469mark :5
470data 8
471Bye
472You
473
474commit refs/heads/origin
475mark :6
476author Alice <alice@example.com> 1253744424 -0700
477committer Alice <alice@example.com> 1253744424 -0700
478data 13
479Personalized
480from :3
481M 100644 :4 a
482M 100644 :5 b
483
484reset refs/heads/master
485from :3
486"""
msb@chromium.orge28e4982009-09-25 20:51:45 +0000487 def Options(self, *args, **kwargs):
488 return self.OptionsObject(self, *args, **kwargs)
489
490 def CreateGitRepo(self, git_import, path):
maruel@chromium.orgd5800f12009-11-12 20:03:43 +0000491 """Do it for real."""
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000492 try:
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000493 Popen(['git', 'init'], stdout=PIPE, stderr=STDOUT,
494 cwd=path).communicate()
495 except OSError:
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000496 # git is not available, skip this test.
497 return False
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000498 Popen(['git', 'fast-import'], stdin=PIPE, stdout=PIPE, stderr=STDOUT,
499 cwd=path).communicate(input=git_import)
500 Popen(['git', 'checkout'], stdout=PIPE, stderr=STDOUT,
501 cwd=path).communicate()
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000502 Popen(['git', 'remote', 'add', '-f', 'origin', '.'], stdout=PIPE,
503 stderr=STDOUT, cwd=path).communicate()
504 Popen(['git', 'checkout', '-b', 'new', 'origin/master'], stdout=PIPE,
505 stderr=STDOUT, cwd=path).communicate()
506 Popen(['git', 'push', 'origin', 'origin/origin:origin/master'], stdout=PIPE,
507 stderr=STDOUT, cwd=path).communicate()
508 Popen(['git', 'config', '--unset', 'remote.origin.fetch'], stdout=PIPE,
509 stderr=STDOUT, cwd=path).communicate()
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000510 return True
msb@chromium.orge28e4982009-09-25 20:51:45 +0000511
msb@chromium.orge28e4982009-09-25 20:51:45 +0000512 def setUp(self):
msb@chromium.orge28e4982009-09-25 20:51:45 +0000513 self.args = self.Args()
514 self.url = 'git://foo'
515 self.root_dir = tempfile.mkdtemp()
516 self.relpath = '.'
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000517 self.base_path = gclient_scm.os.path.join(self.root_dir, self.relpath)
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000518 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000519 SuperMoxBaseTestBase.setUp(self)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000520
521 def tearDown(self):
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000522 SuperMoxBaseTestBase.tearDown(self)
maruel@chromium.orgba551772010-02-03 18:21:42 +0000523 rmtree(self.root_dir)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000524
525 def testDir(self):
526 members = [
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000527 'FullUrlForRelativeUrl', 'RunCommand',
msb@chromium.orge6f78352010-01-13 17:05:33 +0000528 'cleanup', 'diff', 'export', 'pack', 'relpath', 'revert',
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000529 'revinfo', 'runhooks', 'scm_name', 'status', 'update', 'url',
msb@chromium.orge28e4982009-09-25 20:51:45 +0000530 ]
531
532 # If you add a member, be sure to add the relevant test!
533 self.compareMembers(gclient_scm.CreateSCM(url=self.url), members)
534
535 def testRevertMissing(self):
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000536 if not self.enabled:
537 return
msb@chromium.orge28e4982009-09-25 20:51:45 +0000538 options = self.Options()
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000539 file_path = gclient_scm.os.path.join(self.base_path, 'a')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000540 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
541 relpath=self.relpath)
542 file_list = []
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000543 scm.update(options, None, file_list)
544 gclient_scm.os.remove(file_path)
545 file_list = []
msb@chromium.orge28e4982009-09-25 20:51:45 +0000546 scm.revert(options, self.args, file_list)
547 self.assertEquals(file_list, [file_path])
548 file_list = []
549 scm.diff(options, self.args, file_list)
550 self.assertEquals(file_list, [])
551
552 def testRevertNone(self):
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000553 if not self.enabled:
554 return
msb@chromium.orge28e4982009-09-25 20:51:45 +0000555 options = self.Options()
556 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
557 relpath=self.relpath)
558 file_list = []
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000559 scm.update(options, None, file_list)
560 file_list = []
msb@chromium.orge28e4982009-09-25 20:51:45 +0000561 scm.revert(options, self.args, file_list)
562 self.assertEquals(file_list, [])
msb@chromium.org0f282062009-11-06 20:14:02 +0000563 self.assertEquals(scm.revinfo(options, self.args, None),
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000564 'a7142dc9f0009350b96a11f372b6ea658592aa95')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000565
566
567 def testRevertModified(self):
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000568 if not self.enabled:
569 return
msb@chromium.orge28e4982009-09-25 20:51:45 +0000570 options = self.Options()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000571 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
572 relpath=self.relpath)
573 file_list = []
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000574 scm.update(options, None, file_list)
575 file_path = gclient_scm.os.path.join(self.base_path, 'a')
576 open(file_path, 'a').writelines('touched\n')
577 file_list = []
msb@chromium.orge28e4982009-09-25 20:51:45 +0000578 scm.revert(options, self.args, file_list)
579 self.assertEquals(file_list, [file_path])
580 file_list = []
581 scm.diff(options, self.args, file_list)
582 self.assertEquals(file_list, [])
msb@chromium.org0f282062009-11-06 20:14:02 +0000583 self.assertEquals(scm.revinfo(options, self.args, None),
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000584 'a7142dc9f0009350b96a11f372b6ea658592aa95')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000585
586 def testRevertNew(self):
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000587 if not self.enabled:
588 return
msb@chromium.orge28e4982009-09-25 20:51:45 +0000589 options = self.Options()
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000590 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
591 relpath=self.relpath)
592 file_list = []
593 scm.update(options, None, file_list)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000594 file_path = gclient_scm.os.path.join(self.base_path, 'c')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000595 f = open(file_path, 'w')
596 f.writelines('new\n')
597 f.close()
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000598 Popen(['git', 'add', 'c'], stdout=PIPE,
599 stderr=STDOUT, cwd=self.base_path).communicate()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000600 file_list = []
601 scm.revert(options, self.args, file_list)
602 self.assertEquals(file_list, [file_path])
603 file_list = []
604 scm.diff(options, self.args, file_list)
605 self.assertEquals(file_list, [])
msb@chromium.org0f282062009-11-06 20:14:02 +0000606 self.assertEquals(scm.revinfo(options, self.args, None),
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000607 'a7142dc9f0009350b96a11f372b6ea658592aa95')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000608
609 def testStatusNew(self):
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000610 if not self.enabled:
611 return
msb@chromium.orge28e4982009-09-25 20:51:45 +0000612 options = self.Options()
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000613 file_path = gclient_scm.os.path.join(self.base_path, 'a')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000614 open(file_path, 'a').writelines('touched\n')
615 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
616 relpath=self.relpath)
617 file_list = []
618 scm.status(options, self.args, file_list)
619 self.assertEquals(file_list, [file_path])
620
621 def testStatus2New(self):
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000622 if not self.enabled:
623 return
msb@chromium.orge28e4982009-09-25 20:51:45 +0000624 options = self.Options()
625 expected_file_list = []
626 for f in ['a', 'b']:
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000627 file_path = gclient_scm.os.path.join(self.base_path, f)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000628 open(file_path, 'a').writelines('touched\n')
629 expected_file_list.extend([file_path])
630 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
631 relpath=self.relpath)
632 file_list = []
633 scm.status(options, self.args, file_list)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000634 expected_file_list = [gclient_scm.os.path.join(self.base_path, x)
635 for x in ['a', 'b']]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000636 self.assertEquals(sorted(file_list), expected_file_list)
637
638 def testUpdateCheckout(self):
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000639 if not self.enabled:
640 return
msb@chromium.orge28e4982009-09-25 20:51:45 +0000641 options = self.Options(verbose=True)
642 root_dir = tempfile.mkdtemp()
643 relpath = 'foo'
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000644 base_path = gclient_scm.os.path.join(root_dir, relpath)
645 url = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000646 try:
647 scm = gclient_scm.CreateSCM(url=url, root_dir=root_dir,
648 relpath=relpath)
649 file_list = []
650 scm.update(options, (), file_list)
651 self.assertEquals(len(file_list), 2)
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000652 self.assert_(gclient_scm.os.path.isfile(
653 gclient_scm.os.path.join(base_path, 'a')))
msb@chromium.org0f282062009-11-06 20:14:02 +0000654 self.assertEquals(scm.revinfo(options, (), None),
msb@chromium.orge28e4982009-09-25 20:51:45 +0000655 '069c602044c5388d2d15c3f875b057c852003458')
656 finally:
maruel@chromium.orgba551772010-02-03 18:21:42 +0000657 rmtree(root_dir)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000658
659 def testUpdateUpdate(self):
maruel@chromium.orgea6c2c52009-10-09 20:38:14 +0000660 if not self.enabled:
661 return
msb@chromium.orge28e4982009-09-25 20:51:45 +0000662 options = self.Options()
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000663 expected_file_list = [gclient_scm.os.path.join(self.base_path, x)
664 for x in ['a', 'b']]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000665 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
666 relpath=self.relpath)
667 file_list = []
668 scm.update(options, (), file_list)
msb@chromium.org0f282062009-11-06 20:14:02 +0000669 self.assertEquals(file_list, expected_file_list)
670 self.assertEquals(scm.revinfo(options, (), None),
msb@chromium.orge28e4982009-09-25 20:51:45 +0000671 'a7142dc9f0009350b96a11f372b6ea658592aa95')
672
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000673 def testUpdateUnstagedConflict(self):
674 if not self.enabled:
675 return
676 options = self.Options()
677 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
678 relpath=self.relpath)
679 file_path = gclient_scm.os.path.join(self.base_path, 'b')
680 f = open(file_path, 'w').writelines('conflict\n')
681 exception = (
682 "error: Your local changes to 'b' would be overwritten by merge. "
683 "Aborting.\n"
684 "Please, commit your changes or stash them before you can merge.\n")
685 self.assertRaisesError(exception, scm.update, options, (), [])
686
msb@chromium.org5bde4852009-12-14 16:47:12 +0000687 def testUpdateConflict(self):
688 if not self.enabled:
689 return
690 options = self.Options()
691 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
692 relpath=self.relpath)
693 file_path = gclient_scm.os.path.join(self.base_path, 'b')
694 f = open(file_path, 'w').writelines('conflict\n')
695 scm._Run(['commit', '-am', 'test'])
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000696 self.mox.StubOutWithMock(__builtin__, 'raw_input')
697 __builtin__.raw_input.__call__(mox.StrContains('Cannot fast-forward merge, '
698 'attempt to rebase? (y)es / '
699 '(q)uit / (s)kip : ')
700 ).AndReturn('y')
701 self.mox.ReplayAll()
msb@chromium.org5bde4852009-12-14 16:47:12 +0000702 exception = \
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000703 'Conflict while rebasing this branch.\n' \
msb@chromium.org5bde4852009-12-14 16:47:12 +0000704 'Fix the conflict and run gclient again.\n' \
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000705 "See 'man git-rebase' for details.\n"
msb@chromium.org5bde4852009-12-14 16:47:12 +0000706 self.assertRaisesError(exception, scm.update, options, (), [])
707 exception = \
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000708 '\n____ . at refs/heads/master\n' \
msb@chromium.org5bde4852009-12-14 16:47:12 +0000709 '\tAlready in a conflict, i.e. (no branch).\n' \
710 '\tFix the conflict and run gclient again.\n' \
711 '\tOr to abort run:\n\t\tgit-rebase --abort\n' \
712 '\tSee man git-rebase for details.\n'
713 self.assertRaisesError(exception, scm.update, options, (), [])
714
msb@chromium.orge4af1ab2010-01-13 21:26:09 +0000715 def testUpdateNotGit(self):
716 if not self.enabled:
717 return
718 options = self.Options()
719 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
720 relpath=self.relpath)
721 git_path = gclient_scm.os.path.join(self.base_path, '.git')
maruel@chromium.orgba551772010-02-03 18:21:42 +0000722 rename(git_path, git_path + 'foo')
msb@chromium.orge4af1ab2010-01-13 21:26:09 +0000723 exception = \
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000724 '\n____ . at refs/heads/master\n' \
msb@chromium.orge4af1ab2010-01-13 21:26:09 +0000725 '\tPath is not a git repo. No .git dir.\n' \
726 '\tTo resolve:\n' \
727 '\t\trm -rf .\n' \
728 '\tAnd run gclient sync again\n'
729 self.assertRaisesError(exception, scm.update, options, (), [])
730
msb@chromium.org0f282062009-11-06 20:14:02 +0000731 def testRevinfo(self):
732 if not self.enabled:
733 return
734 options = self.Options()
735 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
736 relpath=self.relpath)
737 rev_info = scm.revinfo(options, (), None)
738 self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458')
739
msb@chromium.orge28e4982009-09-25 20:51:45 +0000740
msb@chromium.orge28e4982009-09-25 20:51:45 +0000741if __name__ == '__main__':
maruel@chromium.org8ef5f542009-11-12 02:05:02 +0000742 import unittest
msb@chromium.orge28e4982009-09-25 20:51:45 +0000743 unittest.main()
744
745# vim: ts=2:sw=2:tw=80:et: