__init__.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 unittest import SkipTest
  37. from unittest import TestCase as _TestCase # noqa: F401
  38. from unittest import expectedFailure, skipIf
  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 = [
  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("Unable to find binary %s" % 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 = ["dulwich.tests.test_" + name for name in names]
  136. loader = unittest.TestLoader()
  137. return loader.loadTestsFromNames(module_names)
  138. def tutorial_test_suite():
  139. import dulwich.client
  140. import dulwich.config
  141. import dulwich.index
  142. import dulwich.patch # noqa: F401
  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. def setup(test):
  153. test.__old_cwd = os.getcwd()
  154. test.tempdir = tempfile.mkdtemp()
  155. test.globs.update({"tempdir": test.tempdir})
  156. os.chdir(test.tempdir)
  157. def teardown(test):
  158. os.chdir(test.__old_cwd)
  159. shutil.rmtree(test.tempdir)
  160. return doctest.DocFileSuite(
  161. module_relative=True,
  162. package="dulwich.tests",
  163. setUp=setup,
  164. tearDown=teardown,
  165. *tutorial_files
  166. )
  167. def nocompat_test_suite():
  168. result = unittest.TestSuite()
  169. result.addTests(self_test_suite())
  170. result.addTests(tutorial_test_suite())
  171. from dulwich.contrib import test_suite as contrib_test_suite
  172. result.addTests(contrib_test_suite())
  173. return result
  174. def compat_test_suite():
  175. result = unittest.TestSuite()
  176. from dulwich.tests.compat import test_suite as compat_test_suite
  177. result.addTests(compat_test_suite())
  178. return result
  179. def test_suite():
  180. result = unittest.TestSuite()
  181. result.addTests(self_test_suite())
  182. if sys.platform != "win32":
  183. result.addTests(tutorial_test_suite())
  184. from dulwich.tests.compat import test_suite as compat_test_suite
  185. result.addTests(compat_test_suite())
  186. from dulwich.contrib import test_suite as contrib_test_suite
  187. result.addTests(contrib_test_suite())
  188. return result