test_git_command.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright 2019 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Unittests for the git_command.py module."""
  17. from __future__ import print_function
  18. import re
  19. import unittest
  20. try:
  21. from unittest import mock
  22. except ImportError:
  23. import mock
  24. import git_command
  25. import wrapper
  26. class SSHUnitTest(unittest.TestCase):
  27. """Tests the ssh functions."""
  28. def test_ssh_version(self):
  29. """Check ssh_version() handling."""
  30. ver = git_command._parse_ssh_version('Unknown\n')
  31. self.assertEqual(ver, ())
  32. ver = git_command._parse_ssh_version('OpenSSH_1.0\n')
  33. self.assertEqual(ver, (1, 0))
  34. ver = git_command._parse_ssh_version('OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.13, OpenSSL 1.0.1f 6 Jan 2014\n')
  35. self.assertEqual(ver, (6, 6, 1))
  36. ver = git_command._parse_ssh_version('OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n 7 Dec 2017\n')
  37. self.assertEqual(ver, (7, 6))
  38. def test_ssh_sock(self):
  39. """Check ssh_sock() function."""
  40. with mock.patch('tempfile.mkdtemp', return_value='/tmp/foo'):
  41. # old ssh version uses port
  42. with mock.patch('git_command.ssh_version', return_value=(6, 6)):
  43. self.assertTrue(git_command.ssh_sock().endswith('%p'))
  44. git_command._ssh_sock_path = None
  45. # new ssh version uses hash
  46. with mock.patch('git_command.ssh_version', return_value=(6, 7)):
  47. self.assertTrue(git_command.ssh_sock().endswith('%C'))
  48. git_command._ssh_sock_path = None
  49. class GitCallUnitTest(unittest.TestCase):
  50. """Tests the _GitCall class (via git_command.git)."""
  51. def test_version_tuple(self):
  52. """Check git.version_tuple() handling."""
  53. ver = git_command.git.version_tuple()
  54. self.assertIsNotNone(ver)
  55. # We don't dive too deep into the values here to avoid having to update
  56. # whenever git versions change. We do check relative to this min version
  57. # as this is what `repo` itself requires via MIN_GIT_VERSION.
  58. MIN_GIT_VERSION = (2, 10, 2)
  59. self.assertTrue(isinstance(ver.major, int))
  60. self.assertTrue(isinstance(ver.minor, int))
  61. self.assertTrue(isinstance(ver.micro, int))
  62. self.assertGreater(ver.major, MIN_GIT_VERSION[0] - 1)
  63. self.assertGreaterEqual(ver.micro, 0)
  64. self.assertGreaterEqual(ver.major, 0)
  65. self.assertGreaterEqual(ver, MIN_GIT_VERSION)
  66. self.assertLess(ver, (9999, 9999, 9999))
  67. self.assertNotEqual('', ver.full)
  68. class UserAgentUnitTest(unittest.TestCase):
  69. """Tests the UserAgent function."""
  70. def test_smoke_os(self):
  71. """Make sure UA OS setting returns something useful."""
  72. os_name = git_command.user_agent.os
  73. # We can't dive too deep because of OS/tool differences, but we can check
  74. # the general form.
  75. m = re.match(r'^[^ ]+$', os_name)
  76. self.assertIsNotNone(m)
  77. def test_smoke_repo(self):
  78. """Make sure repo UA returns something useful."""
  79. ua = git_command.user_agent.repo
  80. # We can't dive too deep because of OS/tool differences, but we can check
  81. # the general form.
  82. m = re.match(r'^git-repo/[^ ]+ ([^ ]+) git/[^ ]+ Python/[0-9.]+', ua)
  83. self.assertIsNotNone(m)
  84. def test_smoke_git(self):
  85. """Make sure git UA returns something useful."""
  86. ua = git_command.user_agent.git
  87. # We can't dive too deep because of OS/tool differences, but we can check
  88. # the general form.
  89. m = re.match(r'^git/[^ ]+ ([^ ]+) git-repo/[^ ]+', ua)
  90. self.assertIsNotNone(m)
  91. class GitRequireTests(unittest.TestCase):
  92. """Test the git_require helper."""
  93. def setUp(self):
  94. ver = wrapper.GitVersion(1, 2, 3, 4)
  95. mock.patch.object(git_command.git, 'version_tuple', return_value=ver).start()
  96. def tearDown(self):
  97. mock.patch.stopall()
  98. def test_older_nonfatal(self):
  99. """Test non-fatal require calls with old versions."""
  100. self.assertFalse(git_command.git_require((2,)))
  101. self.assertFalse(git_command.git_require((1, 3)))
  102. self.assertFalse(git_command.git_require((1, 2, 4)))
  103. self.assertFalse(git_command.git_require((1, 2, 3, 5)))
  104. def test_newer_nonfatal(self):
  105. """Test non-fatal require calls with newer versions."""
  106. self.assertTrue(git_command.git_require((0,)))
  107. self.assertTrue(git_command.git_require((1, 0)))
  108. self.assertTrue(git_command.git_require((1, 2, 0)))
  109. self.assertTrue(git_command.git_require((1, 2, 3, 0)))
  110. def test_equal_nonfatal(self):
  111. """Test require calls with equal values."""
  112. self.assertTrue(git_command.git_require((1, 2, 3, 4), fail=False))
  113. self.assertTrue(git_command.git_require((1, 2, 3, 4), fail=True))
  114. def test_older_fatal(self):
  115. """Test fatal require calls with old versions."""
  116. with self.assertRaises(SystemExit) as e:
  117. git_command.git_require((2,), fail=True)
  118. self.assertNotEqual(0, e.code)
  119. def test_older_fatal_msg(self):
  120. """Test fatal require calls with old versions and message."""
  121. with self.assertRaises(SystemExit) as e:
  122. git_command.git_require((2,), fail=True, msg='so sad')
  123. self.assertNotEqual(0, e.code)