You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.0KB

  1. # -*- coding: utf-8 -*-
  2. # test_exc.py
  3. # Copyright (C) 2008, 2009, 2016 Michael Trier (mtrier@gmail.com) and contributors
  4. #
  5. # This module is part of GitPython and is released under
  6. # the BSD License: http://www.opensource.org/licenses/bsd-license.php
  7. import re
  8. import ddt
  9. from git.exc import (
  10. InvalidGitRepositoryError,
  11. WorkTreeRepositoryUnsupported,
  12. NoSuchPathError,
  13. CommandError,
  14. GitCommandNotFound,
  15. GitCommandError,
  16. CheckoutError,
  17. CacheError,
  18. UnmergedEntriesError,
  19. HookExecutionError,
  20. RepositoryDirtyError,
  21. )
  22. from git.test.lib import TestBase
  23. import itertools as itt
  24. _cmd_argvs = (
  25. ('cmd', ),
  26. ('θνιψοδε', ),
  27. ('θνιψοδε', 'normal', 'argvs'),
  28. ('cmd', 'ελληνικα', 'args'),
  29. ('θνιψοδε', 'κι', 'αλλα', 'strange', 'args'),
  30. ('θνιψοδε', 'κι', 'αλλα', 'non-unicode', 'args'),
  31. )
  32. _causes_n_substrings = (
  33. (None, None), # noqa: E241 @IgnorePep8
  34. (7, "exit code(7)"), # noqa: E241 @IgnorePep8
  35. ('Some string', "'Some string'"), # noqa: E241 @IgnorePep8
  36. ('παλιο string', "'παλιο string'"), # noqa: E241 @IgnorePep8
  37. (Exception("An exc."), "Exception('An exc.')"), # noqa: E241 @IgnorePep8
  38. (Exception("Κακια exc."), "Exception('Κακια exc.')"), # noqa: E241 @IgnorePep8
  39. (object(), "<object object at "), # noqa: E241 @IgnorePep8
  40. )
  41. _streams_n_substrings = (None, 'steram', 'ομορφο stream', )
  42. @ddt.ddt
  43. class TExc(TestBase):
  44. def test_ExceptionsHaveBaseClass(self):
  45. from git.exc import GitError
  46. self.assertIsInstance(GitError(), Exception)
  47. exception_classes = [
  48. InvalidGitRepositoryError,
  49. WorkTreeRepositoryUnsupported,
  50. NoSuchPathError,
  51. CommandError,
  52. GitCommandNotFound,
  53. GitCommandError,
  54. CheckoutError,
  55. CacheError,
  56. UnmergedEntriesError,
  57. HookExecutionError,
  58. RepositoryDirtyError,
  59. ]
  60. for ex_class in exception_classes:
  61. self.assertTrue(issubclass(ex_class, GitError))
  62. @ddt.data(*list(itt.product(_cmd_argvs, _causes_n_substrings, _streams_n_substrings)))
  63. def test_CommandError_unicode(self, case):
  64. argv, (cause, subs), stream = case
  65. cls = CommandError
  66. c = cls(argv, cause)
  67. s = str(c)
  68. self.assertIsNotNone(c._msg)
  69. self.assertIn(' cmdline: ', s)
  70. for a in argv:
  71. self.assertIn(a, s)
  72. if not cause:
  73. self.assertIn("failed!", s)
  74. else:
  75. self.assertIn(" failed due to:", s)
  76. if subs is not None:
  77. # Substrings (must) already contain opening `'`.
  78. subs = "(?<!')%s(?!')" % re.escape(subs)
  79. self.assertRegexpMatches(s, subs)
  80. if not stream:
  81. c = cls(argv, cause)
  82. s = str(c)
  83. self.assertNotIn(" stdout:", s)
  84. self.assertNotIn(" stderr:", s)
  85. else:
  86. c = cls(argv, cause, stream)
  87. s = str(c)
  88. self.assertIn(" stderr:", s)
  89. self.assertIn(stream, s)
  90. c = cls(argv, cause, None, stream)
  91. s = str(c)
  92. self.assertIn(" stdout:", s)
  93. self.assertIn(stream, s)
  94. c = cls(argv, cause, stream, stream + 'no2')
  95. s = str(c)
  96. self.assertIn(" stderr:", s)
  97. self.assertIn(stream, s)
  98. self.assertIn(" stdout:", s)
  99. self.assertIn(stream + 'no2', s)
  100. @ddt.data(
  101. (['cmd1'], None),
  102. (['cmd1'], "some cause"),
  103. (['cmd1'], Exception()),
  104. )
  105. def test_GitCommandNotFound(self, init_args):
  106. argv, cause = init_args
  107. c = GitCommandNotFound(argv, cause)
  108. s = str(c)
  109. self.assertIn(argv[0], s)
  110. if cause:
  111. self.assertIn(' not found due to: ', s)
  112. self.assertIn(str(cause), s)
  113. else:
  114. self.assertIn(' not found!', s)
  115. @ddt.data(
  116. (['cmd1'], None),
  117. (['cmd1'], "some cause"),
  118. (['cmd1'], Exception()),
  119. )
  120. def test_GitCommandError(self, init_args):
  121. argv, cause = init_args
  122. c = GitCommandError(argv, cause)
  123. s = str(c)
  124. self.assertIn(argv[0], s)
  125. if cause:
  126. self.assertIn(' failed due to: ', s)
  127. self.assertIn(str(cause), s)
  128. else:
  129. self.assertIn(' failed!', s)
  130. @ddt.data(
  131. (['cmd1'], None),
  132. (['cmd1'], "some cause"),
  133. (['cmd1'], Exception()),
  134. )
  135. def test_HookExecutionError(self, init_args):
  136. argv, cause = init_args
  137. c = HookExecutionError(argv, cause)
  138. s = str(c)
  139. self.assertIn(argv[0], s)
  140. if cause:
  141. self.assertTrue(s.startswith('Hook('), s)
  142. self.assertIn(str(cause), s)
  143. else:
  144. self.assertIn(' failed!', s)