test_worktree.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # test_worktree.py -- tests for porcelain worktree functions
  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 worktree functions."""
  22. import os
  23. import shutil
  24. import tempfile
  25. from dulwich import porcelain
  26. from dulwich.objects import Blob, Commit, Tree
  27. from dulwich.repo import Repo
  28. from .. import TestCase
  29. class WorktreeErrorTests(TestCase):
  30. """Tests for worktree function error handling."""
  31. def setUp(self):
  32. super().setUp()
  33. self.test_dir = tempfile.mkdtemp()
  34. self.repo_path = os.path.join(self.test_dir, "repo")
  35. self.repo = Repo.init(self.repo_path, mkdir=True)
  36. # Create a simple commit
  37. blob = Blob.from_string(b"test content")
  38. self.repo.object_store.add_object(blob)
  39. tree = Tree()
  40. tree.add(b"test.txt", 0o100644, blob.id)
  41. self.repo.object_store.add_object(tree)
  42. commit = Commit()
  43. commit.tree = tree.id
  44. commit.author = commit.committer = b"Test User <test@example.com>"
  45. commit.author_time = commit.commit_time = 1234567890
  46. commit.author_timezone = commit.commit_timezone = 0
  47. commit.encoding = b"UTF-8"
  48. commit.message = b"Test commit"
  49. self.repo.object_store.add_object(commit)
  50. self.repo.refs[b"refs/heads/main"] = commit.id
  51. self.repo.refs.set_symbolic_ref(b"HEAD", b"refs/heads/main")
  52. def tearDown(self):
  53. shutil.rmtree(self.test_dir)
  54. super().tearDown()
  55. def test_worktree_add_without_path(self) -> None:
  56. """Test worktree_add raises ValueError when path is None."""
  57. with self.assertRaises(ValueError) as cm:
  58. porcelain.worktree_add(self.repo, path=None)
  59. self.assertEqual("Path is required for worktree add", str(cm.exception))
  60. def test_worktree_remove_without_path(self) -> None:
  61. """Test worktree_remove raises ValueError when path is None."""
  62. with self.assertRaises(ValueError) as cm:
  63. porcelain.worktree_remove(self.repo, path=None)
  64. self.assertEqual("Path is required for worktree remove", str(cm.exception))
  65. def test_worktree_lock_without_path(self) -> None:
  66. """Test worktree_lock raises ValueError when path is None."""
  67. with self.assertRaises(ValueError) as cm:
  68. porcelain.worktree_lock(self.repo, path=None)
  69. self.assertEqual("Path is required for worktree lock", str(cm.exception))
  70. def test_worktree_unlock_without_path(self) -> None:
  71. """Test worktree_unlock raises ValueError when path is None."""
  72. with self.assertRaises(ValueError) as cm:
  73. porcelain.worktree_unlock(self.repo, path=None)
  74. self.assertEqual("Path is required for worktree unlock", str(cm.exception))
  75. def test_worktree_move_without_old_path(self) -> None:
  76. """Test worktree_move raises ValueError when old_path is None."""
  77. new_path = os.path.join(self.test_dir, "new_worktree")
  78. with self.assertRaises(ValueError) as cm:
  79. porcelain.worktree_move(self.repo, old_path=None, new_path=new_path)
  80. self.assertEqual(
  81. "Both old_path and new_path are required for worktree move",
  82. str(cm.exception),
  83. )
  84. def test_worktree_move_without_new_path(self) -> None:
  85. """Test worktree_move raises ValueError when new_path is None."""
  86. old_path = os.path.join(self.test_dir, "old_worktree")
  87. with self.assertRaises(ValueError) as cm:
  88. porcelain.worktree_move(self.repo, old_path=old_path, new_path=None)
  89. self.assertEqual(
  90. "Both old_path and new_path are required for worktree move",
  91. str(cm.exception),
  92. )
  93. def test_worktree_move_without_both_paths(self) -> None:
  94. """Test worktree_move raises ValueError when both paths are None."""
  95. with self.assertRaises(ValueError) as cm:
  96. porcelain.worktree_move(self.repo, old_path=None, new_path=None)
  97. self.assertEqual(
  98. "Both old_path and new_path are required for worktree move",
  99. str(cm.exception),
  100. )
  101. def test_worktree_repair_with_no_paths(self) -> None:
  102. """Test worktree_repair with paths=None (repairs all)."""
  103. # Should not raise, just return empty list if no worktrees to repair
  104. result = porcelain.worktree_repair(self.repo, paths=None)
  105. self.assertIsInstance(result, list)