2
0

test_utils.py 3.3 KB

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