2
0

__init__.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. "BlackboxTestCase",
  24. "SkipTest",
  25. "TestCase",
  26. "expectedFailure",
  27. "skipIf",
  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. elif name in os.environ:
  50. del os.environ[name]
  51. oldval = os.environ.get(name)
  52. if value is not None:
  53. os.environ[name] = value
  54. elif name in os.environ:
  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. "cloud_gcs",
  105. "config",
  106. "credentials",
  107. "diff_tree",
  108. "fastexport",
  109. "file",
  110. "grafts",
  111. "graph",
  112. "greenthreads",
  113. "hooks",
  114. "ignore",
  115. "index",
  116. "lfs",
  117. "line_ending",
  118. "log_utils",
  119. "lru_cache",
  120. "mailmap",
  121. "objects",
  122. "objectspec",
  123. "object_store",
  124. "missing_obj_finder",
  125. "pack",
  126. "patch",
  127. "porcelain",
  128. "protocol",
  129. "reflog",
  130. "refs",
  131. "repository",
  132. "server",
  133. "stash",
  134. "submodule",
  135. "utils",
  136. "walk",
  137. "web",
  138. ]
  139. module_names = ["tests.test_" + name for name in names]
  140. loader = unittest.TestLoader()
  141. return loader.loadTestsFromNames(module_names)
  142. def tutorial_test_suite():
  143. tutorial = [
  144. "introduction",
  145. "file-format",
  146. "repo",
  147. "object-store",
  148. "remote",
  149. "conclusion",
  150. ]
  151. tutorial_files = [f"../docs/tutorial/{name}.txt" for name in tutorial]
  152. to_restore = []
  153. def overrideEnv(name, value) -> None:
  154. oldval = os.environ.get(name)
  155. if value is not None:
  156. os.environ[name] = value
  157. else:
  158. del os.environ[name]
  159. to_restore.append((name, oldval))
  160. def setup(test) -> None:
  161. test.__old_cwd = os.getcwd()
  162. test.tempdir = tempfile.mkdtemp()
  163. test.globs.update({"tempdir": test.tempdir})
  164. os.chdir(test.tempdir)
  165. overrideEnv("HOME", "/nonexistent")
  166. overrideEnv("GIT_CONFIG_NOSYSTEM", "1")
  167. def teardown(test) -> None:
  168. os.chdir(test.__old_cwd)
  169. shutil.rmtree(test.tempdir)
  170. for name, oldval in to_restore:
  171. if oldval is not None:
  172. os.environ[name] = oldval
  173. else:
  174. del os.environ[name]
  175. to_restore.clear()
  176. return doctest.DocFileSuite(
  177. module_relative=True,
  178. package="tests",
  179. setUp=setup,
  180. tearDown=teardown,
  181. *tutorial_files,
  182. )
  183. def nocompat_test_suite():
  184. result = unittest.TestSuite()
  185. result.addTests(self_test_suite())
  186. result.addTests(tutorial_test_suite())
  187. from .contrib import test_suite as contrib_test_suite
  188. result.addTests(contrib_test_suite())
  189. return result
  190. def compat_test_suite():
  191. result = unittest.TestSuite()
  192. from .compat import test_suite as compat_test_suite
  193. result.addTests(compat_test_suite())
  194. return result
  195. def test_suite():
  196. result = unittest.TestSuite()
  197. result.addTests(self_test_suite())
  198. if sys.platform != "win32":
  199. result.addTests(tutorial_test_suite())
  200. from .compat import test_suite as compat_test_suite
  201. result.addTests(compat_test_suite())
  202. from .contrib import test_suite as contrib_test_suite
  203. result.addTests(contrib_test_suite())
  204. return result