scm_unittest.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 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. import logging
  7. import os
  8. import sys
  9. import unittest
  10. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  11. from testing_support import fake_repos
  12. from testing_support.super_mox import SuperMoxTestBase
  13. import scm
  14. import subprocess2
  15. # Access to a protected member XXX of a client class
  16. # pylint: disable=protected-access
  17. class BaseTestCase(SuperMoxTestBase):
  18. # Like unittest's assertRaises, but checks for Gclient.Error.
  19. def assertRaisesError(self, msg, fn, *args, **kwargs):
  20. try:
  21. fn(*args, **kwargs)
  22. except scm.gclient_utils.Error, e:
  23. self.assertEquals(e.args[0], msg)
  24. else:
  25. self.fail('%s not raised' % msg)
  26. class BaseSCMTestCase(BaseTestCase):
  27. def setUp(self):
  28. BaseTestCase.setUp(self)
  29. self.mox.StubOutWithMock(scm.gclient_utils, 'CheckCallAndFilter')
  30. self.mox.StubOutWithMock(scm.gclient_utils, 'CheckCallAndFilterAndHeader')
  31. self.mox.StubOutWithMock(subprocess2, 'Popen')
  32. self.mox.StubOutWithMock(subprocess2, 'communicate')
  33. class RootTestCase(BaseSCMTestCase):
  34. def testMembersChanged(self):
  35. self.mox.ReplayAll()
  36. members = [
  37. 'cStringIO',
  38. 'determine_scm',
  39. 'ElementTree',
  40. 'gclient_utils',
  41. 'GenFakeDiff',
  42. 'GetCasedPath',
  43. 'GIT',
  44. 'glob',
  45. 'logging',
  46. 'only_int',
  47. 'os',
  48. 're',
  49. 'subprocess2',
  50. 'SVN',
  51. 'sys',
  52. 'tempfile',
  53. 'time',
  54. 'ValidateEmail',
  55. ]
  56. # If this test fails, you should add the relevant test.
  57. self.compareMembers(scm, members)
  58. class GitWrapperTestCase(BaseSCMTestCase):
  59. def testMembersChanged(self):
  60. members = [
  61. 'ApplyEnvVars',
  62. 'AssertVersion',
  63. 'Capture',
  64. 'CaptureStatus',
  65. 'CleanupDir',
  66. 'current_version',
  67. 'FetchUpstreamTuple',
  68. 'GenerateDiff',
  69. 'GetBlessedSha1ForSvnRev',
  70. 'GetBranch',
  71. 'GetBranchRef',
  72. 'GetCheckoutRoot',
  73. 'GetDifferentFiles',
  74. 'GetEmail',
  75. 'GetGitDir',
  76. 'GetGitSvnHeadRev',
  77. 'GetPatchName',
  78. 'GetSha1ForSvnRev',
  79. 'GetSVNBranch',
  80. 'GetUpstreamBranch',
  81. 'IsDirectoryVersioned',
  82. 'IsGitSvn',
  83. 'IsInsideWorkTree',
  84. 'IsValidRevision',
  85. 'IsWorkTreeDirty',
  86. 'MatchSvnGlob',
  87. 'ParseGitSvnSha1',
  88. 'RefToRemoteRef',
  89. 'ShortBranchName',
  90. ]
  91. # If this test fails, you should add the relevant test.
  92. self.compareMembers(scm.GIT, members)
  93. def testGetEmail(self):
  94. self.mox.StubOutWithMock(scm.GIT, 'Capture')
  95. scm.GIT.Capture(['config', 'user.email'], cwd=self.root_dir
  96. ).AndReturn('mini@me.com')
  97. self.mox.ReplayAll()
  98. self.assertEqual(scm.GIT.GetEmail(self.root_dir), 'mini@me.com')
  99. def testMatchSvnGlob(self):
  100. self.assertEquals(scm.GIT.MatchSvnGlob(
  101. 'svn://svn.chromium.org/chrome/trunk/src',
  102. 'svn://svn.chromium.org/chrome',
  103. 'trunk/src:refs/remotes/origin/trunk',
  104. False), 'refs/remotes/origin/trunk')
  105. self.assertEquals(scm.GIT.MatchSvnGlob(
  106. 'https://v8.googlecode.com/svn/branches/bleeding_edge',
  107. 'https://v8.googlecode.com/svn',
  108. 'branches/*:refs/remotes/*',
  109. True), 'refs/remotes/bleeding_edge')
  110. def testRefToRemoteRefNoRemote(self):
  111. refs = {
  112. # local ref for upstream branch-head
  113. 'refs/remotes/branch-heads/1234': ('refs/remotes/branch-heads/',
  114. '1234'),
  115. # upstream ref for branch-head
  116. 'refs/branch-heads/1234': ('refs/remotes/branch-heads/', '1234'),
  117. # could be either local or upstream ref, assumed to refer to
  118. # upstream, but probably don't want to encourage refs like this.
  119. 'branch-heads/1234': ('refs/remotes/branch-heads/', '1234'),
  120. # actively discouraging refs like this, should prepend with 'refs/'
  121. 'remotes/branch-heads/1234': None,
  122. # might be non-"branch-heads" upstream branches, but can't resolve
  123. # without knowing the remote.
  124. 'refs/heads/1234': None,
  125. 'heads/1234': None,
  126. # underspecified, probably intended to refer to a local branch
  127. '1234': None,
  128. }
  129. for k, v in refs.items():
  130. r = scm.GIT.RefToRemoteRef(k)
  131. self.assertEqual(r, v, msg='%s -> %s, expected %s' % (k, r, v))
  132. def testRefToRemoteRefWithRemote(self):
  133. remote = 'origin'
  134. refs = {
  135. # This shouldn't be any different from the NoRemote() version.
  136. 'refs/branch-heads/1234': ('refs/remotes/branch-heads/', '1234'),
  137. # local refs for upstream branch
  138. 'refs/remotes/%s/foobar' % remote: ('refs/remotes/%s/' % remote,
  139. 'foobar'),
  140. '%s/foobar' % remote: ('refs/remotes/%s/' % remote, 'foobar'),
  141. # upstream ref for branch
  142. 'refs/heads/foobar': ('refs/remotes/%s/' % remote, 'foobar'),
  143. # could be either local or upstream ref, assumed to refer to
  144. # upstream, but probably don't want to encourage refs like this.
  145. 'heads/foobar': ('refs/remotes/%s/' % remote, 'foobar'),
  146. # underspecified, probably intended to refer to a local branch
  147. 'foobar': None,
  148. }
  149. for k, v in refs.items():
  150. r = scm.GIT.RefToRemoteRef(k, remote)
  151. self.assertEqual(r, v, msg='%s -> %s, expected %s' % (k, r, v))
  152. class RealGitTest(fake_repos.FakeReposTestBase):
  153. def setUp(self):
  154. super(RealGitTest, self).setUp()
  155. self.enabled = self.FAKE_REPOS.set_up_git()
  156. if self.enabled:
  157. self.clone_dir = scm.os.path.join(self.FAKE_REPOS.git_root, 'repo_1')
  158. def testIsValidRevision(self):
  159. if not self.enabled:
  160. return
  161. # Sha1's are [0-9a-z]{32}, so starting with a 'z' or 'r' should always fail.
  162. self.assertFalse(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev='zebra'))
  163. self.assertFalse(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev='r123456'))
  164. # Valid cases
  165. first_rev = self.githash('repo_1', 1)
  166. self.assertTrue(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev=first_rev))
  167. self.assertTrue(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev='HEAD'))
  168. if __name__ == '__main__':
  169. if '-v' in sys.argv:
  170. logging.basicConfig(level=logging.DEBUG)
  171. unittest.main()
  172. # vim: ts=2:sw=2:tw=80:et: