utils.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # utils.py -- Test utilities for Dulwich.
  2. # Copyright (C) 2010 Google, Inc.
  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. """Utility functions common to Dulwich tests."""
  20. import datetime
  21. import os
  22. import shutil
  23. import tempfile
  24. import time
  25. import types
  26. from dulwich.objects import (
  27. FixedSha,
  28. Commit,
  29. )
  30. from dulwich.repo import Repo
  31. from dulwich.tests import (
  32. TestSkipped,
  33. )
  34. def open_repo(name):
  35. """Open a copy of a repo in a temporary directory.
  36. Use this function for accessing repos in dulwich/tests/data/repos to avoid
  37. accidentally or intentionally modifying those repos in place. Use
  38. tear_down_repo to delete any temp files created.
  39. :param name: The name of the repository, relative to
  40. dulwich/tests/data/repos
  41. :returns: An initialized Repo object that lives in a temporary directory.
  42. """
  43. temp_dir = tempfile.mkdtemp()
  44. repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos', name)
  45. temp_repo_dir = os.path.join(temp_dir, name)
  46. shutil.copytree(repo_dir, temp_repo_dir, symlinks=True)
  47. return Repo(temp_repo_dir)
  48. def tear_down_repo(repo):
  49. """Tear down a test repository."""
  50. temp_dir = os.path.dirname(repo.path.rstrip(os.sep))
  51. shutil.rmtree(temp_dir)
  52. def make_object(cls, **attrs):
  53. """Make an object for testing and assign some members.
  54. This method creates a new subclass to allow arbitrary attribute
  55. reassignment, which is not otherwise possible with objects having __slots__.
  56. :param attrs: dict of attributes to set on the new object.
  57. :return: A newly initialized object of type cls.
  58. """
  59. class TestObject(cls):
  60. """Class that inherits from the given class, but without __slots__.
  61. Note that classes with __slots__ can't have arbitrary attributes monkey-
  62. patched in, so this is a class that is exactly the same only with a
  63. __dict__ instead of __slots__.
  64. """
  65. pass
  66. obj = TestObject()
  67. for name, value in attrs.iteritems():
  68. if name == 'id':
  69. # id property is read-only, so we overwrite sha instead.
  70. sha = FixedSha(value)
  71. obj.sha = lambda: sha
  72. else:
  73. setattr(obj, name, value)
  74. return obj
  75. def make_commit(**attrs):
  76. """Make a Commit object with a default set of members.
  77. :param attrs: dict of attributes to overwrite from the default values.
  78. :return: A newly initialized Commit object.
  79. """
  80. default_time = int(time.mktime(datetime.datetime(2010, 1, 1).timetuple()))
  81. all_attrs = {'author': 'Test Author <test@nodomain.com>',
  82. 'author_time': default_time,
  83. 'author_timezone': 0,
  84. 'committer': 'Test Committer <test@nodomain.com>',
  85. 'commit_time': default_time,
  86. 'commit_timezone': 0,
  87. 'message': 'Test message.',
  88. 'parents': [],
  89. 'tree': '0' * 40}
  90. all_attrs.update(attrs)
  91. return make_object(Commit, **all_attrs)
  92. def functest_builder(method, func):
  93. """Generate a test method that tests the given function."""
  94. def do_test(self):
  95. method(self, func)
  96. return do_test
  97. def ext_functest_builder(method, func):
  98. """Generate a test method that tests the given extension function.
  99. This is intended to generate test methods that test both a pure-Python
  100. version and an extension version using common test code. The extension test
  101. will raise TestSkipped if the extension is not found.
  102. Sample usage:
  103. class MyTest(TestCase);
  104. def _do_some_test(self, func_impl):
  105. self.assertEqual('foo', func_impl())
  106. test_foo = functest_builder(_do_some_test, foo_py)
  107. test_foo_extension = ext_functest_builder(_do_some_test, _foo_c)
  108. :param method: The method to run. It must must two parameters, self and the
  109. function implementation to test.
  110. :param func: The function implementation to pass to method.
  111. """
  112. def do_test(self):
  113. if not isinstance(func, types.BuiltinFunctionType):
  114. raise TestSkipped("%s extension not found", func.func_name)
  115. method(self, func)
  116. return do_test