scm_unittest.py 4.0 KB

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