__init__.py 5.6 KB

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