test_grafts.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # test_grafts.py -- Tests for graftpoints
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # or (at your option) a later version of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. # MA 02110-1301, USA.
  17. """Tests for graftpoints."""
  18. import os
  19. import tempfile
  20. import shutil
  21. from dulwich.errors import ObjectFormatException
  22. from dulwich.tests import TestCase
  23. from dulwich.objects import (
  24. Tree,
  25. )
  26. from dulwich.repo import (
  27. parse_graftpoints,
  28. serialize_graftpoints,
  29. MemoryRepo,
  30. Repo,
  31. )
  32. def makesha(digit):
  33. return (str(digit).encode('ascii') * 40)[:40]
  34. class GraftParserTests(TestCase):
  35. def assertParse(self, expected, graftpoints):
  36. self.assertEqual(expected, parse_graftpoints(iter(graftpoints)))
  37. def test_no_grafts(self):
  38. self.assertParse({}, [])
  39. def test_no_parents(self):
  40. self.assertParse({makesha(0): []}, [makesha(0)])
  41. def test_parents(self):
  42. self.assertParse({makesha(0): [makesha(1), makesha(2)]},
  43. [b' '.join([makesha(0), makesha(1), makesha(2)])])
  44. def test_multiple_hybrid(self):
  45. self.assertParse(
  46. {makesha(0): [],
  47. makesha(1): [makesha(2)],
  48. makesha(3): [makesha(4), makesha(5)]},
  49. [makesha(0),
  50. b' '.join([makesha(1), makesha(2)]),
  51. b' '.join([makesha(3), makesha(4), makesha(5)])])
  52. class GraftSerializerTests(TestCase):
  53. def assertSerialize(self, expected, graftpoints):
  54. self.assertEqual(
  55. sorted(expected),
  56. 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(b' '.join([makesha(0), makesha(1), makesha(2)]),
  63. {makesha(0): [makesha(1), makesha(2)]})
  64. def test_multiple_hybrid(self):
  65. self.assertSerialize(
  66. b'\n'.join([
  67. makesha(0),
  68. b' '.join([makesha(1), makesha(2)]),
  69. b' '.join([makesha(3), makesha(4), makesha(5)])]),
  70. {makesha(0): [],
  71. makesha(1): [makesha(2)],
  72. makesha(3): [makesha(4), makesha(5)]})
  73. class GraftsInRepositoryBase(object):
  74. def tearDown(self):
  75. super(GraftsInRepositoryBase, self).tearDown()
  76. def get_repo_with_grafts(self, grafts):
  77. r = self._repo
  78. r._add_graftpoints(grafts)
  79. return r
  80. def test_no_grafts(self):
  81. r = self.get_repo_with_grafts({})
  82. shas = [e.commit.id for e in r.get_walker()]
  83. self.assertEqual(shas, self._shas[::-1])
  84. def test_no_parents_graft(self):
  85. r = self.get_repo_with_grafts({self._repo.head(): []})
  86. self.assertEqual([e.commit.id for e in r.get_walker()],
  87. [r.head()])
  88. def test_existing_parent_graft(self):
  89. r = self.get_repo_with_grafts({self._shas[-1]: [self._shas[0]]})
  90. self.assertEqual([e.commit.id for e in r.get_walker()],
  91. [self._shas[-1], self._shas[0]])
  92. def test_remove_graft(self):
  93. r = self.get_repo_with_grafts({self._repo.head(): []})
  94. r._remove_graftpoints([self._repo.head()])
  95. self.assertEqual([e.commit.id for e in r.get_walker()],
  96. self._shas[::-1])
  97. def test_object_store_fail_invalid_parents(self):
  98. r = self._repo
  99. self.assertRaises(
  100. ObjectFormatException,
  101. r._add_graftpoints,
  102. {self._shas[-1]: ['1']})
  103. class GraftsInRepoTests(GraftsInRepositoryBase, TestCase):
  104. def setUp(self):
  105. super(GraftsInRepoTests, self).setUp()
  106. self._repo_dir = os.path.join(tempfile.mkdtemp())
  107. r = self._repo = Repo.init(self._repo_dir)
  108. self.addCleanup(shutil.rmtree, self._repo_dir)
  109. self._shas = []
  110. commit_kwargs = {
  111. 'committer': b'Test Committer <test@nodomain.com>',
  112. 'author': b'Test Author <test@nodomain.com>',
  113. 'commit_timestamp': 12395,
  114. 'commit_timezone': 0,
  115. 'author_timestamp': 12395,
  116. 'author_timezone': 0,
  117. }
  118. self._shas.append(r.do_commit(
  119. b'empty commit', **commit_kwargs))
  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. def test_init_with_empty_info_grafts(self):
  125. r = self._repo
  126. r._put_named_file(os.path.join('info', 'grafts'), b'')
  127. r = Repo(self._repo_dir)
  128. self.assertEqual({}, r._graftpoints)
  129. def test_init_with_info_grafts(self):
  130. r = self._repo
  131. r._put_named_file(
  132. os.path.join('info', 'grafts'),
  133. self._shas[-1] + b' ' + self._shas[0])
  134. r = Repo(self._repo_dir)
  135. self.assertEqual({self._shas[-1]: [self._shas[0]]}, r._graftpoints)
  136. class GraftsInMemoryRepoTests(GraftsInRepositoryBase, TestCase):
  137. def setUp(self):
  138. super(GraftsInMemoryRepoTests, self).setUp()
  139. r = self._repo = MemoryRepo()
  140. self._shas = []
  141. tree = Tree()
  142. commit_kwargs = {
  143. 'committer': b'Test Committer <test@nodomain.com>',
  144. 'author': b'Test Author <test@nodomain.com>',
  145. 'commit_timestamp': 12395,
  146. 'commit_timezone': 0,
  147. 'author_timestamp': 12395,
  148. 'author_timezone': 0,
  149. 'tree': tree.id
  150. }
  151. self._shas.append(r.do_commit(
  152. b'empty commit', **commit_kwargs))
  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))