2
0

__init__.py 6.3 KB

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