scm_unittest.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #!/usr/bin/env vpython3
  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. from unittest import mock
  11. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. from testing_support import fake_repos
  13. import scm
  14. import subprocess
  15. import subprocess2
  16. def callError(code=1, cmd='', cwd='', stdout=b'', stderr=b''):
  17. return subprocess2.CalledProcessError(code, cmd, cwd, stdout, stderr)
  18. class GitWrapperTestCase(unittest.TestCase):
  19. def setUp(self):
  20. super(GitWrapperTestCase, self).setUp()
  21. self.root_dir = '/foo/bar'
  22. def testRefToRemoteRef(self):
  23. remote = 'origin'
  24. refs = {
  25. 'refs/branch-heads/1234': ('refs/remotes/branch-heads/', '1234'),
  26. # local refs for upstream branch
  27. 'refs/remotes/%s/foobar' % remote:
  28. ('refs/remotes/%s/' % remote, 'foobar'),
  29. '%s/foobar' % remote: ('refs/remotes/%s/' % remote, 'foobar'),
  30. # upstream ref for branch
  31. 'refs/heads/foobar': ('refs/remotes/%s/' % remote, 'foobar'),
  32. # could be either local or upstream ref, assumed to refer to
  33. # upstream, but probably don't want to encourage refs like this.
  34. 'heads/foobar': ('refs/remotes/%s/' % remote, 'foobar'),
  35. # underspecified, probably intended to refer to a local branch
  36. 'foobar':
  37. None,
  38. # tags and other refs
  39. 'refs/tags/TAG':
  40. None,
  41. 'refs/changes/34/1234':
  42. None,
  43. }
  44. for k, v in refs.items():
  45. r = scm.GIT.RefToRemoteRef(k, remote)
  46. self.assertEqual(r, v, msg='%s -> %s, expected %s' % (k, r, v))
  47. def testRemoteRefToRef(self):
  48. remote = 'origin'
  49. refs = {
  50. 'refs/remotes/branch-heads/1234': 'refs/branch-heads/1234',
  51. # local refs for upstream branch
  52. 'refs/remotes/origin/foobar': 'refs/heads/foobar',
  53. # tags and other refs
  54. 'refs/tags/TAG': 'refs/tags/TAG',
  55. 'refs/changes/34/1234': 'refs/changes/34/1234',
  56. # different remote
  57. 'refs/remotes/other-remote/foobar': None,
  58. # underspecified, probably intended to refer to a local branch
  59. 'heads/foobar': None,
  60. 'origin/foobar': None,
  61. 'foobar': None,
  62. None: None,
  63. }
  64. for k, v in refs.items():
  65. r = scm.GIT.RemoteRefToRef(k, remote)
  66. self.assertEqual(r, v, msg='%s -> %s, expected %s' % (k, r, v))
  67. @mock.patch('scm.GIT.Capture')
  68. @mock.patch('os.path.exists', lambda _: True)
  69. def testGetRemoteHeadRefLocal(self, mockCapture):
  70. mockCapture.side_effect = ['refs/remotes/origin/main']
  71. self.assertEqual(
  72. 'refs/remotes/origin/main',
  73. scm.GIT.GetRemoteHeadRef('foo', 'proto://url', 'origin'))
  74. self.assertEqual(mockCapture.call_count, 1)
  75. @mock.patch('scm.GIT.Capture')
  76. @mock.patch('os.path.exists', lambda _: True)
  77. def testGetRemoteHeadRefLocalUpdateHead(self, mockCapture):
  78. mockCapture.side_effect = [
  79. 'refs/remotes/origin/master', # first symbolic-ref call
  80. 'foo', # set-head call
  81. 'refs/remotes/origin/main', # second symbolic-ref call
  82. ]
  83. self.assertEqual(
  84. 'refs/remotes/origin/main',
  85. scm.GIT.GetRemoteHeadRef('foo', 'proto://url', 'origin'))
  86. self.assertEqual(mockCapture.call_count, 3)
  87. @mock.patch('scm.GIT.Capture')
  88. @mock.patch('os.path.exists', lambda _: True)
  89. def testGetRemoteHeadRefRemote(self, mockCapture):
  90. mockCapture.side_effect = [
  91. subprocess2.CalledProcessError(1, '', '', '', ''),
  92. 'ref: refs/heads/main\tHEAD\n' +
  93. '0000000000000000000000000000000000000000\tHEAD',
  94. ]
  95. self.assertEqual(
  96. 'refs/remotes/origin/main',
  97. scm.GIT.GetRemoteHeadRef('foo', 'proto://url', 'origin'))
  98. self.assertEqual(mockCapture.call_count, 2)
  99. @mock.patch('scm.GIT.Capture')
  100. def testIsVersioned(self, mockCapture):
  101. mockCapture.return_value = (
  102. '160000 blob 423dc77d2182cb2687c53598a1dcef62ea2804ae dir')
  103. actual_state = scm.GIT.IsVersioned('cwd', 'dir')
  104. self.assertEqual(actual_state, scm.VERSIONED_SUBMODULE)
  105. mockCapture.return_value = ''
  106. actual_state = scm.GIT.IsVersioned('cwd', 'dir')
  107. self.assertEqual(actual_state, scm.VERSIONED_NO)
  108. mockCapture.return_value = (
  109. '040000 tree ef016abffb316e47a02af447bc51342dcef6f3ca dir')
  110. actual_state = scm.GIT.IsVersioned('cwd', 'dir')
  111. self.assertEqual(actual_state, scm.VERSIONED_DIR)
  112. @mock.patch('os.path.exists', return_value=True)
  113. @mock.patch('scm.GIT.Capture')
  114. def testListSubmodules(self, mockCapture, *_mock):
  115. mockCapture.return_value = (
  116. 'submodule.submodulename.path foo/path/script'
  117. '\nsubmodule.submodule2name.path foo/path/script2')
  118. actual_list = scm.GIT.ListSubmodules('root')
  119. if sys.platform.startswith('win'):
  120. self.assertEqual(actual_list,
  121. ['foo\\path\\script', 'foo\\path\\script2'])
  122. else:
  123. self.assertEqual(actual_list,
  124. ['foo/path/script', 'foo/path/script2'])
  125. def testListSubmodules_missing(self):
  126. self.assertEqual(scm.GIT.ListSubmodules('root'), [])
  127. class RealGitTest(fake_repos.FakeReposTestBase):
  128. def setUp(self):
  129. super(RealGitTest, self).setUp()
  130. self.enabled = self.FAKE_REPOS.set_up_git()
  131. if self.enabled:
  132. self.cwd = scm.os.path.join(self.FAKE_REPOS.git_base, 'repo_1')
  133. else:
  134. self.skipTest('git fake repos not available')
  135. def testResolveCommit(self):
  136. with self.assertRaises(Exception):
  137. scm.GIT.ResolveCommit(self.cwd, 'zebra')
  138. with self.assertRaises(Exception):
  139. scm.GIT.ResolveCommit(self.cwd, 'r123456')
  140. first_rev = self.githash('repo_1', 1)
  141. self.assertEqual(first_rev, scm.GIT.ResolveCommit(self.cwd, first_rev))
  142. self.assertEqual(self.githash('repo_1', 2),
  143. scm.GIT.ResolveCommit(self.cwd, 'HEAD'))
  144. def testIsValidRevision(self):
  145. # Sha1's are [0-9a-z]{32}, so starting with a 'z' or 'r' should always
  146. # fail.
  147. self.assertFalse(scm.GIT.IsValidRevision(cwd=self.cwd, rev='zebra'))
  148. self.assertFalse(scm.GIT.IsValidRevision(cwd=self.cwd, rev='r123456'))
  149. # Valid cases
  150. first_rev = self.githash('repo_1', 1)
  151. self.assertTrue(scm.GIT.IsValidRevision(cwd=self.cwd, rev=first_rev))
  152. self.assertTrue(scm.GIT.IsValidRevision(cwd=self.cwd, rev='HEAD'))
  153. def testIsAncestor(self):
  154. self.assertTrue(
  155. scm.GIT.IsAncestor(self.githash('repo_1', 1),
  156. self.githash('repo_1', 2),
  157. cwd=self.cwd))
  158. self.assertFalse(
  159. scm.GIT.IsAncestor(self.githash('repo_1', 2),
  160. self.githash('repo_1', 1),
  161. cwd=self.cwd))
  162. self.assertFalse(scm.GIT.IsAncestor(self.githash('repo_1', 1), 'zebra'))
  163. def testGetAllFiles(self):
  164. self.assertEqual(['DEPS', 'foo bar', 'origin'],
  165. scm.GIT.GetAllFiles(self.cwd))
  166. def testGetSetConfig(self):
  167. key = 'scm.test-key'
  168. self.assertIsNone(scm.GIT.GetConfig(self.cwd, key))
  169. self.assertEqual('default-value',
  170. scm.GIT.GetConfig(self.cwd, key, 'default-value'))
  171. scm.GIT.SetConfig(self.cwd, key, 'set-value')
  172. self.assertEqual('set-value', scm.GIT.GetConfig(self.cwd, key))
  173. self.assertEqual('set-value',
  174. scm.GIT.GetConfig(self.cwd, key, 'default-value'))
  175. scm.GIT.SetConfig(self.cwd, key, '')
  176. self.assertEqual('', scm.GIT.GetConfig(self.cwd, key))
  177. self.assertEqual('', scm.GIT.GetConfig(self.cwd, key, 'default-value'))
  178. scm.GIT._clear_config(self.cwd)
  179. subprocess.run(['git', 'config', key, 'line 1\nline 2\nline 3'],
  180. cwd=self.cwd)
  181. self.assertEqual('line 1\nline 2\nline 3',
  182. scm.GIT.GetConfig(self.cwd, key))
  183. self.assertEqual('line 1\nline 2\nline 3',
  184. scm.GIT.GetConfig(self.cwd, key, 'default-value'))
  185. scm.GIT.SetConfig(self.cwd, key)
  186. self.assertIsNone(scm.GIT.GetConfig(self.cwd, key))
  187. self.assertEqual('default-value',
  188. scm.GIT.GetConfig(self.cwd, key, 'default-value'))
  189. def testGetSetConfigBool(self):
  190. key = 'scm.test-key'
  191. self.assertFalse(scm.GIT.GetConfigBool(self.cwd, key))
  192. scm.GIT.SetConfig(self.cwd, key, 'true')
  193. self.assertTrue(scm.GIT.GetConfigBool(self.cwd, key))
  194. scm.GIT.SetConfig(self.cwd, key)
  195. self.assertFalse(scm.GIT.GetConfigBool(self.cwd, key))
  196. def testGetSetConfigList(self):
  197. key = 'scm.test-key'
  198. self.assertListEqual([], scm.GIT.GetConfigList(self.cwd, key))
  199. scm.GIT.SetConfig(self.cwd, key, 'foo')
  200. scm.GIT.Capture(['config', '--add', key, 'bar'], cwd=self.cwd)
  201. self.assertListEqual(['foo', 'bar'],
  202. scm.GIT.GetConfigList(self.cwd, key))
  203. scm.GIT.SetConfig(self.cwd, key, modify_all=True, value_pattern='^f')
  204. self.assertListEqual(['bar'], scm.GIT.GetConfigList(self.cwd, key))
  205. scm.GIT.SetConfig(self.cwd, key)
  206. self.assertListEqual([], scm.GIT.GetConfigList(self.cwd, key))
  207. def testYieldConfigRegexp(self):
  208. key1 = 'scm.aaa'
  209. key2 = 'scm.aaab'
  210. config = scm.GIT.YieldConfigRegexp(self.cwd, key1)
  211. with self.assertRaises(StopIteration):
  212. next(config)
  213. scm.GIT.SetConfig(self.cwd, key1, 'foo')
  214. scm.GIT.SetConfig(self.cwd, key2, 'bar')
  215. scm.GIT.Capture(['config', '--add', key2, 'baz'], cwd=self.cwd)
  216. config = scm.GIT.YieldConfigRegexp(self.cwd, '^scm\\.aaa')
  217. self.assertEqual((key1, 'foo'), next(config))
  218. self.assertEqual((key2, 'bar'), next(config))
  219. self.assertEqual((key2, 'baz'), next(config))
  220. with self.assertRaises(StopIteration):
  221. next(config)
  222. def testGetSetBranchConfig(self):
  223. branch = scm.GIT.GetBranch(self.cwd)
  224. key = 'scm.test-key'
  225. self.assertIsNone(scm.GIT.GetBranchConfig(self.cwd, branch, key))
  226. self.assertEqual(
  227. 'default-value',
  228. scm.GIT.GetBranchConfig(self.cwd, branch, key, 'default-value'))
  229. scm.GIT.SetBranchConfig(self.cwd, branch, key, 'set-value')
  230. self.assertEqual('set-value',
  231. scm.GIT.GetBranchConfig(self.cwd, branch, key))
  232. self.assertEqual(
  233. 'set-value',
  234. scm.GIT.GetBranchConfig(self.cwd, branch, key, 'default-value'))
  235. self.assertEqual(
  236. 'set-value',
  237. scm.GIT.GetConfig(self.cwd, 'branch.%s.%s' % (branch, key)))
  238. scm.GIT.SetBranchConfig(self.cwd, branch, key)
  239. self.assertIsNone(scm.GIT.GetBranchConfig(self.cwd, branch, key))
  240. def testFetchUpstreamTuple_NoUpstreamFound(self):
  241. self.assertEqual((None, None), scm.GIT.FetchUpstreamTuple(self.cwd))
  242. @mock.patch('scm.GIT.GetRemoteBranches', return_value=['origin/main'])
  243. def testFetchUpstreamTuple_GuessOriginMaster(self, _mockGetRemoteBranches):
  244. self.assertEqual(('origin', 'refs/heads/main'),
  245. scm.GIT.FetchUpstreamTuple(self.cwd))
  246. @mock.patch('scm.GIT.GetRemoteBranches',
  247. return_value=['origin/master', 'origin/main'])
  248. def testFetchUpstreamTuple_GuessOriginMain(self, _mockGetRemoteBranches):
  249. self.assertEqual(('origin', 'refs/heads/main'),
  250. scm.GIT.FetchUpstreamTuple(self.cwd))
  251. def testFetchUpstreamTuple_RietveldUpstreamConfig(self):
  252. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-branch',
  253. 'rietveld-upstream')
  254. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-remote',
  255. 'rietveld-remote')
  256. self.assertEqual(('rietveld-remote', 'rietveld-upstream'),
  257. scm.GIT.FetchUpstreamTuple(self.cwd))
  258. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-branch')
  259. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-remote')
  260. @mock.patch('scm.GIT.GetBranch', side_effect=callError())
  261. def testFetchUpstreamTuple_NotOnBranch(self, _mockGetBranch):
  262. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-branch',
  263. 'rietveld-upstream')
  264. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-remote',
  265. 'rietveld-remote')
  266. self.assertEqual(('rietveld-remote', 'rietveld-upstream'),
  267. scm.GIT.FetchUpstreamTuple(self.cwd))
  268. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-branch')
  269. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-remote')
  270. def testFetchUpstreamTuple_BranchConfig(self):
  271. branch = scm.GIT.GetBranch(self.cwd)
  272. scm.GIT.SetBranchConfig(self.cwd, branch, 'merge', 'branch-merge')
  273. scm.GIT.SetBranchConfig(self.cwd, branch, 'remote', 'branch-remote')
  274. self.assertEqual(('branch-remote', 'branch-merge'),
  275. scm.GIT.FetchUpstreamTuple(self.cwd))
  276. scm.GIT.SetBranchConfig(self.cwd, branch, 'merge')
  277. scm.GIT.SetBranchConfig(self.cwd, branch, 'remote')
  278. def testFetchUpstreamTuple_AnotherBranchConfig(self):
  279. branch = 'scm-test-branch'
  280. scm.GIT.SetBranchConfig(self.cwd, branch, 'merge', 'other-merge')
  281. scm.GIT.SetBranchConfig(self.cwd, branch, 'remote', 'other-remote')
  282. self.assertEqual(('other-remote', 'other-merge'),
  283. scm.GIT.FetchUpstreamTuple(self.cwd, branch))
  284. scm.GIT.SetBranchConfig(self.cwd, branch, 'merge')
  285. scm.GIT.SetBranchConfig(self.cwd, branch, 'remote')
  286. def testGetBranchRef(self):
  287. self.assertEqual('refs/heads/main', scm.GIT.GetBranchRef(self.cwd))
  288. HEAD = scm.GIT.Capture(['rev-parse', 'HEAD'], cwd=self.cwd)
  289. scm.GIT.Capture(['checkout', HEAD], cwd=self.cwd)
  290. self.assertIsNone(scm.GIT.GetBranchRef(self.cwd))
  291. scm.GIT.Capture(['checkout', 'main'], cwd=self.cwd)
  292. def testGetBranch(self):
  293. self.assertEqual('main', scm.GIT.GetBranch(self.cwd))
  294. HEAD = scm.GIT.Capture(['rev-parse', 'HEAD'], cwd=self.cwd)
  295. scm.GIT.Capture(['checkout', HEAD], cwd=self.cwd)
  296. self.assertIsNone(scm.GIT.GetBranchRef(self.cwd))
  297. scm.GIT.Capture(['checkout', 'main'], cwd=self.cwd)
  298. if __name__ == '__main__':
  299. if '-v' in sys.argv:
  300. logging.basicConfig(level=logging.DEBUG)
  301. unittest.main()
  302. # vim: ts=2:sw=2:tw=80:et: