test_bisect.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # test_bisect.py -- Tests for bisect functionality
  2. # Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
  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 public 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. """Tests for bisect functionality."""
  22. import os
  23. import shutil
  24. import tempfile
  25. from dulwich import porcelain
  26. from dulwich.bisect import BisectState
  27. from dulwich.objects import Tree
  28. from dulwich.tests.utils import make_commit
  29. from . import TestCase
  30. class BisectStateTests(TestCase):
  31. """Tests for BisectState class."""
  32. def setUp(self):
  33. self.test_dir = tempfile.mkdtemp()
  34. self.repo = porcelain.init(self.test_dir)
  35. def tearDown(self):
  36. shutil.rmtree(self.test_dir)
  37. def test_is_active_false(self):
  38. """Test is_active when no bisect session is active."""
  39. state = BisectState(self.repo)
  40. self.assertFalse(state.is_active)
  41. def test_start_bisect(self):
  42. """Test starting a bisect session."""
  43. # Create at least one commit so HEAD exists
  44. c1 = make_commit(id=b"1" * 40, message=b"initial commit")
  45. self.repo.object_store.add_object(c1)
  46. self.repo.refs[b"HEAD"] = c1.id
  47. self.repo.refs[b"refs/heads/main"] = c1.id
  48. state = BisectState(self.repo)
  49. state.start()
  50. self.assertTrue(state.is_active)
  51. self.assertTrue(
  52. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_START"))
  53. )
  54. self.assertTrue(
  55. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_TERMS"))
  56. )
  57. self.assertTrue(
  58. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_NAMES"))
  59. )
  60. self.assertTrue(
  61. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_LOG"))
  62. )
  63. def test_start_bisect_no_head(self):
  64. """Test starting a bisect session when repository has no HEAD."""
  65. state = BisectState(self.repo)
  66. with self.assertRaises(ValueError) as cm:
  67. state.start()
  68. self.assertIn("Cannot start bisect: repository has no HEAD", str(cm.exception))
  69. def test_start_bisect_already_active(self):
  70. """Test starting a bisect session when one is already active."""
  71. # Create at least one commit so HEAD exists
  72. c1 = make_commit(id=b"1" * 40, message=b"initial commit")
  73. self.repo.object_store.add_object(c1)
  74. self.repo.refs[b"HEAD"] = c1.id
  75. state = BisectState(self.repo)
  76. state.start()
  77. with self.assertRaises(ValueError):
  78. state.start()
  79. def test_mark_bad_no_session(self):
  80. """Test marking bad commit when no session is active."""
  81. state = BisectState(self.repo)
  82. with self.assertRaises(ValueError):
  83. state.mark_bad()
  84. def test_mark_good_no_session(self):
  85. """Test marking good commit when no session is active."""
  86. state = BisectState(self.repo)
  87. with self.assertRaises(ValueError):
  88. state.mark_good()
  89. def test_reset_no_session(self):
  90. """Test resetting when no session is active."""
  91. state = BisectState(self.repo)
  92. with self.assertRaises(ValueError):
  93. state.reset()
  94. def test_bisect_workflow(self):
  95. """Test a complete bisect workflow."""
  96. # Create some commits
  97. c1 = make_commit(id=b"1" * 40, message=b"good commit 1")
  98. c2 = make_commit(id=b"2" * 40, message=b"good commit 2", parents=[b"1" * 40])
  99. c3 = make_commit(id=b"3" * 40, message=b"bad commit", parents=[b"2" * 40])
  100. c4 = make_commit(id=b"4" * 40, message=b"bad commit 2", parents=[b"3" * 40])
  101. # Add commits to object store
  102. for commit in [c1, c2, c3, c4]:
  103. self.repo.object_store.add_object(commit)
  104. # Set HEAD to latest commit
  105. self.repo.refs[b"HEAD"] = c4.id
  106. # Start bisect
  107. state = BisectState(self.repo)
  108. state.start()
  109. # Mark bad and good
  110. state.mark_bad(c4.id)
  111. state.mark_good(c1.id)
  112. # Check that refs were created
  113. self.assertTrue(
  114. os.path.exists(
  115. os.path.join(self.repo.controldir(), "refs", "bisect", "bad")
  116. )
  117. )
  118. self.assertTrue(
  119. os.path.exists(
  120. os.path.join(
  121. self.repo.controldir(),
  122. "refs",
  123. "bisect",
  124. f"good-{c1.id.decode('ascii')}",
  125. )
  126. )
  127. )
  128. # Reset
  129. state.reset()
  130. self.assertFalse(state.is_active)
  131. class BisectPorcelainTests(TestCase):
  132. """Tests for porcelain bisect functions."""
  133. def setUp(self):
  134. self.test_dir = tempfile.mkdtemp()
  135. self.repo = porcelain.init(self.test_dir)
  136. # Create tree objects
  137. tree = Tree()
  138. self.repo.object_store.add_object(tree)
  139. # Create some commits with proper trees
  140. self.c1 = make_commit(id=b"1" * 40, message=b"initial commit", tree=tree.id)
  141. self.c2 = make_commit(
  142. id=b"2" * 40, message=b"second commit", parents=[b"1" * 40], tree=tree.id
  143. )
  144. self.c3 = make_commit(
  145. id=b"3" * 40, message=b"third commit", parents=[b"2" * 40], tree=tree.id
  146. )
  147. self.c4 = make_commit(
  148. id=b"4" * 40, message=b"fourth commit", parents=[b"3" * 40], tree=tree.id
  149. )
  150. # Add commits to object store
  151. for commit in [self.c1, self.c2, self.c3, self.c4]:
  152. self.repo.object_store.add_object(commit)
  153. # Set HEAD to latest commit
  154. self.repo.refs[b"HEAD"] = self.c4.id
  155. self.repo.refs[b"refs/heads/master"] = self.c4.id
  156. def tearDown(self):
  157. shutil.rmtree(self.test_dir)
  158. def test_bisect_start(self):
  159. """Test bisect_start porcelain function."""
  160. porcelain.bisect_start(self.test_dir)
  161. # Check that bisect state files exist
  162. self.assertTrue(
  163. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_START"))
  164. )
  165. def test_bisect_bad_good(self):
  166. """Test marking commits as bad and good."""
  167. porcelain.bisect_start(self.test_dir)
  168. porcelain.bisect_bad(self.test_dir, self.c4.id.decode("ascii"))
  169. porcelain.bisect_good(self.test_dir, self.c1.id.decode("ascii"))
  170. # Check that refs were created
  171. self.assertTrue(
  172. os.path.exists(
  173. os.path.join(self.repo.controldir(), "refs", "bisect", "bad")
  174. )
  175. )
  176. self.assertTrue(
  177. os.path.exists(
  178. os.path.join(
  179. self.repo.controldir(),
  180. "refs",
  181. "bisect",
  182. f"good-{self.c1.id.decode('ascii')}",
  183. )
  184. )
  185. )
  186. def test_bisect_log(self):
  187. """Test getting bisect log."""
  188. porcelain.bisect_start(self.test_dir)
  189. porcelain.bisect_bad(self.test_dir, self.c4.id.decode("ascii"))
  190. porcelain.bisect_good(self.test_dir, self.c1.id.decode("ascii"))
  191. log = porcelain.bisect_log(self.test_dir)
  192. self.assertIn("git bisect start", log)
  193. self.assertIn("git bisect bad", log)
  194. self.assertIn("git bisect good", log)
  195. def test_bisect_reset(self):
  196. """Test resetting bisect state."""
  197. porcelain.bisect_start(self.test_dir)
  198. porcelain.bisect_bad(self.test_dir)
  199. porcelain.bisect_good(self.test_dir, self.c1.id.decode("ascii"))
  200. porcelain.bisect_reset(self.test_dir)
  201. # Check that bisect state files are removed
  202. self.assertFalse(
  203. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_START"))
  204. )
  205. self.assertFalse(
  206. os.path.exists(os.path.join(self.repo.controldir(), "refs", "bisect"))
  207. )