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