__init__.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. "merge",
  122. "objects",
  123. "objectspec",
  124. "object_store",
  125. "missing_obj_finder",
  126. "pack",
  127. "patch",
  128. "porcelain",
  129. "protocol",
  130. "reflog",
  131. "refs",
  132. "repository",
  133. "server",
  134. "stash",
  135. "submodule",
  136. "utils",
  137. "walk",
  138. "web",
  139. ]
  140. module_names = ["tests.test_" + name for name in names]
  141. loader = unittest.TestLoader()
  142. return loader.loadTestsFromNames(module_names)
  143. def tutorial_test_suite():
  144. tutorial = [
  145. "introduction",
  146. "file-format",
  147. "repo",
  148. "object-store",
  149. "remote",
  150. "conclusion",
  151. ]
  152. tutorial_files = [f"../docs/tutorial/{name}.txt" for name in tutorial]
  153. to_restore = []
  154. def overrideEnv(name, value) -> None:
  155. oldval = os.environ.get(name)
  156. if value is not None:
  157. os.environ[name] = value
  158. else:
  159. del os.environ[name]
  160. to_restore.append((name, oldval))
  161. def setup(test) -> None:
  162. test.__old_cwd = os.getcwd()
  163. test.tempdir = tempfile.mkdtemp()
  164. test.globs.update({"tempdir": test.tempdir})
  165. os.chdir(test.tempdir)
  166. overrideEnv("HOME", "/nonexistent")
  167. overrideEnv("GIT_CONFIG_NOSYSTEM", "1")
  168. def teardown(test) -> None:
  169. os.chdir(test.__old_cwd)
  170. shutil.rmtree(test.tempdir)
  171. for name, oldval in to_restore:
  172. if oldval is not None:
  173. os.environ[name] = oldval
  174. else:
  175. del os.environ[name]
  176. to_restore.clear()
  177. return doctest.DocFileSuite(
  178. module_relative=True,
  179. package="tests",
  180. setUp=setup,
  181. tearDown=teardown,
  182. *tutorial_files,
  183. )
  184. def nocompat_test_suite():
  185. result = unittest.TestSuite()
  186. result.addTests(self_test_suite())
  187. result.addTests(tutorial_test_suite())
  188. from .contrib import test_suite as contrib_test_suite
  189. result.addTests(contrib_test_suite())
  190. return result
  191. def compat_test_suite():
  192. result = unittest.TestSuite()
  193. from .compat import test_suite as compat_test_suite
  194. result.addTests(compat_test_suite())
  195. return result
  196. def test_suite():
  197. result = unittest.TestSuite()
  198. result.addTests(self_test_suite())
  199. if sys.platform != "win32":
  200. result.addTests(tutorial_test_suite())
  201. from .compat import test_suite as compat_test_suite
  202. result.addTests(compat_test_suite())
  203. from .contrib import test_suite as contrib_test_suite
  204. result.addTests(contrib_test_suite())
  205. return result