utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. from dulwich.objects import (
  26. FixedSha,
  27. Commit,
  28. )
  29. from dulwich.repo import Repo
  30. def open_repo(name):
  31. """Open a copy of a repo in a temporary directory.
  32. Use this function for accessing repos in dulwich/tests/data/repos to avoid
  33. accidentally or intentionally modifying those repos in place. Use
  34. tear_down_repo to delete any temp files created.
  35. :param name: The name of the repository, relative to
  36. dulwich/tests/data/repos
  37. :returns: An initialized Repo object that lives in a temporary directory.
  38. """
  39. temp_dir = tempfile.mkdtemp()
  40. repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos', name)
  41. temp_repo_dir = os.path.join(temp_dir, name)
  42. shutil.copytree(repo_dir, temp_repo_dir, symlinks=True)
  43. return Repo(temp_repo_dir)
  44. def tear_down_repo(repo):
  45. """Tear down a test repository."""
  46. temp_dir = os.path.dirname(repo.path.rstrip(os.sep))
  47. shutil.rmtree(temp_dir)
  48. def make_object(cls, **attrs):
  49. """Make an object for testing and assign some members.
  50. This method creates a new subclass to allow arbitrary attribute
  51. reassignment, which is not otherwise possible with objects having __slots__.
  52. :param attrs: dict of attributes to set on the new object.
  53. :return: A newly initialized object of type cls.
  54. """
  55. class TestObject(cls):
  56. """Class that inherits from the given class, but without __slots__.
  57. Note that classes with __slots__ can't have arbitrary attributes monkey-
  58. patched in, so this is a class that is exactly the same only with a
  59. __dict__ instead of __slots__.
  60. """
  61. pass
  62. obj = TestObject()
  63. for name, value in attrs.iteritems():
  64. if name == 'id':
  65. # id property is read-only, so we overwrite sha instead.
  66. sha = FixedSha(value)
  67. obj.sha = lambda: sha
  68. else:
  69. setattr(obj, name, value)
  70. return obj
  71. def make_commit(**attrs):
  72. """Make a Commit object with a default set of members.
  73. :param attrs: dict of attributes to overwrite from the default values.
  74. :return: A newly initialized Commit object.
  75. """
  76. default_time = int(time.mktime(datetime.datetime(2010, 1, 1).timetuple()))
  77. all_attrs = {'author': 'Test Author <test@nodomain.com>',
  78. 'author_time': default_time,
  79. 'author_timezone': 0,
  80. 'committer': 'Test Committer <test@nodomain.com>',
  81. 'commit_time': default_time,
  82. 'commit_timezone': 0,
  83. 'message': 'Test message.',
  84. 'parents': [],
  85. 'tree': '0' * 40}
  86. all_attrs.update(attrs)
  87. return make_object(Commit, **all_attrs)