__init__.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # __init__.py -- The tests for dulwich
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for Dulwich."""
  21. __all__ = [
  22. "SkipTest",
  23. "TestCase",
  24. "BlackboxTestCase",
  25. "skipIf",
  26. "expectedFailure",
  27. ]
  28. import doctest
  29. import os
  30. import shutil
  31. import subprocess
  32. import sys
  33. import tempfile
  34. # If Python itself provides an exception, use that
  35. import unittest
  36. from typing import ClassVar, List
  37. from unittest import SkipTest, expectedFailure, skipIf
  38. from unittest import TestCase as _TestCase
  39. class TestCase(_TestCase):
  40. def setUp(self):
  41. super().setUp()
  42. self.overrideEnv("HOME", "/nonexistent")
  43. self.overrideEnv("GIT_CONFIG_NOSYSTEM", "1")
  44. def overrideEnv(self, name, value):
  45. def restore():
  46. if oldval is not None:
  47. os.environ[name] = oldval
  48. else:
  49. del os.environ[name]
  50. oldval = os.environ.get(name)
  51. if value is not None:
  52. os.environ[name] = value
  53. else:
  54. del os.environ[name]
  55. self.addCleanup(restore)
  56. class BlackboxTestCase(TestCase):
  57. """Blackbox testing."""
  58. # TODO(jelmer): Include more possible binary paths.
  59. bin_directories: ClassVar[List[str]] = [
  60. os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "bin")),
  61. "/usr/bin",
  62. "/usr/local/bin",
  63. ]
  64. def bin_path(self, name):
  65. """Determine the full path of a binary.
  66. Args:
  67. name: Name of the script
  68. Returns: Full path
  69. """
  70. for d in self.bin_directories:
  71. p = os.path.join(d, name)
  72. if os.path.isfile(p):
  73. return p
  74. else:
  75. raise SkipTest(f"Unable to find binary {name}")
  76. def run_command(self, name, args):
  77. """Run a Dulwich command.
  78. Args:
  79. name: Name of the command, as it exists in bin/
  80. args: Arguments to the command
  81. """
  82. env = dict(os.environ)
  83. env["PYTHONPATH"] = os.pathsep.join(sys.path)
  84. # Since they don't have any extensions, Windows can't recognize
  85. # executablility of the Python files in /bin. Even then, we'd have to
  86. # expect the user to set up file associations for .py files.
  87. #
  88. # Save us from all that headache and call python with the bin script.
  89. argv = [sys.executable, self.bin_path(name), *args]
  90. return subprocess.Popen(
  91. argv,
  92. stdout=subprocess.PIPE,
  93. stdin=subprocess.PIPE,
  94. stderr=subprocess.PIPE,
  95. env=env,
  96. )
  97. def self_test_suite():
  98. names = [
  99. "archive",
  100. "blackbox",
  101. "bundle",
  102. "client",
  103. "config",
  104. "credentials",
  105. "diff_tree",
  106. "fastexport",
  107. "file",
  108. "grafts",
  109. "graph",
  110. "greenthreads",
  111. "hooks",
  112. "ignore",
  113. "index",
  114. "lfs",
  115. "line_ending",
  116. "lru_cache",
  117. "mailmap",
  118. "objects",
  119. "objectspec",
  120. "object_store",
  121. "missing_obj_finder",
  122. "pack",
  123. "patch",
  124. "porcelain",
  125. "protocol",
  126. "reflog",
  127. "refs",
  128. "repository",
  129. "server",
  130. "stash",
  131. "utils",
  132. "walk",
  133. "web",
  134. ]
  135. module_names = ["tests.test_" + name for name in names]
  136. loader = unittest.TestLoader()
  137. return loader.loadTestsFromNames(module_names)
  138. def tutorial_test_suite():
  139. tutorial = [
  140. "introduction",
  141. "file-format",
  142. "repo",
  143. "object-store",
  144. "remote",
  145. "conclusion",
  146. ]
  147. tutorial_files = [f"../docs/tutorial/{name}.txt" for name in tutorial]
  148. to_restore = []
  149. def overrideEnv(name, value):
  150. oldval = os.environ.get(name)
  151. if value is not None:
  152. os.environ[name] = value
  153. else:
  154. del os.environ[name]
  155. to_restore.append((name, oldval))
  156. def setup(test):
  157. test.__old_cwd = os.getcwd()
  158. test.tempdir = tempfile.mkdtemp()
  159. test.globs.update({"tempdir": test.tempdir})
  160. os.chdir(test.tempdir)
  161. overrideEnv("HOME", "/nonexistent")
  162. overrideEnv("GIT_CONFIG_NOSYSTEM", "1")
  163. def teardown(test):
  164. os.chdir(test.__old_cwd)
  165. shutil.rmtree(test.tempdir)
  166. for name, oldval in to_restore:
  167. if oldval is not None:
  168. os.environ[name] = oldval
  169. else:
  170. del os.environ[name]
  171. to_restore.clear()
  172. return doctest.DocFileSuite(
  173. module_relative=True,
  174. package="tests",
  175. setUp=setup,
  176. tearDown=teardown,
  177. *tutorial_files,
  178. )
  179. def nocompat_test_suite():
  180. result = unittest.TestSuite()
  181. result.addTests(self_test_suite())
  182. result.addTests(tutorial_test_suite())
  183. from dulwich.contrib import test_suite as contrib_test_suite
  184. result.addTests(contrib_test_suite())
  185. return result
  186. def compat_test_suite():
  187. result = unittest.TestSuite()
  188. from .compat import test_suite as compat_test_suite
  189. result.addTests(compat_test_suite())
  190. return result
  191. def test_suite():
  192. result = unittest.TestSuite()
  193. result.addTests(self_test_suite())
  194. if sys.platform != "win32":
  195. result.addTests(tutorial_test_suite())
  196. from .compat import test_suite as compat_test_suite
  197. result.addTests(compat_test_suite())
  198. from .contrib import test_suite as contrib_test_suite
  199. result.addTests(contrib_test_suite())
  200. return result