scm_unittest.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. subprocess2.CalledProcessError(1, '', '', '', ''),
  93. 'ref: refs/heads/main\tHEAD\n' +
  94. '0000000000000000000000000000000000000000\tHEAD',
  95. ]
  96. self.assertEqual(
  97. 'refs/remotes/origin/main',
  98. scm.GIT.GetRemoteHeadRef('foo', 'proto://url', 'origin'))
  99. self.assertEqual(mockCapture.call_count, 3)
  100. @mock.patch('scm.GIT.Capture')
  101. def testIsVersioned(self, mockCapture):
  102. mockCapture.return_value = (
  103. '160000 blob 423dc77d2182cb2687c53598a1dcef62ea2804ae dir')
  104. actual_state = scm.GIT.IsVersioned('cwd', 'dir')
  105. self.assertEqual(actual_state, scm.VERSIONED_SUBMODULE)
  106. mockCapture.return_value = ''
  107. actual_state = scm.GIT.IsVersioned('cwd', 'dir')
  108. self.assertEqual(actual_state, scm.VERSIONED_NO)
  109. mockCapture.return_value = (
  110. '040000 tree ef016abffb316e47a02af447bc51342dcef6f3ca dir')
  111. actual_state = scm.GIT.IsVersioned('cwd', 'dir')
  112. self.assertEqual(actual_state, scm.VERSIONED_DIR)
  113. @mock.patch('os.path.exists', return_value=True)
  114. @mock.patch('scm.GIT.Capture')
  115. def testListSubmodules(self, mockCapture, *_mock):
  116. mockCapture.return_value = (
  117. 'submodule.submodulename.path foo/path/script'
  118. '\nsubmodule.submodule2name.path foo/path/script2')
  119. actual_list = scm.GIT.ListSubmodules('root')
  120. if sys.platform.startswith('win'):
  121. self.assertEqual(actual_list,
  122. ['foo\\path\\script', 'foo\\path\\script2'])
  123. else:
  124. self.assertEqual(actual_list,
  125. ['foo/path/script', 'foo/path/script2'])
  126. def testListSubmodules_missing(self):
  127. self.assertEqual(scm.GIT.ListSubmodules('root'), [])
  128. class RealGitTest(fake_repos.FakeReposTestBase):
  129. def setUp(self):
  130. super(RealGitTest, self).setUp()
  131. self.enabled = self.FAKE_REPOS.set_up_git()
  132. if self.enabled:
  133. self.cwd = scm.os.path.join(self.FAKE_REPOS.git_base, 'repo_1')
  134. else:
  135. self.skipTest('git fake repos not available')
  136. def testResolveCommit(self):
  137. with self.assertRaises(Exception):
  138. scm.GIT.ResolveCommit(self.cwd, 'zebra')
  139. with self.assertRaises(Exception):
  140. scm.GIT.ResolveCommit(self.cwd, 'r123456')
  141. first_rev = self.githash('repo_1', 1)
  142. self.assertEqual(first_rev, scm.GIT.ResolveCommit(self.cwd, first_rev))
  143. self.assertEqual(self.githash('repo_1', 2),
  144. scm.GIT.ResolveCommit(self.cwd, 'HEAD'))
  145. def testIsValidRevision(self):
  146. # Sha1's are [0-9a-z]{32}, so starting with a 'z' or 'r' should always
  147. # fail.
  148. self.assertFalse(scm.GIT.IsValidRevision(cwd=self.cwd, rev='zebra'))
  149. self.assertFalse(scm.GIT.IsValidRevision(cwd=self.cwd, rev='r123456'))
  150. # Valid cases
  151. first_rev = self.githash('repo_1', 1)
  152. self.assertTrue(scm.GIT.IsValidRevision(cwd=self.cwd, rev=first_rev))
  153. self.assertTrue(scm.GIT.IsValidRevision(cwd=self.cwd, rev='HEAD'))
  154. def testIsAncestor(self):
  155. self.assertTrue(
  156. scm.GIT.IsAncestor(self.githash('repo_1', 1),
  157. self.githash('repo_1', 2),
  158. cwd=self.cwd))
  159. self.assertFalse(
  160. scm.GIT.IsAncestor(self.githash('repo_1', 2),
  161. self.githash('repo_1', 1),
  162. cwd=self.cwd))
  163. self.assertFalse(scm.GIT.IsAncestor(self.githash('repo_1', 1), 'zebra'))
  164. def testGetAllFiles(self):
  165. self.assertEqual(['DEPS', 'foo bar', 'origin'],
  166. scm.GIT.GetAllFiles(self.cwd))
  167. def testGetSetConfig(self):
  168. key = 'scm.test-key'
  169. self.assertIsNone(scm.GIT.GetConfig(self.cwd, key))
  170. self.assertEqual('default-value',
  171. scm.GIT.GetConfig(self.cwd, key, 'default-value'))
  172. scm.GIT.SetConfig(self.cwd, key, 'set-value')
  173. self.assertEqual('set-value', scm.GIT.GetConfig(self.cwd, key))
  174. self.assertEqual('set-value',
  175. scm.GIT.GetConfig(self.cwd, key, 'default-value'))
  176. scm.GIT.SetConfig(self.cwd, key, '')
  177. self.assertEqual('', scm.GIT.GetConfig(self.cwd, key))
  178. self.assertEqual('', scm.GIT.GetConfig(self.cwd, key, 'default-value'))
  179. scm.GIT._clear_config(self.cwd)
  180. subprocess.run(['git', 'config', key, 'line 1\nline 2\nline 3'],
  181. cwd=self.cwd)
  182. self.assertEqual('line 1\nline 2\nline 3',
  183. scm.GIT.GetConfig(self.cwd, key))
  184. self.assertEqual('line 1\nline 2\nline 3',
  185. scm.GIT.GetConfig(self.cwd, key, 'default-value'))
  186. scm.GIT.SetConfig(self.cwd, key)
  187. self.assertIsNone(scm.GIT.GetConfig(self.cwd, key))
  188. self.assertEqual('default-value',
  189. scm.GIT.GetConfig(self.cwd, key, 'default-value'))
  190. def testGetSetConfigBool(self):
  191. key = 'scm.test-key'
  192. self.assertFalse(scm.GIT.GetConfigBool(self.cwd, key))
  193. scm.GIT.SetConfig(self.cwd, key, 'true')
  194. self.assertTrue(scm.GIT.GetConfigBool(self.cwd, key))
  195. scm.GIT.SetConfig(self.cwd, key)
  196. self.assertFalse(scm.GIT.GetConfigBool(self.cwd, key))
  197. def testGetSetConfigList(self):
  198. key = 'scm.test-key'
  199. self.assertListEqual([], scm.GIT.GetConfigList(self.cwd, key))
  200. scm.GIT.SetConfig(self.cwd, key, 'foo')
  201. scm.GIT.Capture(['config', '--add', key, 'bar'], cwd=self.cwd)
  202. self.assertListEqual(['foo', 'bar'],
  203. scm.GIT.GetConfigList(self.cwd, key))
  204. scm.GIT.SetConfig(self.cwd, key, modify_all=True, value_pattern='^f')
  205. self.assertListEqual(['bar'], scm.GIT.GetConfigList(self.cwd, key))
  206. scm.GIT.SetConfig(self.cwd, key)
  207. self.assertListEqual([], scm.GIT.GetConfigList(self.cwd, key))
  208. def testYieldConfigRegexp(self):
  209. key1 = 'scm.aaa'
  210. key2 = 'scm.aaab'
  211. config = scm.GIT.YieldConfigRegexp(self.cwd, key1)
  212. with self.assertRaises(StopIteration):
  213. next(config)
  214. scm.GIT.SetConfig(self.cwd, key1, 'foo')
  215. scm.GIT.SetConfig(self.cwd, key2, 'bar')
  216. scm.GIT.Capture(['config', '--add', key2, 'baz'], cwd=self.cwd)
  217. config = scm.GIT.YieldConfigRegexp(self.cwd, '^scm\\.aaa')
  218. self.assertEqual((key1, 'foo'), next(config))
  219. self.assertEqual((key2, 'bar'), next(config))
  220. self.assertEqual((key2, 'baz'), next(config))
  221. with self.assertRaises(StopIteration):
  222. next(config)
  223. def testGetSetBranchConfig(self):
  224. branch = scm.GIT.GetBranch(self.cwd)
  225. key = 'scm.test-key'
  226. self.assertIsNone(scm.GIT.GetBranchConfig(self.cwd, branch, key))
  227. self.assertEqual(
  228. 'default-value',
  229. scm.GIT.GetBranchConfig(self.cwd, branch, key, 'default-value'))
  230. scm.GIT.SetBranchConfig(self.cwd, branch, key, 'set-value')
  231. self.assertEqual('set-value',
  232. scm.GIT.GetBranchConfig(self.cwd, branch, key))
  233. self.assertEqual(
  234. 'set-value',
  235. scm.GIT.GetBranchConfig(self.cwd, branch, key, 'default-value'))
  236. self.assertEqual(
  237. 'set-value',
  238. scm.GIT.GetConfig(self.cwd, 'branch.%s.%s' % (branch, key)))
  239. scm.GIT.SetBranchConfig(self.cwd, branch, key)
  240. self.assertIsNone(scm.GIT.GetBranchConfig(self.cwd, branch, key))
  241. def testFetchUpstreamTuple_NoUpstreamFound(self):
  242. self.assertEqual((None, None), scm.GIT.FetchUpstreamTuple(self.cwd))
  243. @mock.patch('scm.GIT.GetRemoteBranches', return_value=['origin/main'])
  244. def testFetchUpstreamTuple_GuessOriginMaster(self, _mockGetRemoteBranches):
  245. self.assertEqual(('origin', 'refs/heads/main'),
  246. scm.GIT.FetchUpstreamTuple(self.cwd))
  247. @mock.patch('scm.GIT.GetRemoteBranches',
  248. return_value=['origin/master', 'origin/main'])
  249. def testFetchUpstreamTuple_GuessOriginMain(self, _mockGetRemoteBranches):
  250. self.assertEqual(('origin', 'refs/heads/main'),
  251. scm.GIT.FetchUpstreamTuple(self.cwd))
  252. def testFetchUpstreamTuple_RietveldUpstreamConfig(self):
  253. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-branch',
  254. 'rietveld-upstream')
  255. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-remote',
  256. 'rietveld-remote')
  257. self.assertEqual(('rietveld-remote', 'rietveld-upstream'),
  258. scm.GIT.FetchUpstreamTuple(self.cwd))
  259. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-branch')
  260. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-remote')
  261. @mock.patch('scm.GIT.GetBranch', side_effect=callError())
  262. def testFetchUpstreamTuple_NotOnBranch(self, _mockGetBranch):
  263. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-branch',
  264. 'rietveld-upstream')
  265. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-remote',
  266. 'rietveld-remote')
  267. self.assertEqual(('rietveld-remote', 'rietveld-upstream'),
  268. scm.GIT.FetchUpstreamTuple(self.cwd))
  269. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-branch')
  270. scm.GIT.SetConfig(self.cwd, 'rietveld.upstream-remote')
  271. def testFetchUpstreamTuple_BranchConfig(self):
  272. branch = scm.GIT.GetBranch(self.cwd)
  273. scm.GIT.SetBranchConfig(self.cwd, branch, 'merge', 'branch-merge')
  274. scm.GIT.SetBranchConfig(self.cwd, branch, 'remote', 'branch-remote')
  275. self.assertEqual(('branch-remote', 'branch-merge'),
  276. scm.GIT.FetchUpstreamTuple(self.cwd))
  277. scm.GIT.SetBranchConfig(self.cwd, branch, 'merge')
  278. scm.GIT.SetBranchConfig(self.cwd, branch, 'remote')
  279. def testFetchUpstreamTuple_AnotherBranchConfig(self):
  280. branch = 'scm-test-branch'
  281. scm.GIT.SetBranchConfig(self.cwd, branch, 'merge', 'other-merge')
  282. scm.GIT.SetBranchConfig(self.cwd, branch, 'remote', 'other-remote')
  283. self.assertEqual(('other-remote', 'other-merge'),
  284. scm.GIT.FetchUpstreamTuple(self.cwd, branch))
  285. scm.GIT.SetBranchConfig(self.cwd, branch, 'merge')
  286. scm.GIT.SetBranchConfig(self.cwd, branch, 'remote')
  287. def testGetBranchRef(self):
  288. self.assertEqual('refs/heads/main', scm.GIT.GetBranchRef(self.cwd))
  289. HEAD = scm.GIT.Capture(['rev-parse', 'HEAD'], cwd=self.cwd)
  290. scm.GIT.Capture(['checkout', HEAD], cwd=self.cwd)
  291. self.assertIsNone(scm.GIT.GetBranchRef(self.cwd))
  292. scm.GIT.Capture(['checkout', 'main'], cwd=self.cwd)
  293. def testGetBranch(self):
  294. self.assertEqual('main', scm.GIT.GetBranch(self.cwd))
  295. HEAD = scm.GIT.Capture(['rev-parse', 'HEAD'], cwd=self.cwd)
  296. scm.GIT.Capture(['checkout', HEAD], cwd=self.cwd)
  297. self.assertIsNone(scm.GIT.GetBranchRef(self.cwd))
  298. scm.GIT.Capture(['checkout', 'main'], cwd=self.cwd)
  299. if __name__ == '__main__':
  300. if '-v' in sys.argv:
  301. logging.basicConfig(level=logging.DEBUG)
  302. unittest.main()
  303. # vim: ts=2:sw=2:tw=80:et: