test_git_command.py 5.3 KB

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