test_bisect.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # test_bisect.py -- tests for porcelain bisect
  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 porcelain bisect functions."""
  22. import os
  23. import shutil
  24. import tempfile
  25. from dulwich import porcelain
  26. from dulwich.objects import Tree
  27. from dulwich.tests.utils import make_commit
  28. from .. import TestCase
  29. class BisectPorcelainTests(TestCase):
  30. """Tests for porcelain bisect functions."""
  31. def setUp(self) -> None:
  32. self.test_dir = tempfile.mkdtemp()
  33. self.repo = porcelain.init(self.test_dir)
  34. # Create tree objects
  35. tree = Tree()
  36. self.repo.object_store.add_object(tree)
  37. # Create some commits with proper trees
  38. self.c1 = make_commit(id=b"1" * 40, message=b"initial commit", tree=tree.id)
  39. self.c2 = make_commit(
  40. id=b"2" * 40, message=b"second commit", parents=[b"1" * 40], tree=tree.id
  41. )
  42. self.c3 = make_commit(
  43. id=b"3" * 40, message=b"third commit", parents=[b"2" * 40], tree=tree.id
  44. )
  45. self.c4 = make_commit(
  46. id=b"4" * 40, message=b"fourth commit", parents=[b"3" * 40], tree=tree.id
  47. )
  48. # Add commits to object store
  49. for commit in [self.c1, self.c2, self.c3, self.c4]:
  50. self.repo.object_store.add_object(commit)
  51. # Set HEAD to latest commit
  52. self.repo.refs[b"HEAD"] = self.c4.id
  53. self.repo.refs[b"refs/heads/master"] = self.c4.id
  54. def tearDown(self) -> None:
  55. shutil.rmtree(self.test_dir)
  56. def test_bisect_start(self) -> None:
  57. """Test bisect_start porcelain function."""
  58. porcelain.bisect_start(self.test_dir)
  59. # Check that bisect state files exist
  60. self.assertTrue(
  61. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_START"))
  62. )
  63. def test_bisect_bad_good(self) -> None:
  64. """Test marking commits as bad and good."""
  65. porcelain.bisect_start(self.test_dir)
  66. porcelain.bisect_bad(self.test_dir, self.c4.id.decode("ascii"))
  67. porcelain.bisect_good(self.test_dir, self.c1.id.decode("ascii"))
  68. # Check that refs were created
  69. self.assertTrue(
  70. os.path.exists(
  71. os.path.join(self.repo.controldir(), "refs", "bisect", "bad")
  72. )
  73. )
  74. self.assertTrue(
  75. os.path.exists(
  76. os.path.join(
  77. self.repo.controldir(),
  78. "refs",
  79. "bisect",
  80. f"good-{self.c1.id.decode('ascii')}",
  81. )
  82. )
  83. )
  84. def test_bisect_log(self) -> None:
  85. """Test getting bisect log."""
  86. porcelain.bisect_start(self.test_dir)
  87. porcelain.bisect_bad(self.test_dir, self.c4.id.decode("ascii"))
  88. porcelain.bisect_good(self.test_dir, self.c1.id.decode("ascii"))
  89. log = porcelain.bisect_log(self.test_dir)
  90. self.assertIn("git bisect start", log)
  91. self.assertIn("git bisect bad", log)
  92. self.assertIn("git bisect good", log)
  93. def test_bisect_reset(self) -> None:
  94. """Test resetting bisect state."""
  95. porcelain.bisect_start(self.test_dir)
  96. porcelain.bisect_bad(self.test_dir)
  97. porcelain.bisect_good(self.test_dir, self.c1.id.decode("ascii"))
  98. porcelain.bisect_reset(self.test_dir)
  99. # Check that bisect state files are removed
  100. self.assertFalse(
  101. os.path.exists(os.path.join(self.repo.controldir(), "BISECT_START"))
  102. )
  103. self.assertFalse(
  104. os.path.exists(os.path.join(self.repo.controldir(), "refs", "bisect"))
  105. )