gclient_scm_test.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. #!/usr/bin/env python
  2. # Copyright (c) 2011 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Unit tests for gclient_scm.py."""
  6. # pylint: disable=E1101,E1103,W0403
  7. # Import before super_mox to keep valid references.
  8. from os import rename
  9. from shutil import rmtree
  10. from subprocess import Popen, PIPE, STDOUT
  11. import tempfile
  12. import unittest
  13. import __builtin__
  14. # Fixes include path.
  15. from super_mox import mox, StdoutCheck, TestCaseUtils, SuperMoxTestBase
  16. import logging
  17. import sys
  18. import gclient_scm
  19. # Shortcut since this function is used often
  20. join = gclient_scm.os.path.join
  21. class GCBaseTestCase(object):
  22. def assertRaisesError(self, msg, fn, *args, **kwargs):
  23. """Like unittest's assertRaises() but checks for Gclient.Error."""
  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)
  30. class BaseTestCase(GCBaseTestCase, SuperMoxTestBase):
  31. def setUp(self):
  32. SuperMoxTestBase.setUp(self)
  33. self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'CheckCall')
  34. self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'CheckCallAndFilter')
  35. self.mox.StubOutWithMock(gclient_scm.gclient_utils,
  36. 'CheckCallAndFilterAndHeader')
  37. self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead')
  38. self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite')
  39. self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'Popen')
  40. self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'RemoveDirectory')
  41. self._CaptureSVNInfo = gclient_scm.scm.SVN.CaptureInfo
  42. self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'Capture')
  43. self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureInfo')
  44. self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureStatus')
  45. self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'RunAndGetFileList')
  46. self._scm_wrapper = gclient_scm.CreateSCM
  47. gclient_scm.scm.SVN.current_version = None
  48. # Absolute path of the fake checkout directory.
  49. self.base_path = join(self.root_dir, self.relpath)
  50. def tearDown(self):
  51. SuperMoxTestBase.tearDown(self)
  52. class SVNWrapperTestCase(BaseTestCase):
  53. class OptionsObject(object):
  54. def __init__(self, verbose=False, revision=None):
  55. self.verbose = verbose
  56. self.revision = revision
  57. self.manually_grab_svn_rev = True
  58. self.deps_os = None
  59. self.force = False
  60. self.reset = False
  61. self.nohooks = False
  62. def Options(self, *args, **kwargs):
  63. return self.OptionsObject(*args, **kwargs)
  64. def setUp(self):
  65. BaseTestCase.setUp(self)
  66. self.url = self.SvnUrl()
  67. def testDir(self):
  68. members = [
  69. 'FullUrlForRelativeUrl', 'GetRevisionDate', 'RunCommand',
  70. 'cleanup', 'diff', 'pack', 'relpath', 'revert',
  71. 'revinfo', 'runhooks', 'status', 'update',
  72. 'updatesingle', 'url',
  73. ]
  74. # If you add a member, be sure to add the relevant test!
  75. self.compareMembers(self._scm_wrapper('svn://a'), members)
  76. def testUnsupportedSCM(self):
  77. args = ['gopher://foo', self.root_dir, self.relpath]
  78. exception_msg = 'No SCM found for url gopher://foo'
  79. self.assertRaisesError(exception_msg, self._scm_wrapper, *args)
  80. def testSVNFullUrlForRelativeUrl(self):
  81. self.url = 'svn://a/b/c/d'
  82. self.mox.ReplayAll()
  83. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  84. relpath=self.relpath)
  85. self.assertEqual(scm.FullUrlForRelativeUrl('/crap'), 'svn://a/b/crap')
  86. def testGITFullUrlForRelativeUrl(self):
  87. self.url = 'git://a/b/c/d'
  88. self.mox.ReplayAll()
  89. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  90. relpath=self.relpath)
  91. self.assertEqual(scm.FullUrlForRelativeUrl('/crap'), 'git://a/b/c/crap')
  92. def testRunCommandException(self):
  93. options = self.Options(verbose=False)
  94. gclient_scm.os.path.exists(join(self.base_path, '.git')).AndReturn(False)
  95. gclient_scm.os.path.exists(join(self.base_path, '.hg')).AndReturn(False)
  96. self.mox.ReplayAll()
  97. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  98. relpath=self.relpath)
  99. exception = "Unsupported argument(s): %s" % ','.join(self.args)
  100. self.assertRaisesError(exception, scm.RunCommand,
  101. 'update', options, self.args)
  102. def testRunCommandUnknown(self):
  103. # TODO(maruel): if ever used.
  104. pass
  105. def testRevertMissing(self):
  106. options = self.Options(verbose=True)
  107. gclient_scm.os.path.isdir(self.base_path).AndReturn(False)
  108. gclient_scm.scm.SVN.Capture(['--version']
  109. ).AndReturn('svn, version 1.5.1 (r32289)')
  110. # It'll to a checkout instead.
  111. gclient_scm.os.path.exists(join(self.base_path, '.git')).AndReturn(False)
  112. gclient_scm.os.path.exists(join(self.base_path, '.hg')).AndReturn(False)
  113. # Checkout.
  114. gclient_scm.os.path.exists(self.base_path).AndReturn(False)
  115. files_list = self.mox.CreateMockAnything()
  116. gclient_scm.scm.SVN.RunAndGetFileList(
  117. options.verbose,
  118. ['checkout', self.url, self.base_path, '--force', '--ignore-externals'],
  119. cwd=self.root_dir,
  120. file_list=files_list)
  121. self.mox.ReplayAll()
  122. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  123. relpath=self.relpath)
  124. scm.revert(options, self.args, files_list)
  125. self.checkstdout(
  126. ('\n_____ %s is missing, synching instead\n' % self.relpath))
  127. def testRevertNone(self):
  128. options = self.Options(verbose=True)
  129. gclient_scm.os.path.isdir(self.base_path).AndReturn(True)
  130. gclient_scm.scm.SVN.CaptureStatus(self.base_path).AndReturn([])
  131. gclient_scm.scm.SVN.RunAndGetFileList(
  132. options.verbose,
  133. ['update', '--revision', 'BASE', '--ignore-externals'],
  134. cwd=self.base_path,
  135. file_list=mox.IgnoreArg())
  136. self.mox.ReplayAll()
  137. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  138. relpath=self.relpath)
  139. file_list = []
  140. scm.revert(options, self.args, file_list)
  141. def testRevertDirectory(self):
  142. options = self.Options(verbose=True)
  143. gclient_scm.os.path.isdir(self.base_path).AndReturn(True)
  144. items = [
  145. ('~ ', 'a'),
  146. ]
  147. gclient_scm.scm.SVN.CaptureStatus(self.base_path).AndReturn(items)
  148. file_path = join(self.base_path, 'a')
  149. gclient_scm.os.path.exists(file_path).AndReturn(True)
  150. gclient_scm.os.path.isfile(file_path).AndReturn(False)
  151. gclient_scm.os.path.islink(file_path).AndReturn(False)
  152. gclient_scm.os.path.isdir(file_path).AndReturn(True)
  153. gclient_scm.gclient_utils.RemoveDirectory(file_path)
  154. gclient_scm.scm.SVN.RunAndGetFileList(
  155. options.verbose,
  156. ['update', '--revision', 'BASE', '--ignore-externals'],
  157. cwd=self.base_path,
  158. file_list=mox.IgnoreArg())
  159. self.mox.ReplayAll()
  160. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  161. relpath=self.relpath)
  162. file_list2 = []
  163. scm.revert(options, self.args, file_list2)
  164. self.checkstdout(('%s\n' % file_path))
  165. def testStatus(self):
  166. options = self.Options(verbose=True)
  167. gclient_scm.os.path.isdir(self.base_path).AndReturn(True)
  168. gclient_scm.scm.SVN.RunAndGetFileList(
  169. options.verbose,
  170. ['status'] + self.args + ['--ignore-externals'],
  171. cwd=self.base_path,
  172. file_list=[]).AndReturn(None)
  173. self.mox.ReplayAll()
  174. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  175. relpath=self.relpath)
  176. file_list = []
  177. self.assertEqual(scm.status(options, self.args, file_list), None)
  178. # TODO(maruel): TEST REVISIONS!!!
  179. # TODO(maruel): TEST RELOCATE!!!
  180. def testUpdateCheckout(self):
  181. options = self.Options(verbose=True)
  182. file_info = gclient_scm.gclient_utils.PrintableObject()
  183. file_info.root = 'blah'
  184. file_info.url = self.url
  185. file_info.uuid = 'ABC'
  186. file_info.revision = 42
  187. gclient_scm.os.path.exists(join(self.base_path, '.git')).AndReturn(False)
  188. gclient_scm.os.path.exists(join(self.base_path, '.hg')).AndReturn(False)
  189. # Checkout.
  190. gclient_scm.os.path.exists(self.base_path).AndReturn(False)
  191. files_list = self.mox.CreateMockAnything()
  192. gclient_scm.scm.SVN.Capture(['--version']
  193. ).AndReturn('svn, version 1.5.1 (r32289)')
  194. gclient_scm.scm.SVN.RunAndGetFileList(
  195. options.verbose,
  196. ['checkout', self.url, self.base_path, '--force', '--ignore-externals'],
  197. cwd=self.root_dir,
  198. file_list=files_list)
  199. self.mox.ReplayAll()
  200. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  201. relpath=self.relpath)
  202. scm.update(options, (), files_list)
  203. def testUpdateUpdate(self):
  204. options = self.Options(verbose=True)
  205. options.force = True
  206. options.nohooks = False
  207. file_info = {
  208. 'Repository Root': 'blah',
  209. 'URL': self.url,
  210. 'UUID': 'ABC',
  211. 'Revision': 42,
  212. }
  213. gclient_scm.os.path.exists(join(self.base_path, '.git')).AndReturn(False)
  214. gclient_scm.os.path.exists(join(self.base_path, '.hg')).AndReturn(False)
  215. # Verify no locked files.
  216. dotted_path = join(self.base_path, '.')
  217. gclient_scm.scm.SVN.CaptureStatus(dotted_path).AndReturn([])
  218. # Checkout or update.
  219. gclient_scm.os.path.exists(self.base_path).AndReturn(True)
  220. gclient_scm.scm.SVN.CaptureInfo(dotted_path).AndReturn(file_info)
  221. # Cheat a bit here.
  222. gclient_scm.scm.SVN.CaptureInfo(file_info['URL']).AndReturn(file_info)
  223. additional_args = []
  224. if options.manually_grab_svn_rev:
  225. additional_args = ['--revision', str(file_info['Revision'])]
  226. gclient_scm.scm.SVN.Capture(['--version']
  227. ).AndReturn('svn, version 1.5.1 (r32289)')
  228. additional_args.extend(['--force', '--ignore-externals'])
  229. files_list = []
  230. gclient_scm.scm.SVN.RunAndGetFileList(
  231. options.verbose,
  232. ['update', self.base_path] + additional_args,
  233. cwd=self.root_dir, file_list=files_list)
  234. self.mox.ReplayAll()
  235. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  236. relpath=self.relpath)
  237. scm.update(options, (), files_list)
  238. def testUpdateSingleCheckout(self):
  239. options = self.Options(verbose=True)
  240. file_info = {
  241. 'URL': self.url,
  242. 'Revision': 42,
  243. }
  244. # Checks to make sure that we support svn co --depth.
  245. gclient_scm.scm.SVN.current_version = None
  246. gclient_scm.scm.SVN.Capture(['--version']
  247. ).AndReturn('svn, version 1.5.1 (r32289)')
  248. gclient_scm.os.path.exists(join(self.base_path, '.svn')).AndReturn(False)
  249. gclient_scm.os.path.exists(join(self.base_path, 'DEPS')).AndReturn(False)
  250. # Verify no locked files.
  251. dotted_path = join(self.base_path, '.')
  252. gclient_scm.scm.SVN.CaptureStatus(dotted_path).AndReturn([])
  253. # When checking out a single file, we issue an svn checkout and svn update.
  254. files_list = self.mox.CreateMockAnything()
  255. gclient_scm.gclient_utils.CheckCallAndFilterAndHeader(
  256. ['svn', 'checkout', '--depth', 'empty', self.url, self.base_path],
  257. always=True,
  258. cwd=self.root_dir)
  259. gclient_scm.scm.SVN.RunAndGetFileList(
  260. options.verbose,
  261. ['update', 'DEPS', '--ignore-externals'],
  262. cwd=self.base_path,
  263. file_list=files_list)
  264. # Now we fall back on scm.update().
  265. gclient_scm.os.path.exists(join(self.base_path, '.git')).AndReturn(False)
  266. gclient_scm.os.path.exists(join(self.base_path, '.hg')).AndReturn(False)
  267. gclient_scm.os.path.exists(self.base_path).AndReturn(True)
  268. gclient_scm.scm.SVN.CaptureInfo(dotted_path).AndReturn(file_info)
  269. gclient_scm.scm.SVN.CaptureInfo(file_info['URL']).AndReturn(file_info)
  270. self.mox.ReplayAll()
  271. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  272. relpath=self.relpath)
  273. scm.updatesingle(options, ['DEPS'], files_list)
  274. self.checkstdout('\n_____ %s at 42\n' % self.relpath)
  275. def testUpdateSingleCheckoutSVN14(self):
  276. options = self.Options(verbose=True)
  277. # Checks to make sure that we support svn co --depth.
  278. gclient_scm.scm.SVN.current_version = None
  279. gclient_scm.scm.SVN.Capture(['--version']
  280. ).AndReturn('svn, version 1.4.4 (r25188)')
  281. gclient_scm.os.path.exists(self.base_path).AndReturn(True)
  282. # When checking out a single file with svn 1.4, we use svn export
  283. files_list = self.mox.CreateMockAnything()
  284. gclient_scm.gclient_utils.CheckCallAndFilterAndHeader(
  285. ['svn', 'export', join(self.url, 'DEPS'), join(self.base_path, 'DEPS')],
  286. always=True, cwd=self.root_dir)
  287. self.mox.ReplayAll()
  288. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  289. relpath=self.relpath)
  290. scm.updatesingle(options, ['DEPS'], files_list)
  291. def testUpdateSingleCheckoutSVNUpgrade(self):
  292. options = self.Options(verbose=True)
  293. file_info = {
  294. 'URL': self.url,
  295. 'Revision': 42,
  296. }
  297. # Checks to make sure that we support svn co --depth.
  298. gclient_scm.scm.SVN.current_version = None
  299. gclient_scm.scm.SVN.Capture(['--version']
  300. ).AndReturn('svn, version 1.5.1 (r32289)')
  301. gclient_scm.os.path.exists(join(self.base_path, '.svn')).AndReturn(False)
  302. # If DEPS already exists, assume we're upgrading from svn1.4, so delete
  303. # the old DEPS file.
  304. gclient_scm.os.path.exists(join(self.base_path, 'DEPS')).AndReturn(True)
  305. gclient_scm.os.remove(join(self.base_path, 'DEPS'))
  306. # Verify no locked files.
  307. gclient_scm.scm.SVN.CaptureStatus(join(self.base_path, '.')).AndReturn([])
  308. # When checking out a single file, we issue an svn checkout and svn update.
  309. files_list = self.mox.CreateMockAnything()
  310. gclient_scm.gclient_utils.CheckCallAndFilterAndHeader(
  311. ['svn', 'checkout', '--depth', 'empty', self.url, self.base_path],
  312. always=True,
  313. cwd=self.root_dir)
  314. gclient_scm.scm.SVN.RunAndGetFileList(
  315. options.verbose,
  316. ['update', 'DEPS', '--ignore-externals'],
  317. cwd=self.base_path,
  318. file_list=files_list)
  319. # Now we fall back on scm.update().
  320. gclient_scm.os.path.exists(join(self.base_path, '.git')).AndReturn(False)
  321. gclient_scm.os.path.exists(join(self.base_path, '.hg')).AndReturn(False)
  322. gclient_scm.os.path.exists(self.base_path).AndReturn(True)
  323. gclient_scm.scm.SVN.CaptureInfo(
  324. join(self.base_path, ".")).AndReturn(file_info)
  325. gclient_scm.scm.SVN.CaptureInfo(file_info['URL']).AndReturn(file_info)
  326. self.mox.ReplayAll()
  327. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  328. relpath=self.relpath)
  329. scm.updatesingle(options, ['DEPS'], files_list)
  330. self.checkstdout(
  331. ('\n_____ %s at 42\n' % self.relpath))
  332. def testUpdateSingleUpdate(self):
  333. options = self.Options(verbose=True)
  334. file_info = {
  335. 'URL': self.url,
  336. 'Revision': 42,
  337. }
  338. # Checks to make sure that we support svn co --depth.
  339. gclient_scm.scm.SVN.current_version = None
  340. gclient_scm.scm.SVN.Capture(['--version']
  341. ).AndReturn('svn, version 1.5.1 (r32289)')
  342. gclient_scm.os.path.exists(join(self.base_path, '.svn')).AndReturn(True)
  343. # Verify no locked files.
  344. gclient_scm.scm.SVN.CaptureStatus(join(self.base_path, '.')).AndReturn([])
  345. # Now we fall back on scm.update().
  346. files_list = self.mox.CreateMockAnything()
  347. gclient_scm.os.path.exists(join(self.base_path, '.git')).AndReturn(False)
  348. gclient_scm.os.path.exists(join(self.base_path, '.hg')).AndReturn(False)
  349. gclient_scm.os.path.exists(self.base_path).AndReturn(True)
  350. gclient_scm.scm.SVN.CaptureInfo(
  351. join(self.base_path, '.')).AndReturn(file_info)
  352. gclient_scm.scm.SVN.CaptureInfo(file_info['URL']).AndReturn(file_info)
  353. self.mox.ReplayAll()
  354. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  355. relpath=self.relpath)
  356. scm.updatesingle(options, ['DEPS'], files_list)
  357. self.checkstdout('\n_____ %s at 42\n' % self.relpath)
  358. def testUpdateGit(self):
  359. options = self.Options(verbose=True)
  360. file_path = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git')
  361. gclient_scm.os.path.exists(file_path).AndReturn(True)
  362. self.mox.ReplayAll()
  363. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  364. relpath=self.relpath)
  365. file_list = []
  366. scm.update(options, self.args, file_list)
  367. self.checkstdout(
  368. ('________ found .git directory; skipping %s\n' % self.relpath))
  369. def testUpdateHg(self):
  370. options = self.Options(verbose=True)
  371. gclient_scm.os.path.exists(join(self.base_path, '.git')).AndReturn(False)
  372. gclient_scm.os.path.exists(join(self.base_path, '.hg')).AndReturn(True)
  373. self.mox.ReplayAll()
  374. scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir,
  375. relpath=self.relpath)
  376. file_list = []
  377. scm.update(options, self.args, file_list)
  378. self.checkstdout(
  379. ('________ found .hg directory; skipping %s\n' % self.relpath))
  380. class GitWrapperTestCase(GCBaseTestCase, StdoutCheck, TestCaseUtils,
  381. unittest.TestCase):
  382. """This class doesn't use pymox."""
  383. class OptionsObject(object):
  384. def __init__(self, verbose=False, revision=None):
  385. self.verbose = verbose
  386. self.revision = revision
  387. self.manually_grab_svn_rev = True
  388. self.deps_os = None
  389. self.force = False
  390. self.reset = False
  391. self.nohooks = False
  392. sample_git_import = """blob
  393. mark :1
  394. data 6
  395. Hello
  396. blob
  397. mark :2
  398. data 4
  399. Bye
  400. reset refs/heads/master
  401. commit refs/heads/master
  402. mark :3
  403. author Bob <bob@example.com> 1253744361 -0700
  404. committer Bob <bob@example.com> 1253744361 -0700
  405. data 8
  406. A and B
  407. M 100644 :1 a
  408. M 100644 :2 b
  409. blob
  410. mark :4
  411. data 10
  412. Hello
  413. You
  414. blob
  415. mark :5
  416. data 8
  417. Bye
  418. You
  419. commit refs/heads/origin
  420. mark :6
  421. author Alice <alice@example.com> 1253744424 -0700
  422. committer Alice <alice@example.com> 1253744424 -0700
  423. data 13
  424. Personalized
  425. from :3
  426. M 100644 :4 a
  427. M 100644 :5 b
  428. reset refs/heads/master
  429. from :3
  430. """
  431. def Options(self, *args, **kwargs):
  432. return self.OptionsObject(*args, **kwargs)
  433. @staticmethod
  434. def CreateGitRepo(git_import, path):
  435. """Do it for real."""
  436. try:
  437. Popen(['git', 'init', '-q'], stdout=PIPE, stderr=STDOUT,
  438. cwd=path).communicate()
  439. except OSError:
  440. # git is not available, skip this test.
  441. return False
  442. Popen(['git', 'fast-import', '--quiet'], stdin=PIPE, stdout=PIPE,
  443. stderr=STDOUT, cwd=path).communicate(input=git_import)
  444. Popen(['git', 'checkout', '-q'], stdout=PIPE, stderr=STDOUT,
  445. cwd=path).communicate()
  446. Popen(['git', 'remote', 'add', '-f', 'origin', '.'], stdout=PIPE,
  447. stderr=STDOUT, cwd=path).communicate()
  448. Popen(['git', 'checkout', '-b', 'new', 'origin/master', '-q'], stdout=PIPE,
  449. stderr=STDOUT, cwd=path).communicate()
  450. Popen(['git', 'push', 'origin', 'origin/origin:origin/master', '-q'],
  451. stdout=PIPE, stderr=STDOUT, cwd=path).communicate()
  452. Popen(['git', 'config', '--unset', 'remote.origin.fetch'], stdout=PIPE,
  453. stderr=STDOUT, cwd=path).communicate()
  454. return True
  455. def setUp(self):
  456. TestCaseUtils.setUp(self)
  457. unittest.TestCase.setUp(self)
  458. self.url = 'git://foo'
  459. self.root_dir = tempfile.mkdtemp()
  460. self.relpath = '.'
  461. self.base_path = join(self.root_dir, self.relpath)
  462. self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path)
  463. StdoutCheck.setUp(self)
  464. def tearDown(self):
  465. StdoutCheck.tearDown(self)
  466. TestCaseUtils.tearDown(self)
  467. unittest.TestCase.tearDown(self)
  468. rmtree(self.root_dir)
  469. def testDir(self):
  470. members = [
  471. 'FullUrlForRelativeUrl', 'GetRevisionDate', 'RunCommand',
  472. 'cleanup', 'diff', 'pack', 'relpath', 'revert',
  473. 'revinfo', 'runhooks', 'status', 'update', 'url',
  474. ]
  475. # If you add a member, be sure to add the relevant test!
  476. self.compareMembers(gclient_scm.CreateSCM(url=self.url), members)
  477. def testRevertMissing(self):
  478. if not self.enabled:
  479. return
  480. options = self.Options()
  481. file_path = join(self.base_path, 'a')
  482. scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
  483. relpath=self.relpath)
  484. file_list = []
  485. scm.update(options, None, file_list)
  486. gclient_scm.os.remove(file_path)
  487. file_list = []
  488. scm.revert(options, self.args, file_list)
  489. self.assertEquals(file_list, [file_path])
  490. file_list = []
  491. scm.diff(options, self.args, file_list)
  492. self.assertEquals(file_list, [])
  493. self.checkstdout(
  494. ('\n_____ . at refs/heads/master\nUpdating 069c602..a7142dc\n'
  495. 'Fast-forward\n a | 1 +\n b | 1 +\n'
  496. ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n\n'
  497. '________ running \'git reset --hard origin/master\' in \'%s\'\n'
  498. 'HEAD is now at a7142dc Personalized\n') %
  499. join(self.root_dir, '.'))
  500. def testRevertNone(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_list = []
  507. scm.update(options, None, file_list)
  508. file_list = []
  509. scm.revert(options, self.args, file_list)
  510. self.assertEquals(file_list, [])
  511. self.assertEquals(scm.revinfo(options, self.args, None),
  512. 'a7142dc9f0009350b96a11f372b6ea658592aa95')
  513. self.checkstdout(
  514. ('\n_____ . at refs/heads/master\nUpdating 069c602..a7142dc\n'
  515. 'Fast-forward\n a | 1 +\n b | 1 +\n'
  516. ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n\n'
  517. '________ running \'git reset --hard origin/master\' in \'%s\'\n'
  518. 'HEAD is now at a7142dc Personalized\n') %
  519. join(self.root_dir, '.'))
  520. def testRevertModified(self):
  521. if not self.enabled:
  522. return
  523. options = self.Options()
  524. scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
  525. relpath=self.relpath)
  526. file_list = []
  527. scm.update(options, None, file_list)
  528. file_path = join(self.base_path, 'a')
  529. open(file_path, 'a').writelines('touched\n')
  530. file_list = []
  531. scm.revert(options, self.args, file_list)
  532. self.assertEquals(file_list, [file_path])
  533. file_list = []
  534. scm.diff(options, self.args, file_list)
  535. self.assertEquals(file_list, [])
  536. self.assertEquals(scm.revinfo(options, self.args, None),
  537. 'a7142dc9f0009350b96a11f372b6ea658592aa95')
  538. self.checkstdout(
  539. ('\n_____ . at refs/heads/master\nUpdating 069c602..a7142dc\n'
  540. 'Fast-forward\n a | 1 +\n b | 1 +\n'
  541. ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n\n'
  542. '________ running \'git reset --hard origin/master\' in \'%s\'\n'
  543. 'HEAD is now at a7142dc Personalized\n') %
  544. join(self.root_dir, '.'))
  545. def testRevertNew(self):
  546. if not self.enabled:
  547. return
  548. options = self.Options()
  549. scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
  550. relpath=self.relpath)
  551. file_list = []
  552. scm.update(options, None, file_list)
  553. file_path = join(self.base_path, 'c')
  554. f = open(file_path, 'w')
  555. f.writelines('new\n')
  556. f.close()
  557. Popen(['git', 'add', 'c'], stdout=PIPE,
  558. stderr=STDOUT, cwd=self.base_path).communicate()
  559. file_list = []
  560. scm.revert(options, self.args, file_list)
  561. self.assertEquals(file_list, [file_path])
  562. file_list = []
  563. scm.diff(options, self.args, file_list)
  564. self.assertEquals(file_list, [])
  565. self.assertEquals(scm.revinfo(options, self.args, None),
  566. 'a7142dc9f0009350b96a11f372b6ea658592aa95')
  567. self.checkstdout(
  568. ('\n_____ . at refs/heads/master\nUpdating 069c602..a7142dc\n'
  569. 'Fast-forward\n a | 1 +\n b | 1 +\n'
  570. ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n\n'
  571. '________ running \'git reset --hard origin/master\' in \'%s\'\n'
  572. 'HEAD is now at a7142dc Personalized\n') %
  573. join(self.root_dir, '.'))
  574. def testStatusNew(self):
  575. if not self.enabled:
  576. return
  577. options = self.Options()
  578. file_path = join(self.base_path, 'a')
  579. open(file_path, 'a').writelines('touched\n')
  580. scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
  581. relpath=self.relpath)
  582. file_list = []
  583. scm.status(options, self.args, file_list)
  584. self.assertEquals(file_list, [file_path])
  585. self.checkstdout(
  586. ('\n________ running \'git diff --name-status '
  587. '069c602044c5388d2d15c3f875b057c852003458\' in \'%s\'\nM\ta\n') %
  588. join(self.root_dir, '.'))
  589. def testStatus2New(self):
  590. if not self.enabled:
  591. return
  592. options = self.Options()
  593. expected_file_list = []
  594. for f in ['a', 'b']:
  595. file_path = join(self.base_path, f)
  596. open(file_path, 'a').writelines('touched\n')
  597. expected_file_list.extend([file_path])
  598. scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
  599. relpath=self.relpath)
  600. file_list = []
  601. scm.status(options, self.args, file_list)
  602. expected_file_list = [join(self.base_path, x) for x in ['a', 'b']]
  603. self.assertEquals(sorted(file_list), expected_file_list)
  604. self.checkstdout(
  605. ('\n________ running \'git diff --name-status '
  606. '069c602044c5388d2d15c3f875b057c852003458\' in \'%s\'\nM\ta\nM\tb\n') %
  607. join(self.root_dir, '.'))
  608. def testUpdateCheckout(self):
  609. if not self.enabled:
  610. return
  611. options = self.Options(verbose=True)
  612. root_dir = gclient_scm.os.path.realpath(tempfile.mkdtemp())
  613. relpath = 'foo'
  614. base_path = join(root_dir, relpath)
  615. url = join(self.base_path, '.git')
  616. try:
  617. scm = gclient_scm.CreateSCM(url=url, root_dir=root_dir,
  618. relpath=relpath)
  619. file_list = []
  620. scm.update(options, (), file_list)
  621. self.assertEquals(len(file_list), 2)
  622. self.assert_(gclient_scm.os.path.isfile(join(base_path, 'a')))
  623. self.assertEquals(scm.revinfo(options, (), None),
  624. '069c602044c5388d2d15c3f875b057c852003458')
  625. finally:
  626. rmtree(root_dir)
  627. msg1 = (
  628. "\n_____ foo at refs/heads/master\n\n"
  629. "________ running 'git clone -b master --verbose %s %s' in '%s'\n"
  630. "Initialized empty Git repository in %s\n") % (
  631. join(self.root_dir, '.', '.git'),
  632. join(root_dir, 'foo'),
  633. root_dir,
  634. join(gclient_scm.os.path.realpath(root_dir), 'foo', '.git') + '/')
  635. msg2 = (
  636. "\n_____ foo at refs/heads/master\n\n"
  637. "________ running 'git clone -b master --verbose %s %s' in '%s'\n"
  638. "Cloning into %s...\ndone.\n") % (
  639. join(self.root_dir, '.', '.git'),
  640. join(root_dir, 'foo'),
  641. root_dir,
  642. join(gclient_scm.os.path.realpath(root_dir), 'foo'))
  643. out = sys.stdout.getvalue()
  644. sys.stdout.close()
  645. sys.stdout = self._old_stdout
  646. self.assertTrue(out in (msg1, msg2), (out, msg1, msg2))
  647. def testUpdateUpdate(self):
  648. if not self.enabled:
  649. return
  650. options = self.Options()
  651. expected_file_list = [join(self.base_path, x) for x in ['a', 'b']]
  652. scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
  653. relpath=self.relpath)
  654. file_list = []
  655. scm.update(options, (), file_list)
  656. self.assertEquals(file_list, expected_file_list)
  657. self.assertEquals(scm.revinfo(options, (), None),
  658. 'a7142dc9f0009350b96a11f372b6ea658592aa95')
  659. self.checkstdout(
  660. '\n_____ . at refs/heads/master\n'
  661. 'Updating 069c602..a7142dc\nFast-forward\n a | 1 +\n b | 1 +\n'
  662. ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n')
  663. def testUpdateUnstagedConflict(self):
  664. if not self.enabled:
  665. return
  666. options = self.Options()
  667. scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
  668. relpath=self.relpath)
  669. file_path = join(self.base_path, 'b')
  670. open(file_path, 'w').writelines('conflict\n')
  671. try:
  672. scm.update(options, (), [])
  673. self.fail()
  674. except gclient_scm.gclient_utils.CheckCallError:
  675. # The exact exception text varies across git versions so it's not worth
  676. # verifying it. It's fine as long as it throws.
  677. pass
  678. # Manually flush stdout since we can't verify it's content accurately across
  679. # git versions.
  680. sys.stdout.getvalue()
  681. sys.stdout.close()
  682. def testUpdateConflict(self):
  683. if not self.enabled:
  684. return
  685. options = self.Options()
  686. scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
  687. relpath=self.relpath)
  688. file_path = join(self.base_path, 'b')
  689. open(file_path, 'w').writelines('conflict\n')
  690. # pylint: disable=W0212
  691. scm._Run(['commit', '-am', 'test'], options)
  692. __builtin__.raw_input = lambda x: 'y'
  693. exception = ('Conflict while rebasing this branch.\n'
  694. 'Fix the conflict and run gclient again.\n'
  695. 'See \'man git-rebase\' for details.\n')
  696. self.assertRaisesError(exception, scm.update, options, (), [])
  697. exception = ('\n____ . at refs/heads/master\n'
  698. '\tYou have unstaged changes.\n'
  699. '\tPlease commit, stash, or reset.\n')
  700. self.assertRaisesError(exception, scm.update, options, (), [])
  701. # The hash always changes. Use a cheap trick.
  702. start = ('\n________ running \'git commit -am test\' in \'%s\'\n'
  703. '[new ') % join(self.root_dir, '.')
  704. end = ('] test\n 1 files changed, 1 insertions(+), '
  705. '1 deletions(-)\n\n_____ . at refs/heads/master\n'
  706. 'Attempting rebase onto refs/remotes/origin/master...\n')
  707. self.assertTrue(sys.stdout.getvalue().startswith(start))
  708. self.assertTrue(sys.stdout.getvalue().endswith(end))
  709. self.assertEquals(len(sys.stdout.getvalue()),
  710. len(start) + len(end) + 7)
  711. sys.stdout.close()
  712. def testUpdateNotGit(self):
  713. if not self.enabled:
  714. return
  715. options = self.Options()
  716. scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
  717. relpath=self.relpath)
  718. git_path = join(self.base_path, '.git')
  719. rename(git_path, git_path + 'foo')
  720. exception = ('\n____ . at refs/heads/master\n'
  721. '\tPath is not a git repo. No .git dir.\n'
  722. '\tTo resolve:\n'
  723. '\t\trm -rf .\n'
  724. '\tAnd run gclient sync again\n')
  725. self.assertRaisesError(exception, scm.update, options, (), [])
  726. def testRevinfo(self):
  727. if not self.enabled:
  728. return
  729. options = self.Options()
  730. scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir,
  731. relpath=self.relpath)
  732. rev_info = scm.revinfo(options, (), None)
  733. self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458')
  734. if __name__ == '__main__':
  735. if '-v' in sys.argv:
  736. logging.basicConfig(
  737. level=logging.DEBUG,
  738. format='%(asctime).19s %(levelname)s %(filename)s:'
  739. '%(lineno)s %(message)s')
  740. unittest.main()
  741. # vim: ts=2:sw=2:tw=80:et: