__init__.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # __init__.py -- The tests for dulwich
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) any later version of
  8. # the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Tests for Dulwich."""
  20. import doctest
  21. import os
  22. import shutil
  23. import subprocess
  24. import sys
  25. import tempfile
  26. # If Python itself provides an exception, use that
  27. import unittest
  28. if sys.version_info < (2, 7):
  29. from unittest2 import SkipTest, TestCase as _TestCase, skipIf
  30. else:
  31. from unittest import SkipTest, TestCase as _TestCase, skipIf
  32. def get_safe_env(env=None):
  33. """Returns the environment "env" (or a copy of "os.environ" by default)
  34. modified to avoid side-effects caused by user's ~/.gitconfig"""
  35. if env is None:
  36. env = os.environ.copy()
  37. # On Windows it's not enough to set "HOME" to a non-existing
  38. # directory. Git.cmd takes the first existing directory out of
  39. # "%HOME%", "%HOMEDRIVE%%HOMEPATH%" and "%USERPROFILE%".
  40. for e in 'HOME', 'HOMEPATH', 'USERPROFILE':
  41. env[e] = '/nosuchdir'
  42. return env
  43. class TestCase(_TestCase):
  44. def makeSafeEnv(self):
  45. """Create environment with homedirectory-related variables stripped.
  46. Modifies os.environ for the duration of a test case to avoid
  47. side-effects caused by the user's ~/.gitconfig and other
  48. files in their home directory.
  49. """
  50. old_env = os.environ
  51. def restore():
  52. os.environ = old_env
  53. self.addCleanup(restore)
  54. new_env = dict(os.environ)
  55. for e in ['HOME', 'HOMEPATH', 'USERPROFILE']:
  56. new_env[e] = '/nosuchdir'
  57. os.environ = new_env
  58. def setUp(self):
  59. super(TestCase, self).setUp()
  60. self.makeSafeEnv()
  61. class BlackboxTestCase(TestCase):
  62. """Blackbox testing."""
  63. # TODO(jelmer): Include more possible binary paths.
  64. bin_directories = [os.path.abspath(os.path.join(os.path.dirname(__file__),
  65. "..", "..", "bin")), '/usr/bin', '/usr/local/bin']
  66. def bin_path(self, name):
  67. """Determine the full path of a binary.
  68. :param name: Name of the script
  69. :return: Full path
  70. """
  71. for d in self.bin_directories:
  72. p = os.path.join(d, name)
  73. if os.path.isfile(p):
  74. return p
  75. else:
  76. raise SkipTest("Unable to find binary %s" % name)
  77. def run_command(self, name, args):
  78. """Run a Dulwich command.
  79. :param name: Name of the command, as it exists in bin/
  80. :param 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(argv,
  91. stdout=subprocess.PIPE,
  92. stdin=subprocess.PIPE, stderr=subprocess.PIPE,
  93. env=env)
  94. def self_test_suite():
  95. names = [
  96. 'blackbox',
  97. 'client',
  98. 'config',
  99. 'diff_tree',
  100. 'fastexport',
  101. 'file',
  102. 'grafts',
  103. 'greenthreads',
  104. 'hooks',
  105. 'index',
  106. 'lru_cache',
  107. 'objects',
  108. 'objectspec',
  109. 'object_store',
  110. 'missing_obj_finder',
  111. 'pack',
  112. 'patch',
  113. 'porcelain',
  114. 'protocol',
  115. 'refs',
  116. 'repository',
  117. 'server',
  118. 'walk',
  119. 'web',
  120. ]
  121. module_names = ['dulwich.tests.test_' + name for name in names]
  122. loader = unittest.TestLoader()
  123. return loader.loadTestsFromNames(module_names)
  124. def tutorial_test_suite():
  125. tutorial = [
  126. 'introduction',
  127. 'file-format',
  128. 'repo',
  129. 'object-store',
  130. 'remote',
  131. 'conclusion',
  132. ]
  133. tutorial_files = ["../../docs/tutorial/%s.txt" % name for name in tutorial]
  134. def setup(test):
  135. test.__old_cwd = os.getcwd()
  136. test.__dulwich_tempdir = tempfile.mkdtemp()
  137. os.chdir(test.__dulwich_tempdir)
  138. def teardown(test):
  139. os.chdir(test.__old_cwd)
  140. shutil.rmtree(test.__dulwich_tempdir)
  141. return doctest.DocFileSuite(setUp=setup, tearDown=teardown,
  142. *tutorial_files)
  143. def nocompat_test_suite():
  144. result = unittest.TestSuite()
  145. result.addTests(self_test_suite())
  146. from dulwich.contrib import test_suite as contrib_test_suite
  147. result.addTests(tutorial_test_suite())
  148. result.addTests(contrib_test_suite())
  149. return result
  150. def compat_test_suite():
  151. result = unittest.TestSuite()
  152. from dulwich.tests.compat import test_suite as compat_test_suite
  153. result.addTests(compat_test_suite())
  154. return result
  155. def test_suite():
  156. result = unittest.TestSuite()
  157. result.addTests(self_test_suite())
  158. if sys.version_info[0] == 2:
  159. result.addTests(tutorial_test_suite())
  160. from dulwich.tests.compat import test_suite as compat_test_suite
  161. result.addTests(compat_test_suite())
  162. from dulwich.contrib import test_suite as contrib_test_suite
  163. result.addTests(contrib_test_suite())
  164. return result