__init__.py 5.7 KB

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