__init__.py 5.8 KB

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