test_git_command.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 GitCallUnitTest(unittest.TestCase):
  27. """Tests the _GitCall class (via git_command.git)."""
  28. def test_version_tuple(self):
  29. """Check git.version_tuple() handling."""
  30. ver = git_command.git.version_tuple()
  31. self.assertIsNotNone(ver)
  32. # We don't dive too deep into the values here to avoid having to update
  33. # whenever git versions change. We do check relative to this min version
  34. # as this is what `repo` itself requires via MIN_GIT_VERSION.
  35. MIN_GIT_VERSION = (2, 10, 2)
  36. self.assertTrue(isinstance(ver.major, int))
  37. self.assertTrue(isinstance(ver.minor, int))
  38. self.assertTrue(isinstance(ver.micro, int))
  39. self.assertGreater(ver.major, MIN_GIT_VERSION[0] - 1)
  40. self.assertGreaterEqual(ver.micro, 0)
  41. self.assertGreaterEqual(ver.major, 0)
  42. self.assertGreaterEqual(ver, MIN_GIT_VERSION)
  43. self.assertLess(ver, (9999, 9999, 9999))
  44. self.assertNotEqual('', ver.full)
  45. class UserAgentUnitTest(unittest.TestCase):
  46. """Tests the UserAgent function."""
  47. def test_smoke_os(self):
  48. """Make sure UA OS setting returns something useful."""
  49. os_name = git_command.user_agent.os
  50. # We can't dive too deep because of OS/tool differences, but we can check
  51. # the general form.
  52. m = re.match(r'^[^ ]+$', os_name)
  53. self.assertIsNotNone(m)
  54. def test_smoke_repo(self):
  55. """Make sure repo UA returns something useful."""
  56. ua = git_command.user_agent.repo
  57. # We can't dive too deep because of OS/tool differences, but we can check
  58. # the general form.
  59. m = re.match(r'^git-repo/[^ ]+ ([^ ]+) git/[^ ]+ Python/[0-9.]+', ua)
  60. self.assertIsNotNone(m)
  61. def test_smoke_git(self):
  62. """Make sure git UA returns something useful."""
  63. ua = git_command.user_agent.git
  64. # We can't dive too deep because of OS/tool differences, but we can check
  65. # the general form.
  66. m = re.match(r'^git/[^ ]+ ([^ ]+) git-repo/[^ ]+', ua)
  67. self.assertIsNotNone(m)
  68. class GitRequireTests(unittest.TestCase):
  69. """Test the git_require helper."""
  70. def setUp(self):
  71. ver = wrapper.GitVersion(1, 2, 3, 4)
  72. mock.patch.object(git_command.git, 'version_tuple', return_value=ver).start()
  73. def tearDown(self):
  74. mock.patch.stopall()
  75. def test_older_nonfatal(self):
  76. """Test non-fatal require calls with old versions."""
  77. self.assertFalse(git_command.git_require((2,)))
  78. self.assertFalse(git_command.git_require((1, 3)))
  79. self.assertFalse(git_command.git_require((1, 2, 4)))
  80. self.assertFalse(git_command.git_require((1, 2, 3, 5)))
  81. def test_newer_nonfatal(self):
  82. """Test non-fatal require calls with newer versions."""
  83. self.assertTrue(git_command.git_require((0,)))
  84. self.assertTrue(git_command.git_require((1, 0)))
  85. self.assertTrue(git_command.git_require((1, 2, 0)))
  86. self.assertTrue(git_command.git_require((1, 2, 3, 0)))
  87. def test_equal_nonfatal(self):
  88. """Test require calls with equal values."""
  89. self.assertTrue(git_command.git_require((1, 2, 3, 4), fail=False))
  90. self.assertTrue(git_command.git_require((1, 2, 3, 4), fail=True))
  91. def test_older_fatal(self):
  92. """Test fatal require calls with old versions."""
  93. with self.assertRaises(SystemExit) as e:
  94. git_command.git_require((2,), fail=True)
  95. self.assertNotEqual(0, e.code)
  96. def test_older_fatal_msg(self):
  97. """Test fatal require calls with old versions and message."""
  98. with self.assertRaises(SystemExit) as e:
  99. git_command.git_require((2,), fail=True, msg='so sad')
  100. self.assertNotEqual(0, e.code)