utils.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. # utils.py -- Test utilities for Dulwich.
  2. # Copyright (C) 2010 Google, Inc.
  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. """Utility functions common to Dulwich tests."""
  21. import datetime
  22. import os
  23. import shutil
  24. import tempfile
  25. import time
  26. import types
  27. import warnings
  28. from dulwich.index import (
  29. commit_tree,
  30. )
  31. from dulwich.objects import (
  32. FixedSha,
  33. Commit,
  34. Tag,
  35. object_class,
  36. )
  37. from dulwich.pack import (
  38. OFS_DELTA,
  39. REF_DELTA,
  40. DELTA_TYPES,
  41. obj_sha,
  42. SHA1Writer,
  43. write_pack_header,
  44. write_pack_object,
  45. create_delta,
  46. )
  47. from dulwich.repo import Repo
  48. from dulwich.tests import (
  49. SkipTest,
  50. skipIf,
  51. )
  52. # Plain files are very frequently used in tests, so let the mode be very short.
  53. F = 0o100644 # Shorthand mode for Files.
  54. def open_repo(name, temp_dir=None):
  55. """Open a copy of a repo in a temporary directory.
  56. Use this function for accessing repos in dulwich/tests/data/repos to avoid
  57. accidentally or intentionally modifying those repos in place. Use
  58. tear_down_repo to delete any temp files created.
  59. :param name: The name of the repository, relative to
  60. dulwich/tests/data/repos
  61. :param temp_dir: temporary directory to initialize to. If not provided, a
  62. temporary directory will be created.
  63. :returns: An initialized Repo object that lives in a temporary directory.
  64. """
  65. if temp_dir is None:
  66. temp_dir = tempfile.mkdtemp()
  67. repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos', name)
  68. temp_repo_dir = os.path.join(temp_dir, name)
  69. shutil.copytree(repo_dir, temp_repo_dir, symlinks=True)
  70. return Repo(temp_repo_dir)
  71. def tear_down_repo(repo):
  72. """Tear down a test repository."""
  73. repo.close()
  74. temp_dir = os.path.dirname(repo.path.rstrip(os.sep))
  75. shutil.rmtree(temp_dir)
  76. def make_object(cls, **attrs):
  77. """Make an object for testing and assign some members.
  78. This method creates a new subclass to allow arbitrary attribute
  79. reassignment, which is not otherwise possible with objects having __slots__.
  80. :param attrs: dict of attributes to set on the new object.
  81. :return: A newly initialized object of type cls.
  82. """
  83. class TestObject(cls):
  84. """Class that inherits from the given class, but without __slots__.
  85. Note that classes with __slots__ can't have arbitrary attributes monkey-
  86. patched in, so this is a class that is exactly the same only with a
  87. __dict__ instead of __slots__.
  88. """
  89. pass
  90. TestObject.__name__ = 'TestObject_' + cls.__name__
  91. obj = TestObject()
  92. for name, value in attrs.items():
  93. if name == 'id':
  94. # id property is read-only, so we overwrite sha instead.
  95. sha = FixedSha(value)
  96. obj.sha = lambda: sha
  97. else:
  98. setattr(obj, name, value)
  99. return obj
  100. def make_commit(**attrs):
  101. """Make a Commit object with a default set of members.
  102. :param attrs: dict of attributes to overwrite from the default values.
  103. :return: A newly initialized Commit object.
  104. """
  105. default_time = int(time.mktime(datetime.datetime(2010, 1, 1).timetuple()))
  106. all_attrs = {'author': b'Test Author <test@nodomain.com>',
  107. 'author_time': default_time,
  108. 'author_timezone': 0,
  109. 'committer': b'Test Committer <test@nodomain.com>',
  110. 'commit_time': default_time,
  111. 'commit_timezone': 0,
  112. 'message': b'Test message.',
  113. 'parents': [],
  114. 'tree': b'0' * 40}
  115. all_attrs.update(attrs)
  116. return make_object(Commit, **all_attrs)
  117. def make_tag(target, **attrs):
  118. """Make a Tag object with a default set of values.
  119. :param target: object to be tagged (Commit, Blob, Tree, etc)
  120. :param attrs: dict of attributes to overwrite from the default values.
  121. :return: A newly initialized Tag object.
  122. """
  123. target_id = target.id
  124. target_type = object_class(target.type_name)
  125. default_time = int(time.mktime(datetime.datetime(2010, 1, 1).timetuple()))
  126. all_attrs = {'tagger': b'Test Author <test@nodomain.com>',
  127. 'tag_time': default_time,
  128. 'tag_timezone': 0,
  129. 'message': b'Test message.',
  130. 'object': (target_type, target_id),
  131. 'name': b'Test Tag',
  132. }
  133. all_attrs.update(attrs)
  134. return make_object(Tag, **all_attrs)
  135. def functest_builder(method, func):
  136. """Generate a test method that tests the given function."""
  137. def do_test(self):
  138. method(self, func)
  139. return do_test
  140. def ext_functest_builder(method, func):
  141. """Generate a test method that tests the given extension function.
  142. This is intended to generate test methods that test both a pure-Python
  143. version and an extension version using common test code. The extension test
  144. will raise SkipTest if the extension is not found.
  145. Sample usage:
  146. class MyTest(TestCase);
  147. def _do_some_test(self, func_impl):
  148. self.assertEqual('foo', func_impl())
  149. test_foo = functest_builder(_do_some_test, foo_py)
  150. test_foo_extension = ext_functest_builder(_do_some_test, _foo_c)
  151. :param method: The method to run. It must must two parameters, self and the
  152. function implementation to test.
  153. :param func: The function implementation to pass to method.
  154. """
  155. def do_test(self):
  156. if not isinstance(func, types.BuiltinFunctionType):
  157. raise SkipTest("%s extension not found" % func)
  158. method(self, func)
  159. return do_test
  160. def build_pack(f, objects_spec, store=None):
  161. """Write test pack data from a concise spec.
  162. :param f: A file-like object to write the pack to.
  163. :param objects_spec: A list of (type_num, obj). For non-delta types, obj
  164. is the string of that object's data.
  165. For delta types, obj is a tuple of (base, data), where:
  166. * base can be either an index in objects_spec of the base for that
  167. * delta; or for a ref delta, a SHA, in which case the resulting pack
  168. * will be thin and the base will be an external ref.
  169. * data is a string of the full, non-deltified data for that object.
  170. Note that offsets/refs and deltas are computed within this function.
  171. :param store: An optional ObjectStore for looking up external refs.
  172. :return: A list of tuples in the order specified by objects_spec:
  173. (offset, type num, data, sha, CRC32)
  174. """
  175. sf = SHA1Writer(f)
  176. num_objects = len(objects_spec)
  177. write_pack_header(sf, num_objects)
  178. full_objects = {}
  179. offsets = {}
  180. crc32s = {}
  181. while len(full_objects) < num_objects:
  182. for i, (type_num, data) in enumerate(objects_spec):
  183. if type_num not in DELTA_TYPES:
  184. full_objects[i] = (type_num, data,
  185. obj_sha(type_num, [data]))
  186. continue
  187. base, data = data
  188. if isinstance(base, int):
  189. if base not in full_objects:
  190. continue
  191. base_type_num, _, _ = full_objects[base]
  192. else:
  193. base_type_num, _ = store.get_raw(base)
  194. full_objects[i] = (base_type_num, data,
  195. obj_sha(base_type_num, [data]))
  196. for i, (type_num, obj) in enumerate(objects_spec):
  197. offset = f.tell()
  198. if type_num == OFS_DELTA:
  199. base_index, data = obj
  200. base = offset - offsets[base_index]
  201. _, base_data, _ = full_objects[base_index]
  202. obj = (base, create_delta(base_data, data))
  203. elif type_num == REF_DELTA:
  204. base_ref, data = obj
  205. if isinstance(base_ref, int):
  206. _, base_data, base = full_objects[base_ref]
  207. else:
  208. base_type_num, base_data = store.get_raw(base_ref)
  209. base = obj_sha(base_type_num, base_data)
  210. obj = (base, create_delta(base_data, data))
  211. crc32 = write_pack_object(sf, type_num, obj)
  212. offsets[i] = offset
  213. crc32s[i] = crc32
  214. expected = []
  215. for i in range(num_objects):
  216. type_num, data, sha = full_objects[i]
  217. assert len(sha) == 20
  218. expected.append((offsets[i], type_num, data, sha, crc32s[i]))
  219. sf.write_sha()
  220. f.seek(0)
  221. return expected
  222. def build_commit_graph(object_store, commit_spec, trees=None, attrs=None):
  223. """Build a commit graph from a concise specification.
  224. Sample usage:
  225. >>> c1, c2, c3 = build_commit_graph(store, [[1], [2, 1], [3, 1, 2]])
  226. >>> store[store[c3].parents[0]] == c1
  227. True
  228. >>> store[store[c3].parents[1]] == c2
  229. True
  230. If not otherwise specified, commits will refer to the empty tree and have
  231. commit times increasing in the same order as the commit spec.
  232. :param object_store: An ObjectStore to commit objects to.
  233. :param commit_spec: An iterable of iterables of ints defining the commit
  234. graph. Each entry defines one commit, and entries must be in topological
  235. order. The first element of each entry is a commit number, and the
  236. remaining elements are its parents. The commit numbers are only
  237. meaningful for the call to make_commits; since real commit objects are
  238. created, they will get created with real, opaque SHAs.
  239. :param trees: An optional dict of commit number -> tree spec for building
  240. trees for commits. The tree spec is an iterable of (path, blob, mode) or
  241. (path, blob) entries; if mode is omitted, it defaults to the normal file
  242. mode (0100644).
  243. :param attrs: A dict of commit number -> (dict of attribute -> value) for
  244. assigning additional values to the commits.
  245. :return: The list of commit objects created.
  246. :raise ValueError: If an undefined commit identifier is listed as a parent.
  247. """
  248. if trees is None:
  249. trees = {}
  250. if attrs is None:
  251. attrs = {}
  252. commit_time = 0
  253. nums = {}
  254. commits = []
  255. for commit in commit_spec:
  256. commit_num = commit[0]
  257. try:
  258. parent_ids = [nums[pn] for pn in commit[1:]]
  259. except KeyError as e:
  260. missing_parent, = e.args
  261. raise ValueError('Unknown parent %i' % missing_parent)
  262. blobs = []
  263. for entry in trees.get(commit_num, []):
  264. if len(entry) == 2:
  265. path, blob = entry
  266. entry = (path, blob, F)
  267. path, blob, mode = entry
  268. blobs.append((path, blob.id, mode))
  269. object_store.add_object(blob)
  270. tree_id = commit_tree(object_store, blobs)
  271. commit_attrs = {
  272. 'message': ('Commit %i' % commit_num).encode('ascii'),
  273. 'parents': parent_ids,
  274. 'tree': tree_id,
  275. 'commit_time': commit_time,
  276. }
  277. commit_attrs.update(attrs.get(commit_num, {}))
  278. commit_obj = make_commit(**commit_attrs)
  279. # By default, increment the time by a lot. Out-of-order commits should
  280. # be closer together than this because their main cause is clock skew.
  281. commit_time = commit_attrs['commit_time'] + 100
  282. nums[commit_num] = commit_obj.id
  283. object_store.add_object(commit_obj)
  284. commits.append(commit_obj)
  285. return commits
  286. def setup_warning_catcher():
  287. """Wrap warnings.showwarning with code that records warnings."""
  288. caught_warnings = []
  289. original_showwarning = warnings.showwarning
  290. def custom_showwarning(*args, **kwargs):
  291. caught_warnings.append(args[0])
  292. warnings.showwarning = custom_showwarning
  293. def restore_showwarning():
  294. warnings.showwarning = original_showwarning
  295. return caught_warnings, restore_showwarning