__init__.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # __init__.py -- The tests for dulwich
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  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 published 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 Dulwich."""
  22. __all__ = [
  23. "BlackboxTestCase",
  24. "SkipTest",
  25. "TestCase",
  26. "expectedFailure",
  27. "skipIf",
  28. ]
  29. import doctest
  30. import logging
  31. import os
  32. import shutil
  33. import subprocess
  34. import sys
  35. import tempfile
  36. # If Python itself provides an exception, use that
  37. import unittest
  38. from typing import ClassVar, Optional
  39. from unittest import SkipTest, expectedFailure, skipIf
  40. from unittest import TestCase as _TestCase
  41. class TestCase(_TestCase):
  42. def setUp(self) -> None:
  43. super().setUp()
  44. self.overrideEnv("HOME", "/nonexistent")
  45. self.overrideEnv("GIT_CONFIG_NOSYSTEM", "1")
  46. def overrideEnv(self, name: str, value: Optional[str]) -> None:
  47. def restore() -> None:
  48. if oldval is not None:
  49. os.environ[name] = oldval
  50. elif name in os.environ:
  51. del os.environ[name]
  52. oldval = os.environ.get(name)
  53. if value is not None:
  54. os.environ[name] = value
  55. elif name in os.environ:
  56. del os.environ[name]
  57. self.addCleanup(restore)
  58. class BlackboxTestCase(TestCase):
  59. """Blackbox testing."""
  60. # TODO(jelmer): Include more possible binary paths.
  61. bin_directories: ClassVar[list[str]] = [
  62. os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "bin")),
  63. "/usr/bin",
  64. "/usr/local/bin",
  65. ]
  66. def bin_path(self, name: str) -> str:
  67. """Determine the full path of a binary.
  68. Args:
  69. name: Name of the script
  70. Returns: Full path
  71. """
  72. for d in self.bin_directories:
  73. p = os.path.join(d, name)
  74. if os.path.isfile(p):
  75. return p
  76. else:
  77. raise SkipTest(f"Unable to find binary {name}")
  78. def run_command(self, name: str, args: list[str]) -> subprocess.Popen[bytes]:
  79. """Run a Dulwich command.
  80. Args:
  81. name: Name of the command, as it exists in bin/
  82. args: Arguments to the command
  83. """
  84. env = dict(os.environ)
  85. env["PYTHONPATH"] = os.pathsep.join(sys.path)
  86. # Since they don't have any extensions, Windows can't recognize
  87. # executablility of the Python files in /bin. Even then, we'd have to
  88. # expect the user to set up file associations for .py files.
  89. #
  90. # Save us from all that headache and call python with the bin script.
  91. argv = [sys.executable, self.bin_path(name), *args]
  92. return subprocess.Popen(
  93. argv,
  94. stdout=subprocess.PIPE,
  95. stdin=subprocess.PIPE,
  96. stderr=subprocess.PIPE,
  97. env=env,
  98. )
  99. def self_test_suite() -> unittest.TestSuite:
  100. names = [
  101. "annotate",
  102. "archive",
  103. "attrs",
  104. "bisect",
  105. "blackbox",
  106. "bundle",
  107. "cli",
  108. "cli_cherry_pick",
  109. "cli_merge",
  110. "client",
  111. "cloud_gcs",
  112. "commit_graph",
  113. "config",
  114. "credentials",
  115. "diff",
  116. "diff_tree",
  117. "dumb",
  118. "fastexport",
  119. "file",
  120. "gc",
  121. "grafts",
  122. "graph",
  123. "greenthreads",
  124. "hooks",
  125. "ignore",
  126. "index",
  127. "lfs",
  128. "line_ending",
  129. "log_utils",
  130. "lru_cache",
  131. "mailmap",
  132. "merge",
  133. "merge_drivers",
  134. "missing_obj_finder",
  135. "notes",
  136. "objects",
  137. "objectspec",
  138. "object_store",
  139. "pack",
  140. "patch",
  141. "porcelain",
  142. "porcelain_cherry_pick",
  143. "porcelain_filters",
  144. "porcelain_lfs",
  145. "porcelain_merge",
  146. "porcelain_notes",
  147. "protocol",
  148. "rebase",
  149. "reflog",
  150. "refs",
  151. "reftable",
  152. "repository",
  153. "server",
  154. "sparse_patterns",
  155. "stash",
  156. "submodule",
  157. "utils",
  158. "walk",
  159. "web",
  160. "worktree",
  161. ]
  162. module_names = ["tests.test_" + name for name in names]
  163. loader = unittest.TestLoader()
  164. return loader.loadTestsFromNames(module_names)
  165. def tutorial_test_suite() -> unittest.TestSuite:
  166. tutorial = [
  167. "introduction",
  168. "file-format",
  169. "repo",
  170. "object-store",
  171. "remote",
  172. "conclusion",
  173. ]
  174. tutorial_files = [f"../docs/tutorial/{name}.txt" for name in tutorial]
  175. to_restore = []
  176. def overrideEnv(name: str, value: Optional[str]) -> None:
  177. oldval = os.environ.get(name)
  178. if value is not None:
  179. os.environ[name] = value
  180. else:
  181. del os.environ[name]
  182. to_restore.append((name, oldval))
  183. def setup(test: doctest.DocTest) -> None:
  184. test.__old_cwd = os.getcwd() # type: ignore[attr-defined]
  185. test.tempdir = tempfile.mkdtemp() # type: ignore[attr-defined]
  186. test.globs.update({"tempdir": test.tempdir}) # type: ignore[attr-defined]
  187. os.chdir(test.tempdir) # type: ignore[attr-defined]
  188. overrideEnv("HOME", "/nonexistent")
  189. overrideEnv("GIT_CONFIG_NOSYSTEM", "1")
  190. def teardown(test: doctest.DocTest) -> None:
  191. os.chdir(test.__old_cwd) # type: ignore[attr-defined]
  192. shutil.rmtree(test.tempdir) # type: ignore[attr-defined]
  193. for name, oldval in to_restore:
  194. if oldval is not None:
  195. os.environ[name] = oldval
  196. else:
  197. del os.environ[name]
  198. to_restore.clear()
  199. return doctest.DocFileSuite(
  200. module_relative=True,
  201. package="tests",
  202. setUp=setup,
  203. tearDown=teardown,
  204. *tutorial_files,
  205. )
  206. def nocompat_test_suite() -> unittest.TestSuite:
  207. result = unittest.TestSuite()
  208. result.addTests(self_test_suite())
  209. result.addTests(tutorial_test_suite())
  210. from .contrib import test_suite as contrib_test_suite
  211. result.addTests(contrib_test_suite())
  212. return result
  213. def compat_test_suite() -> unittest.TestSuite:
  214. result = unittest.TestSuite()
  215. from .compat import test_suite as compat_test_suite
  216. result.addTests(compat_test_suite())
  217. return result
  218. def test_suite() -> unittest.TestSuite:
  219. result = unittest.TestSuite()
  220. result.addTests(self_test_suite())
  221. if sys.platform != "win32":
  222. result.addTests(tutorial_test_suite())
  223. from .compat import test_suite as compat_test_suite
  224. result.addTests(compat_test_suite())
  225. from .contrib import test_suite as contrib_test_suite
  226. result.addTests(contrib_test_suite())
  227. return result