scm_unittest.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #!/usr/bin/python
  2. # 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.
  5. """Unit tests for scm.py."""
  6. from shutil import rmtree
  7. import tempfile
  8. # Fixes include path.
  9. from super_mox import mox, SuperMoxBaseTestBase
  10. from gclient_test import BaseTestCase
  11. import scm
  12. class BaseSCMTestCase(BaseTestCase):
  13. def setUp(self):
  14. BaseTestCase.setUp(self)
  15. self.mox.StubOutWithMock(scm.gclient_utils, 'SubprocessCall')
  16. self.mox.StubOutWithMock(scm.gclient_utils, 'SubprocessCallAndFilter')
  17. class RootTestCase(BaseSCMTestCase):
  18. def testMembersChanged(self):
  19. self.mox.ReplayAll()
  20. members = [
  21. 'GetCasedPath', 'GIT', 'SVN', 'ValidateEmail',
  22. 'gclient_utils', 'glob', 'os', 're', 'shutil', 'subprocess', 'sys',
  23. 'tempfile', 'xml',
  24. ]
  25. # If this test fails, you should add the relevant test.
  26. self.compareMembers(scm, members)
  27. class GitWrapperTestCase(SuperMoxBaseTestBase):
  28. sample_git_import = """blob
  29. mark :1
  30. data 6
  31. Hello
  32. blob
  33. mark :2
  34. data 4
  35. Bye
  36. reset refs/heads/master
  37. commit refs/heads/master
  38. mark :3
  39. author Bob <bob@example.com> 1253744361 -0700
  40. committer Bob <bob@example.com> 1253744361 -0700
  41. data 8
  42. A and B
  43. M 100644 :1 a
  44. M 100644 :2 b
  45. blob
  46. mark :4
  47. data 10
  48. Hello
  49. You
  50. blob
  51. mark :5
  52. data 8
  53. Bye
  54. You
  55. commit refs/heads/origin
  56. mark :6
  57. author Alice <alice@example.com> 1253744424 -0700
  58. committer Alice <alice@example.com> 1253744424 -0700
  59. data 13
  60. Personalized
  61. from :3
  62. M 100644 :4 a
  63. M 100644 :5 b
  64. reset refs/heads/master
  65. from :3
  66. """
  67. def CreateGitRepo(self, git_import, path):
  68. try:
  69. scm.subprocess.Popen(['git', 'init'],
  70. stdout=scm.subprocess.PIPE,
  71. stderr=scm.subprocess.STDOUT,
  72. cwd=path).communicate()
  73. except OSError:
  74. # git is not available, skip this test.
  75. return False
  76. scm.subprocess.Popen(['git', 'fast-import'],
  77. stdin=scm.subprocess.PIPE,
  78. stdout=scm.subprocess.PIPE,
  79. stderr=scm.subprocess.STDOUT,
  80. cwd=path).communicate(input=git_import)
  81. scm.subprocess.Popen(['git', 'checkout'],
  82. stdout=scm.subprocess.PIPE,
  83. stderr=scm.subprocess.STDOUT,
  84. cwd=path).communicate()
  85. return True
  86. def setUp(self):
  87. SuperMoxBaseTestBase.setUp(self)
  88. self.args = self.Args()
  89. self.url = 'git://foo'
  90. self.root_dir = tempfile.mkdtemp()
  91. self.relpath = '.'
  92. self.base_path = scm.os.path.join(self.root_dir, self.relpath)
  93. self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path)
  94. self.fake_root = self.Dir()
  95. def tearDown(self):
  96. rmtree(self.root_dir)
  97. SuperMoxBaseTestBase.tearDown(self)
  98. def testMembersChanged(self):
  99. self.mox.ReplayAll()
  100. members = [
  101. 'COMMAND', 'Capture', 'CaptureStatus', 'FetchUpstreamTuple',
  102. 'GenerateDiff', 'GetBranch', 'GetBranchRef', 'GetCheckoutRoot',
  103. 'GetDifferentFiles', 'GetEmail', 'GetPatchName', 'GetSVNBranch',
  104. 'GetUpstream', 'IsGitSvn', 'RunAndFilterOutput', 'ShortBranchName',
  105. ]
  106. # If this test fails, you should add the relevant test.
  107. self.compareMembers(scm.GIT, members)
  108. def testGetEmail(self):
  109. self.mox.StubOutWithMock(scm.GIT, 'Capture')
  110. scm.GIT.Capture(['config', 'user.email'], self.fake_root, error_ok=True
  111. ).AndReturn(['mini@me.com', ''])
  112. self.mox.ReplayAll()
  113. self.assertEqual(scm.GIT.GetEmail(self.fake_root), 'mini@me.com')
  114. class SVNTestCase(BaseSCMTestCase):
  115. def setUp(self):
  116. BaseSCMTestCase.setUp(self)
  117. self.root_dir = self.Dir()
  118. self.args = self.Args()
  119. self.url = self.Url()
  120. self.relpath = 'asf'
  121. def testMembersChanged(self):
  122. self.mox.ReplayAll()
  123. members = [
  124. 'COMMAND', 'Capture', 'CaptureHeadRevision', 'CaptureInfo',
  125. 'CaptureStatus', 'DiffItem', 'GenerateDiff', 'GetCheckoutRoot',
  126. 'GetEmail', 'GetFileProperty', 'IsMoved',
  127. 'ReadSimpleAuth', 'Run', 'RunAndFilterOutput', 'RunAndGetFileList',
  128. ]
  129. # If this test fails, you should add the relevant test.
  130. self.compareMembers(scm.SVN, members)
  131. def testGetCheckoutRoot(self):
  132. self.mox.StubOutWithMock(scm.SVN, 'CaptureInfo')
  133. self.mox.StubOutWithMock(scm, 'GetCasedPath')
  134. scm.os.path.abspath(self.root_dir + 'x').AndReturn(self.root_dir)
  135. scm.GetCasedPath(self.root_dir).AndReturn(self.root_dir)
  136. result1 = { "Repository Root": "Some root" }
  137. scm.SVN.CaptureInfo(self.root_dir, print_error=False).AndReturn(result1)
  138. results2 = { "Repository Root": "A different root" }
  139. scm.SVN.CaptureInfo(scm.os.path.dirname(self.root_dir),
  140. print_error=False).AndReturn(results2)
  141. self.mox.ReplayAll()
  142. self.assertEquals(scm.SVN.GetCheckoutRoot(self.root_dir + 'x'),
  143. self.root_dir)
  144. def testGetFileInfo(self):
  145. xml_text = r"""<?xml version="1.0"?>
  146. <info>
  147. <entry kind="file" path="%s" revision="14628">
  148. <url>http://src.chromium.org/svn/trunk/src/chrome/app/d</url>
  149. <repository><root>http://src.chromium.org/svn</root></repository>
  150. <wc-info>
  151. <schedule>add</schedule>
  152. <depth>infinity</depth>
  153. <copy-from-url>http://src.chromium.org/svn/trunk/src/chrome/app/DEPS</copy-from-url>
  154. <copy-from-rev>14628</copy-from-rev>
  155. <checksum>369f59057ba0e6d9017e28f8bdfb1f43</checksum>
  156. </wc-info>
  157. </entry>
  158. </info>
  159. """ % self.url
  160. self.mox.StubOutWithMock(scm.SVN, 'Capture')
  161. scm.SVN.Capture(['info', '--xml', self.url], '.', True).AndReturn(xml_text)
  162. expected = {
  163. 'URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/d',
  164. 'UUID': None,
  165. 'Repository Root': 'http://src.chromium.org/svn',
  166. 'Schedule': 'add',
  167. 'Copied From URL':
  168. 'http://src.chromium.org/svn/trunk/src/chrome/app/DEPS',
  169. 'Copied From Rev': '14628',
  170. 'Path': self.url,
  171. 'Revision': 14628,
  172. 'Node Kind': 'file',
  173. }
  174. self.mox.ReplayAll()
  175. file_info = scm.SVN.CaptureInfo(self.url, '.', True)
  176. self.assertEquals(sorted(file_info.items()), sorted(expected.items()))
  177. def testCaptureInfo(self):
  178. xml_text = """<?xml version="1.0"?>
  179. <info>
  180. <entry
  181. kind="dir"
  182. path="."
  183. revision="35">
  184. <url>%s</url>
  185. <repository>
  186. <root>%s</root>
  187. <uuid>7b9385f5-0452-0410-af26-ad4892b7a1fb</uuid>
  188. </repository>
  189. <wc-info>
  190. <schedule>normal</schedule>
  191. <depth>infinity</depth>
  192. </wc-info>
  193. <commit
  194. revision="35">
  195. <author>maruel</author>
  196. <date>2008-12-04T20:12:19.685120Z</date>
  197. </commit>
  198. </entry>
  199. </info>
  200. """ % (self.url, self.root_dir)
  201. self.mox.StubOutWithMock(scm.SVN, 'Capture')
  202. scm.SVN.Capture(['info', '--xml', self.url], '.', True).AndReturn(xml_text)
  203. self.mox.ReplayAll()
  204. file_info = scm.SVN.CaptureInfo(self.url, '.', True)
  205. expected = {
  206. 'URL': self.url,
  207. 'UUID': '7b9385f5-0452-0410-af26-ad4892b7a1fb',
  208. 'Revision': 35,
  209. 'Repository Root': self.root_dir,
  210. 'Schedule': 'normal',
  211. 'Copied From URL': None,
  212. 'Copied From Rev': None,
  213. 'Path': '.',
  214. 'Node Kind': 'directory',
  215. }
  216. self.assertEqual(file_info, expected)
  217. def testCaptureStatus(self):
  218. text =r"""<?xml version="1.0"?>
  219. <status>
  220. <target path=".">
  221. <entry path="unversionned_file.txt">
  222. <wc-status props="none" item="unversioned"></wc-status>
  223. </entry>
  224. <entry path="build\internal\essential.vsprops">
  225. <wc-status props="normal" item="modified" revision="14628">
  226. <commit revision="13818">
  227. <author>ajwong@chromium.org</author>
  228. <date>2009-04-16T00:42:06.872358Z</date>
  229. </commit>
  230. </wc-status>
  231. </entry>
  232. <entry path="chrome\app\d">
  233. <wc-status props="none" copied="true" tree-conflicted="true" item="added">
  234. </wc-status>
  235. </entry>
  236. <entry path="chrome\app\DEPS">
  237. <wc-status props="modified" item="modified" revision="14628">
  238. <commit revision="1279">
  239. <author>brettw@google.com</author>
  240. <date>2008-08-23T17:16:42.090152Z</date>
  241. </commit>
  242. </wc-status>
  243. </entry>
  244. <entry path="scripts\master\factory\gclient_factory.py">
  245. <wc-status props="normal" item="conflicted" revision="14725">
  246. <commit revision="14633">
  247. <author>nsylvain@chromium.org</author>
  248. <date>2009-04-27T19:37:17.977400Z</date>
  249. </commit>
  250. </wc-status>
  251. </entry>
  252. </target>
  253. </status>
  254. """
  255. proc = self.mox.CreateMockAnything()
  256. scm.subprocess.Popen(['svn', 'status', '--xml', '.'],
  257. cwd=None,
  258. shell=scm.sys.platform.startswith('win'),
  259. stderr=None,
  260. stdout=scm.subprocess.PIPE).AndReturn(proc)
  261. proc.communicate().AndReturn((text, 0))
  262. self.mox.ReplayAll()
  263. info = scm.SVN.CaptureStatus('.')
  264. expected = [
  265. ('? ', 'unversionned_file.txt'),
  266. ('M ', 'build\\internal\\essential.vsprops'),
  267. ('A + ', 'chrome\\app\\d'),
  268. ('MM ', 'chrome\\app\\DEPS'),
  269. ('C ', 'scripts\\master\\factory\\gclient_factory.py'),
  270. ]
  271. self.assertEquals(sorted(info), sorted(expected))
  272. def testRun(self):
  273. param2 = 'bleh'
  274. scm.gclient_utils.SubprocessCall(['svn', 'foo', 'bar'],
  275. param2).AndReturn(None)
  276. self.mox.ReplayAll()
  277. scm.SVN.Run(['foo', 'bar'], param2)
  278. def testCaptureStatusEmpty(self):
  279. text = r"""<?xml version="1.0"?>
  280. <status>
  281. <target
  282. path="perf">
  283. </target>
  284. </status>"""
  285. proc = self.mox.CreateMockAnything()
  286. scm.subprocess.Popen(['svn', 'status', '--xml'],
  287. cwd=None,
  288. shell=scm.sys.platform.startswith('win'),
  289. stderr=None,
  290. stdout=scm.subprocess.PIPE).AndReturn(proc)
  291. proc.communicate().AndReturn((text, 0))
  292. self.mox.ReplayAll()
  293. info = scm.SVN.CaptureStatus(None)
  294. self.assertEquals(info, [])
  295. if __name__ == '__main__':
  296. import unittest
  297. unittest.main()
  298. # vim: ts=2:sw=2:tw=80:et: