test_utils.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # test_utils.py -- Tests for git compatibility utilities
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for git compatibility utilities."""
  22. from .. import SkipTest, TestCase
  23. from . import utils
  24. class GitVersionTests(TestCase):
  25. def setUp(self) -> None:
  26. super().setUp()
  27. self._orig_run_git = utils.run_git
  28. self._version_str = None # tests can override to set stub version
  29. def run_git(args, **unused_kwargs):
  30. self.assertEqual(["--version"], args)
  31. return 0, self._version_str, ""
  32. utils.run_git = run_git
  33. def tearDown(self) -> None:
  34. super().tearDown()
  35. utils.run_git = self._orig_run_git
  36. def test_git_version_none(self) -> None:
  37. self._version_str = b"not a git version"
  38. self.assertEqual(None, utils.git_version())
  39. def test_git_version_3(self) -> None:
  40. self._version_str = b"git version 1.6.6"
  41. self.assertEqual((1, 6, 6, 0), utils.git_version())
  42. def test_git_version_4(self) -> None:
  43. self._version_str = b"git version 1.7.0.2"
  44. self.assertEqual((1, 7, 0, 2), utils.git_version())
  45. def test_git_version_extra(self) -> None:
  46. self._version_str = b"git version 1.7.0.3.295.gd8fa2"
  47. self.assertEqual((1, 7, 0, 3), utils.git_version())
  48. def assertRequireSucceeds(self, required_version) -> None:
  49. try:
  50. utils.require_git_version(required_version)
  51. except SkipTest:
  52. self.fail()
  53. def assertRequireFails(self, required_version) -> None:
  54. self.assertRaises(SkipTest, utils.require_git_version, required_version)
  55. def test_require_git_version(self) -> None:
  56. try:
  57. self._version_str = b"git version 1.6.6"
  58. self.assertRequireSucceeds((1, 6, 6))
  59. self.assertRequireSucceeds((1, 6, 6, 0))
  60. self.assertRequireSucceeds((1, 6, 5))
  61. self.assertRequireSucceeds((1, 6, 5, 99))
  62. self.assertRequireFails((1, 7, 0))
  63. self.assertRequireFails((1, 7, 0, 2))
  64. self.assertRaises(ValueError, utils.require_git_version, (1, 6, 6, 0, 0))
  65. self._version_str = b"git version 1.7.0.2"
  66. self.assertRequireSucceeds((1, 6, 6))
  67. self.assertRequireSucceeds((1, 6, 6, 0))
  68. self.assertRequireSucceeds((1, 7, 0))
  69. self.assertRequireSucceeds((1, 7, 0, 2))
  70. self.assertRequireFails((1, 7, 0, 3))
  71. self.assertRequireFails((1, 7, 1))
  72. except SkipTest as e:
  73. # This test is designed to catch all SkipTest exceptions.
  74. self.fail(f"Test unexpectedly skipped: {e}")