2
0

test_grafts.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # test_grafts.py -- Tests for graftpoints
  2. #
  3. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  4. # General Public License as public by the Free Software Foundation; version 2.0
  5. # or (at your option) any later version. You can redistribute it and/or
  6. # modify it under the terms of either of these two licenses.
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. #
  14. # You should have received a copy of the licenses; if not, see
  15. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  16. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  17. # License, Version 2.0.
  18. #
  19. """Tests for graftpoints."""
  20. import os
  21. import shutil
  22. import tempfile
  23. from dulwich.errors import ObjectFormatException
  24. from dulwich.objects import Tree
  25. from dulwich.repo import MemoryRepo, Repo, parse_graftpoints, serialize_graftpoints
  26. from . import TestCase
  27. def makesha(digit):
  28. return (str(digit).encode("ascii") * 40)[:40]
  29. class GraftParserTests(TestCase):
  30. def assertParse(self, expected, graftpoints):
  31. self.assertEqual(expected, parse_graftpoints(iter(graftpoints)))
  32. def test_no_grafts(self):
  33. self.assertParse({}, [])
  34. def test_no_parents(self):
  35. self.assertParse({makesha(0): []}, [makesha(0)])
  36. def test_parents(self):
  37. self.assertParse(
  38. {makesha(0): [makesha(1), makesha(2)]},
  39. [b" ".join([makesha(0), makesha(1), makesha(2)])],
  40. )
  41. def test_multiple_hybrid(self):
  42. self.assertParse(
  43. {
  44. makesha(0): [],
  45. makesha(1): [makesha(2)],
  46. makesha(3): [makesha(4), makesha(5)],
  47. },
  48. [
  49. makesha(0),
  50. b" ".join([makesha(1), makesha(2)]),
  51. b" ".join([makesha(3), makesha(4), makesha(5)]),
  52. ],
  53. )
  54. class GraftSerializerTests(TestCase):
  55. def assertSerialize(self, expected, graftpoints):
  56. self.assertEqual(sorted(expected), sorted(serialize_graftpoints(graftpoints)))
  57. def test_no_grafts(self):
  58. self.assertSerialize(b"", {})
  59. def test_no_parents(self):
  60. self.assertSerialize(makesha(0), {makesha(0): []})
  61. def test_parents(self):
  62. self.assertSerialize(
  63. b" ".join([makesha(0), makesha(1), makesha(2)]),
  64. {makesha(0): [makesha(1), makesha(2)]},
  65. )
  66. def test_multiple_hybrid(self):
  67. self.assertSerialize(
  68. b"\n".join(
  69. [
  70. makesha(0),
  71. b" ".join([makesha(1), makesha(2)]),
  72. b" ".join([makesha(3), makesha(4), makesha(5)]),
  73. ]
  74. ),
  75. {
  76. makesha(0): [],
  77. makesha(1): [makesha(2)],
  78. makesha(3): [makesha(4), makesha(5)],
  79. },
  80. )
  81. class GraftsInRepositoryBase:
  82. def tearDown(self):
  83. super().tearDown()
  84. def get_repo_with_grafts(self, grafts):
  85. r = self._repo
  86. r._add_graftpoints(grafts)
  87. return r
  88. def test_no_grafts(self):
  89. r = self.get_repo_with_grafts({})
  90. shas = [e.commit.id for e in r.get_walker()]
  91. self.assertEqual(shas, self._shas[::-1])
  92. def test_no_parents_graft(self):
  93. r = self.get_repo_with_grafts({self._repo.head(): []})
  94. self.assertEqual([e.commit.id for e in r.get_walker()], [r.head()])
  95. def test_existing_parent_graft(self):
  96. r = self.get_repo_with_grafts({self._shas[-1]: [self._shas[0]]})
  97. self.assertEqual(
  98. [e.commit.id for e in r.get_walker()],
  99. [self._shas[-1], self._shas[0]],
  100. )
  101. def test_remove_graft(self):
  102. r = self.get_repo_with_grafts({self._repo.head(): []})
  103. r._remove_graftpoints([self._repo.head()])
  104. self.assertEqual([e.commit.id for e in r.get_walker()], self._shas[::-1])
  105. def test_object_store_fail_invalid_parents(self):
  106. r = self._repo
  107. self.assertRaises(
  108. ObjectFormatException, r._add_graftpoints, {self._shas[-1]: ["1"]}
  109. )
  110. class GraftsInRepoTests(GraftsInRepositoryBase, TestCase):
  111. def setUp(self):
  112. super().setUp()
  113. self._repo_dir = os.path.join(tempfile.mkdtemp())
  114. r = self._repo = Repo.init(self._repo_dir)
  115. self.addCleanup(shutil.rmtree, self._repo_dir)
  116. self._shas = []
  117. commit_kwargs = {
  118. "committer": b"Test Committer <test@nodomain.com>",
  119. "author": b"Test Author <test@nodomain.com>",
  120. "commit_timestamp": 12395,
  121. "commit_timezone": 0,
  122. "author_timestamp": 12395,
  123. "author_timezone": 0,
  124. }
  125. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))
  126. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))
  127. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))
  128. def test_init_with_empty_info_grafts(self):
  129. r = self._repo
  130. r._put_named_file(os.path.join("info", "grafts"), b"")
  131. r = Repo(self._repo_dir)
  132. self.assertEqual({}, r._graftpoints)
  133. def test_init_with_info_grafts(self):
  134. r = self._repo
  135. r._put_named_file(
  136. os.path.join("info", "grafts"),
  137. self._shas[-1] + b" " + self._shas[0],
  138. )
  139. r = Repo(self._repo_dir)
  140. self.assertEqual({self._shas[-1]: [self._shas[0]]}, r._graftpoints)
  141. class GraftsInMemoryRepoTests(GraftsInRepositoryBase, TestCase):
  142. def setUp(self):
  143. super().setUp()
  144. r = self._repo = MemoryRepo()
  145. self._shas = []
  146. tree = Tree()
  147. commit_kwargs = {
  148. "committer": b"Test Committer <test@nodomain.com>",
  149. "author": b"Test Author <test@nodomain.com>",
  150. "commit_timestamp": 12395,
  151. "commit_timezone": 0,
  152. "author_timestamp": 12395,
  153. "author_timezone": 0,
  154. "tree": tree.id,
  155. }
  156. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))
  157. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))
  158. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))