scm_unittest.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. # pylint: disable=E1101,W0403
  7. # Fixes include path.
  8. from super_mox import SuperMoxTestBase
  9. import scm
  10. class BaseTestCase(SuperMoxTestBase):
  11. # Like unittest's assertRaises, but checks for Gclient.Error.
  12. def assertRaisesError(self, msg, fn, *args, **kwargs):
  13. try:
  14. fn(*args, **kwargs)
  15. except scm.gclient_utils.Error, e:
  16. self.assertEquals(e.args[0], msg)
  17. else:
  18. self.fail('%s not raised' % msg)
  19. class BaseSCMTestCase(BaseTestCase):
  20. def setUp(self):
  21. BaseTestCase.setUp(self)
  22. self.mox.StubOutWithMock(scm.gclient_utils, 'CheckCall')
  23. self.mox.StubOutWithMock(scm.gclient_utils, 'CheckCallAndFilter')
  24. self.mox.StubOutWithMock(scm.gclient_utils, 'CheckCallAndFilterAndHeader')
  25. self.mox.StubOutWithMock(scm.gclient_utils, 'Popen')
  26. class RootTestCase(BaseSCMTestCase):
  27. def testMembersChanged(self):
  28. self.mox.ReplayAll()
  29. members = [
  30. 'GetCasedPath', 'GenFakeDiff', 'GIT', 'SVN', 'ValidateEmail',
  31. 'cStringIO', 'gclient_utils', 'glob', 'os', 're', 'shutil',
  32. 'subprocess', 'sys', 'tempfile', 'time', 'xml',
  33. ]
  34. # If this test fails, you should add the relevant test.
  35. self.compareMembers(scm, members)
  36. class GitWrapperTestCase(BaseSCMTestCase):
  37. def testMembersChanged(self):
  38. members = [
  39. 'AssertVersion', 'Capture', 'CaptureStatus',
  40. 'FetchUpstreamTuple',
  41. 'GenerateDiff', 'GetBranch', 'GetBranchRef', 'GetCheckoutRoot',
  42. 'GetDifferentFiles', 'GetEmail', 'GetPatchName', 'GetSVNBranch',
  43. 'GetUpstreamBranch', 'IsGitSvn', 'ShortBranchName',
  44. ]
  45. # If this test fails, you should add the relevant test.
  46. self.compareMembers(scm.GIT, members)
  47. def testGetEmail(self):
  48. self.mox.StubOutWithMock(scm.GIT, 'Capture')
  49. scm.GIT.Capture(['config', 'user.email'], cwd=self.root_dir
  50. ).AndReturn('mini@me.com')
  51. self.mox.ReplayAll()
  52. self.assertEqual(scm.GIT.GetEmail(self.root_dir), 'mini@me.com')
  53. class SVNTestCase(BaseSCMTestCase):
  54. def setUp(self):
  55. BaseSCMTestCase.setUp(self)
  56. self.mox.StubOutWithMock(scm.SVN, 'Capture')
  57. self.url = self.SvnUrl()
  58. def testMembersChanged(self):
  59. self.mox.ReplayAll()
  60. members = [
  61. 'AssertVersion', 'Capture', 'CaptureRevision', 'CaptureInfo',
  62. 'CaptureStatus', 'current_version', 'DiffItem', 'GenerateDiff',
  63. 'GetCheckoutRoot', 'GetEmail', 'GetFileProperty', 'IsMoved',
  64. 'IsMovedInfo', 'ReadSimpleAuth', 'RunAndGetFileList',
  65. ]
  66. # If this test fails, you should add the relevant test.
  67. self.compareMembers(scm.SVN, members)
  68. def testGetCheckoutRoot(self):
  69. self.mox.StubOutWithMock(scm.SVN, 'CaptureInfo')
  70. self.mox.StubOutWithMock(scm, 'GetCasedPath')
  71. scm.os.path.abspath(self.root_dir + 'x').AndReturn(self.root_dir)
  72. scm.GetCasedPath(self.root_dir).AndReturn(self.root_dir)
  73. result1 = { "Repository Root": "Some root" }
  74. scm.SVN.CaptureInfo(self.root_dir).AndReturn(result1)
  75. results2 = { "Repository Root": "A different root" }
  76. scm.SVN.CaptureInfo(
  77. scm.os.path.dirname(self.root_dir)).AndReturn(results2)
  78. self.mox.ReplayAll()
  79. self.assertEquals(scm.SVN.GetCheckoutRoot(self.root_dir + 'x'),
  80. self.root_dir)
  81. def testGetFileInfo(self):
  82. xml_text = r"""<?xml version="1.0"?>
  83. <info>
  84. <entry kind="file" path="%s" revision="14628">
  85. <url>http://src.chromium.org/svn/trunk/src/chrome/app/d</url>
  86. <repository><root>http://src.chromium.org/svn</root></repository>
  87. <wc-info>
  88. <schedule>add</schedule>
  89. <depth>infinity</depth>
  90. <copy-from-url>http://src.chromium.org/svn/trunk/src/chrome/app/DEPS</copy-from-url>
  91. <copy-from-rev>14628</copy-from-rev>
  92. <checksum>369f59057ba0e6d9017e28f8bdfb1f43</checksum>
  93. </wc-info>
  94. </entry>
  95. </info>
  96. """ % self.url
  97. scm.SVN.Capture(['info', '--xml', self.url]).AndReturn(xml_text)
  98. expected = {
  99. 'URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/d',
  100. 'UUID': None,
  101. 'Repository Root': 'http://src.chromium.org/svn',
  102. 'Schedule': 'add',
  103. 'Copied From URL':
  104. 'http://src.chromium.org/svn/trunk/src/chrome/app/DEPS',
  105. 'Copied From Rev': '14628',
  106. 'Path': self.url,
  107. 'Revision': 14628,
  108. 'Node Kind': 'file',
  109. }
  110. self.mox.ReplayAll()
  111. file_info = scm.SVN.CaptureInfo(self.url)
  112. self.assertEquals(sorted(file_info.items()), sorted(expected.items()))
  113. def testCaptureInfo(self):
  114. xml_text = """<?xml version="1.0"?>
  115. <info>
  116. <entry
  117. kind="dir"
  118. path="."
  119. revision="35">
  120. <url>%s</url>
  121. <repository>
  122. <root>%s</root>
  123. <uuid>7b9385f5-0452-0410-af26-ad4892b7a1fb</uuid>
  124. </repository>
  125. <wc-info>
  126. <schedule>normal</schedule>
  127. <depth>infinity</depth>
  128. </wc-info>
  129. <commit
  130. revision="35">
  131. <author>maruel</author>
  132. <date>2008-12-04T20:12:19.685120Z</date>
  133. </commit>
  134. </entry>
  135. </info>
  136. """ % (self.url, self.root_dir)
  137. scm.SVN.Capture(['info', '--xml', self.url]).AndReturn(xml_text)
  138. self.mox.ReplayAll()
  139. file_info = scm.SVN.CaptureInfo(self.url)
  140. expected = {
  141. 'URL': self.url,
  142. 'UUID': '7b9385f5-0452-0410-af26-ad4892b7a1fb',
  143. 'Revision': 35,
  144. 'Repository Root': self.root_dir,
  145. 'Schedule': 'normal',
  146. 'Copied From URL': None,
  147. 'Copied From Rev': None,
  148. 'Path': '.',
  149. 'Node Kind': 'directory',
  150. }
  151. self.assertEqual(file_info, expected)
  152. def testCaptureStatus(self):
  153. text = r"""<?xml version="1.0"?>
  154. <status>
  155. <target path=".">
  156. <entry path="unversionned_file.txt">
  157. <wc-status props="none" item="unversioned"></wc-status>
  158. </entry>
  159. <entry path="build\internal\essential.vsprops">
  160. <wc-status props="normal" item="modified" revision="14628">
  161. <commit revision="13818">
  162. <author>ajwong@chromium.org</author>
  163. <date>2009-04-16T00:42:06.872358Z</date>
  164. </commit>
  165. </wc-status>
  166. </entry>
  167. <entry path="chrome\app\d">
  168. <wc-status props="none" copied="true" tree-conflicted="true" item="added">
  169. </wc-status>
  170. </entry>
  171. <entry path="chrome\app\DEPS">
  172. <wc-status props="modified" item="modified" revision="14628">
  173. <commit revision="1279">
  174. <author>brettw@google.com</author>
  175. <date>2008-08-23T17:16:42.090152Z</date>
  176. </commit>
  177. </wc-status>
  178. </entry>
  179. <entry path="scripts\master\factory\gclient_factory.py">
  180. <wc-status props="normal" item="conflicted" revision="14725">
  181. <commit revision="14633">
  182. <author>nsylvain@chromium.org</author>
  183. <date>2009-04-27T19:37:17.977400Z</date>
  184. </commit>
  185. </wc-status>
  186. </entry>
  187. </target>
  188. </status>
  189. """
  190. scm.SVN.Capture(['status', '--xml', '.']).AndReturn(text)
  191. self.mox.ReplayAll()
  192. info = scm.SVN.CaptureStatus('.')
  193. expected = [
  194. ('? ', 'unversionned_file.txt'),
  195. ('M ', 'build\\internal\\essential.vsprops'),
  196. ('A + ', 'chrome\\app\\d'),
  197. ('MM ', 'chrome\\app\\DEPS'),
  198. ('C ', 'scripts\\master\\factory\\gclient_factory.py'),
  199. ]
  200. self.assertEquals(sorted(info), sorted(expected))
  201. def testCaptureStatusEmpty(self):
  202. text = r"""<?xml version="1.0"?>
  203. <status>
  204. <target
  205. path="perf">
  206. </target>
  207. </status>"""
  208. scm.SVN.Capture(['status', '--xml']).AndReturn(text)
  209. self.mox.ReplayAll()
  210. info = scm.SVN.CaptureStatus(None)
  211. self.assertEquals(info, [])
  212. if __name__ == '__main__':
  213. import unittest
  214. unittest.main()
  215. # vim: ts=2:sw=2:tw=80:et: