test_bisect.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 published 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.bisect import BisectState
  26. from dulwich.repo import Repo
  27. from dulwich.tests.utils import make_commit
  28. from . import TestCase
  29. class BisectStateTests(TestCase):
  30. """Tests for BisectState class."""
  31. def setUp(self) -> None:
  32. self.test_dir = tempfile.mkdtemp()
  33. self.repo = Repo.init(self.test_dir)
  34. def tearDown(self) -> None:
  35. shutil.rmtree(self.test_dir)
  36. def test_is_active_false(self) -> None:
  37. """Test is_active when no bisect session is active."""
  38. state = BisectState(self.repo)
  39. self.assertFalse(state.is_active)
  40. def test_start_bisect(self) -> None:
  41. """Test starting a bisect session."""
  42. # Create at least one commit so HEAD exists
  43. c1 = make_commit(id=b"1" * 40, message=b"initial commit")
  44. self.repo.object_store.add_object(c1)
  45. self.repo.refs[b"HEAD"] = c1.id
  46. self.repo.refs[b"refs/heads/main"] = c1.id
  47. state = BisectState(self.repo)
  48. state.start()
  49. self.assertTrue(state.is_active)
  50. self.assertTrue(
  51. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_START"))
  52. )
  53. self.assertTrue(
  54. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_TERMS"))
  55. )
  56. self.assertTrue(
  57. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_NAMES"))
  58. )
  59. self.assertTrue(
  60. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_LOG"))
  61. )
  62. def test_start_bisect_no_head(self) -> None:
  63. """Test starting a bisect session when repository has no HEAD."""
  64. state = BisectState(self.repo)
  65. with self.assertRaises(ValueError) as cm:
  66. state.start()
  67. self.assertIn("Cannot start bisect: repository has no HEAD", str(cm.exception))
  68. def test_start_bisect_already_active(self) -> None:
  69. """Test starting a bisect session when one is already active."""
  70. # Create at least one commit so HEAD exists
  71. c1 = make_commit(id=b"1" * 40, message=b"initial commit")
  72. self.repo.object_store.add_object(c1)
  73. self.repo.refs[b"HEAD"] = c1.id
  74. state = BisectState(self.repo)
  75. state.start()
  76. with self.assertRaises(ValueError):
  77. state.start()
  78. def test_mark_bad_no_session(self) -> None:
  79. """Test marking bad commit when no session is active."""
  80. state = BisectState(self.repo)
  81. with self.assertRaises(ValueError):
  82. state.mark_bad()
  83. def test_mark_good_no_session(self) -> None:
  84. """Test marking good commit when no session is active."""
  85. state = BisectState(self.repo)
  86. with self.assertRaises(ValueError):
  87. state.mark_good()
  88. def test_reset_no_session(self) -> None:
  89. """Test resetting when no session is active."""
  90. state = BisectState(self.repo)
  91. with self.assertRaises(ValueError):
  92. state.reset()
  93. def test_bisect_workflow(self) -> None:
  94. """Test a complete bisect workflow."""
  95. # Create some commits
  96. c1 = make_commit(id=b"1" * 40, message=b"good commit 1")
  97. c2 = make_commit(id=b"2" * 40, message=b"good commit 2", parents=[b"1" * 40])
  98. c3 = make_commit(id=b"3" * 40, message=b"bad commit", parents=[b"2" * 40])
  99. c4 = make_commit(id=b"4" * 40, message=b"bad commit 2", parents=[b"3" * 40])
  100. # Add commits to object store
  101. for commit in [c1, c2, c3, c4]:
  102. self.repo.object_store.add_object(commit)
  103. # Set HEAD to latest commit
  104. self.repo.refs[b"HEAD"] = c4.id
  105. # Start bisect
  106. state = BisectState(self.repo)
  107. state.start()
  108. # Mark bad and good
  109. state.mark_bad(c4.id)
  110. state.mark_good(c1.id)
  111. # Check that refs were created
  112. self.assertTrue(
  113. os.path.exists(
  114. os.path.join(self.repo.controldir(), "refs", "bisect", "bad")
  115. )
  116. )
  117. self.assertTrue(
  118. os.path.exists(
  119. os.path.join(
  120. self.repo.controldir(),
  121. "refs",
  122. "bisect",
  123. f"good-{c1.id.decode('ascii')}",
  124. )
  125. )
  126. )
  127. # Reset
  128. state.reset()
  129. self.assertFalse(state.is_active)