2
0

utils.py 13 KB

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