scm_unittest.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  11. from third_party import mock
  12. from testing_support import fake_repos
  13. import scm
  14. import subprocess2
  15. class GitWrapperTestCase(unittest.TestCase):
  16. def setUp(self):
  17. super(GitWrapperTestCase, self).setUp()
  18. self.root_dir = '/foo/bar'
  19. @mock.patch('scm.GIT.Capture')
  20. def testGetEmail(self, mockCapture):
  21. mockCapture.return_value = 'mini@me.com'
  22. self.assertEqual(scm.GIT.GetEmail(self.root_dir), 'mini@me.com')
  23. mockCapture.assert_called_with(['config', 'user.email'], cwd=self.root_dir)
  24. def testRefToRemoteRef(self):
  25. remote = 'origin'
  26. refs = {
  27. 'refs/branch-heads/1234': ('refs/remotes/branch-heads/', '1234'),
  28. # local refs for upstream branch
  29. 'refs/remotes/%s/foobar' % remote: ('refs/remotes/%s/' % remote,
  30. 'foobar'),
  31. '%s/foobar' % remote: ('refs/remotes/%s/' % remote, 'foobar'),
  32. # upstream ref for branch
  33. 'refs/heads/foobar': ('refs/remotes/%s/' % remote, 'foobar'),
  34. # could be either local or upstream ref, assumed to refer to
  35. # upstream, but probably don't want to encourage refs like this.
  36. 'heads/foobar': ('refs/remotes/%s/' % remote, 'foobar'),
  37. # underspecified, probably intended to refer to a local branch
  38. 'foobar': None,
  39. # tags and other refs
  40. 'refs/tags/TAG': None,
  41. 'refs/changes/34/1234': None,
  42. }
  43. for k, v in refs.items():
  44. r = scm.GIT.RefToRemoteRef(k, remote)
  45. self.assertEqual(r, v, msg='%s -> %s, expected %s' % (k, r, v))
  46. def testRemoteRefToRef(self):
  47. remote = 'origin'
  48. refs = {
  49. 'refs/remotes/branch-heads/1234': 'refs/branch-heads/1234',
  50. # local refs for upstream branch
  51. 'refs/remotes/origin/foobar': 'refs/heads/foobar',
  52. # tags and other refs
  53. 'refs/tags/TAG': 'refs/tags/TAG',
  54. 'refs/changes/34/1234': 'refs/changes/34/1234',
  55. # different remote
  56. 'refs/remotes/other-remote/foobar': None,
  57. # underspecified, probably intended to refer to a local branch
  58. 'heads/foobar': None,
  59. 'origin/foobar': None,
  60. 'foobar': None,
  61. None: None,
  62. }
  63. for k, v in refs.items():
  64. r = scm.GIT.RemoteRefToRef(k, remote)
  65. self.assertEqual(r, v, msg='%s -> %s, expected %s' % (k, r, v))
  66. class RealGitTest(fake_repos.FakeReposTestBase):
  67. def setUp(self):
  68. super(RealGitTest, self).setUp()
  69. self.enabled = self.FAKE_REPOS.set_up_git()
  70. if self.enabled:
  71. self.clone_dir = scm.os.path.join(self.FAKE_REPOS.git_base, 'repo_1')
  72. def testIsValidRevision(self):
  73. if not self.enabled:
  74. return
  75. # Sha1's are [0-9a-z]{32}, so starting with a 'z' or 'r' should always fail.
  76. self.assertFalse(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev='zebra'))
  77. self.assertFalse(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev='r123456'))
  78. # Valid cases
  79. first_rev = self.githash('repo_1', 1)
  80. self.assertTrue(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev=first_rev))
  81. self.assertTrue(scm.GIT.IsValidRevision(cwd=self.clone_dir, rev='HEAD'))
  82. def testIsAncestor(self):
  83. if not self.enabled:
  84. return
  85. self.assertTrue(scm.GIT.IsAncestor(
  86. self.clone_dir, self.githash('repo_1', 1), self.githash('repo_1', 2)))
  87. self.assertFalse(scm.GIT.IsAncestor(
  88. self.clone_dir, self.githash('repo_1', 2), self.githash('repo_1', 1)))
  89. self.assertFalse(scm.GIT.IsAncestor(
  90. self.clone_dir, self.githash('repo_1', 1), 'zebra'))
  91. if __name__ == '__main__':
  92. if '-v' in sys.argv:
  93. logging.basicConfig(level=logging.DEBUG)
  94. unittest.main()
  95. # vim: ts=2:sw=2:tw=80:et: