test_grafts.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 tempfile
  22. import shutil
  23. from dulwich.errors import ObjectFormatException
  24. from dulwich.tests import TestCase
  25. from dulwich.objects import (
  26. Tree,
  27. )
  28. from dulwich.repo import (
  29. parse_graftpoints,
  30. serialize_graftpoints,
  31. MemoryRepo,
  32. Repo,
  33. )
  34. def makesha(digit):
  35. return (str(digit).encode("ascii") * 40)[:40]
  36. class GraftParserTests(TestCase):
  37. def assertParse(self, expected, graftpoints):
  38. self.assertEqual(expected, parse_graftpoints(iter(graftpoints)))
  39. def test_no_grafts(self):
  40. self.assertParse({}, [])
  41. def test_no_parents(self):
  42. self.assertParse({makesha(0): []}, [makesha(0)])
  43. def test_parents(self):
  44. self.assertParse(
  45. {makesha(0): [makesha(1), makesha(2)]},
  46. [b" ".join([makesha(0), makesha(1), makesha(2)])],
  47. )
  48. def test_multiple_hybrid(self):
  49. self.assertParse(
  50. {
  51. makesha(0): [],
  52. makesha(1): [makesha(2)],
  53. makesha(3): [makesha(4), makesha(5)],
  54. },
  55. [
  56. makesha(0),
  57. b" ".join([makesha(1), makesha(2)]),
  58. b" ".join([makesha(3), makesha(4), makesha(5)]),
  59. ],
  60. )
  61. class GraftSerializerTests(TestCase):
  62. def assertSerialize(self, expected, graftpoints):
  63. self.assertEqual(sorted(expected), sorted(serialize_graftpoints(graftpoints)))
  64. def test_no_grafts(self):
  65. self.assertSerialize(b"", {})
  66. def test_no_parents(self):
  67. self.assertSerialize(makesha(0), {makesha(0): []})
  68. def test_parents(self):
  69. self.assertSerialize(
  70. b" ".join([makesha(0), makesha(1), makesha(2)]),
  71. {makesha(0): [makesha(1), makesha(2)]},
  72. )
  73. def test_multiple_hybrid(self):
  74. self.assertSerialize(
  75. b"\n".join(
  76. [
  77. makesha(0),
  78. b" ".join([makesha(1), makesha(2)]),
  79. b" ".join([makesha(3), makesha(4), makesha(5)]),
  80. ]
  81. ),
  82. {
  83. makesha(0): [],
  84. makesha(1): [makesha(2)],
  85. makesha(3): [makesha(4), makesha(5)],
  86. },
  87. )
  88. class GraftsInRepositoryBase(object):
  89. def tearDown(self):
  90. super(GraftsInRepositoryBase, self).tearDown()
  91. def get_repo_with_grafts(self, grafts):
  92. r = self._repo
  93. r._add_graftpoints(grafts)
  94. return r
  95. def test_no_grafts(self):
  96. r = self.get_repo_with_grafts({})
  97. shas = [e.commit.id for e in r.get_walker()]
  98. self.assertEqual(shas, self._shas[::-1])
  99. def test_no_parents_graft(self):
  100. r = self.get_repo_with_grafts({self._repo.head(): []})
  101. self.assertEqual([e.commit.id for e in r.get_walker()], [r.head()])
  102. def test_existing_parent_graft(self):
  103. r = self.get_repo_with_grafts({self._shas[-1]: [self._shas[0]]})
  104. self.assertEqual(
  105. [e.commit.id for e in r.get_walker()], [self._shas[-1], self._shas[0]]
  106. )
  107. def test_remove_graft(self):
  108. r = self.get_repo_with_grafts({self._repo.head(): []})
  109. r._remove_graftpoints([self._repo.head()])
  110. self.assertEqual([e.commit.id for e in r.get_walker()], self._shas[::-1])
  111. def test_object_store_fail_invalid_parents(self):
  112. r = self._repo
  113. self.assertRaises(
  114. ObjectFormatException, r._add_graftpoints, {self._shas[-1]: ["1"]}
  115. )
  116. class GraftsInRepoTests(GraftsInRepositoryBase, TestCase):
  117. def setUp(self):
  118. super(GraftsInRepoTests, self).setUp()
  119. self._repo_dir = os.path.join(tempfile.mkdtemp())
  120. r = self._repo = Repo.init(self._repo_dir)
  121. self.addCleanup(shutil.rmtree, self._repo_dir)
  122. self._shas = []
  123. commit_kwargs = {
  124. "committer": b"Test Committer <test@nodomain.com>",
  125. "author": b"Test Author <test@nodomain.com>",
  126. "commit_timestamp": 12395,
  127. "commit_timezone": 0,
  128. "author_timestamp": 12395,
  129. "author_timezone": 0,
  130. }
  131. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))
  132. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))
  133. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))
  134. def test_init_with_empty_info_grafts(self):
  135. r = self._repo
  136. r._put_named_file(os.path.join("info", "grafts"), b"")
  137. r = Repo(self._repo_dir)
  138. self.assertEqual({}, r._graftpoints)
  139. def test_init_with_info_grafts(self):
  140. r = self._repo
  141. r._put_named_file(
  142. os.path.join("info", "grafts"), self._shas[-1] + b" " + self._shas[0]
  143. )
  144. r = Repo(self._repo_dir)
  145. self.assertEqual({self._shas[-1]: [self._shas[0]]}, r._graftpoints)
  146. class GraftsInMemoryRepoTests(GraftsInRepositoryBase, TestCase):
  147. def setUp(self):
  148. super(GraftsInMemoryRepoTests, self).setUp()
  149. r = self._repo = MemoryRepo()
  150. self._shas = []
  151. tree = Tree()
  152. commit_kwargs = {
  153. "committer": b"Test Committer <test@nodomain.com>",
  154. "author": b"Test Author <test@nodomain.com>",
  155. "commit_timestamp": 12395,
  156. "commit_timezone": 0,
  157. "author_timestamp": 12395,
  158. "author_timezone": 0,
  159. "tree": tree.id,
  160. }
  161. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))
  162. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))
  163. self._shas.append(r.do_commit(b"empty commit", **commit_kwargs))