test_annotate.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # test_annotate.py -- tests for annotate
  2. # Copyright (C) 2015 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) a later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Tests for annotate support."""
  19. import tempfile
  20. from typing import Any
  21. from unittest import TestCase
  22. from dulwich.annotate import annotate_lines, update_lines
  23. from dulwich.objects import Blob, Commit, Tree
  24. from dulwich.repo import Repo
  25. class UpdateLinesTestCase(TestCase):
  26. """Tests for update_lines function."""
  27. def test_update_lines_equal(self) -> None:
  28. """Test update_lines when all lines are equal."""
  29. old_lines: list[tuple[tuple[Any, Any], bytes]] = [
  30. (("commit1", "entry1"), b"line1"),
  31. (("commit2", "entry2"), b"line2"),
  32. ]
  33. new_blob = b"line1\nline2"
  34. new_history_data = ("commit3", "entry3")
  35. result = update_lines(old_lines, new_history_data, new_blob) # type: ignore[arg-type]
  36. self.assertEqual(old_lines, result)
  37. def test_update_lines_insert(self) -> None:
  38. """Test update_lines when new lines are inserted."""
  39. old_lines: list[tuple[tuple[Any, Any], bytes]] = [
  40. (("commit1", "entry1"), b"line1"),
  41. (("commit2", "entry2"), b"line3"),
  42. ]
  43. new_blob = b"line1\nline2\nline3"
  44. new_history_data = ("commit3", "entry3")
  45. result = update_lines(old_lines, new_history_data, new_blob) # type: ignore[arg-type]
  46. expected = [
  47. (("commit1", "entry1"), b"line1"),
  48. (("commit3", "entry3"), b"line2"),
  49. (("commit2", "entry2"), b"line3"),
  50. ]
  51. self.assertEqual(expected, result)
  52. def test_update_lines_delete(self) -> None:
  53. """Test update_lines when lines are deleted."""
  54. old_lines: list[tuple[tuple[Any, Any], bytes]] = [
  55. (("commit1", "entry1"), b"line1"),
  56. (("commit2", "entry2"), b"line2"),
  57. (("commit3", "entry3"), b"line3"),
  58. ]
  59. new_blob = b"line1\nline3"
  60. new_history_data = ("commit4", "entry4")
  61. result = update_lines(old_lines, new_history_data, new_blob) # type: ignore[arg-type]
  62. expected = [
  63. (("commit1", "entry1"), b"line1"),
  64. (("commit3", "entry3"), b"line3"),
  65. ]
  66. self.assertEqual(expected, result)
  67. def test_update_lines_replace(self) -> None:
  68. """Test update_lines when lines are replaced."""
  69. old_lines: list[tuple[tuple[Any, Any], bytes]] = [
  70. (("commit1", "entry1"), b"line1"),
  71. (("commit2", "entry2"), b"line2"),
  72. ]
  73. new_blob = b"line1\nline2_modified"
  74. new_history_data = ("commit3", "entry3")
  75. result = update_lines(old_lines, new_history_data, new_blob) # type: ignore[arg-type]
  76. expected = [
  77. (("commit1", "entry1"), b"line1"),
  78. (("commit3", "entry3"), b"line2_modified"),
  79. ]
  80. self.assertEqual(expected, result)
  81. def test_update_lines_empty_old(self) -> None:
  82. """Test update_lines with empty old lines."""
  83. old_lines: list[tuple[tuple[Any, Any], bytes]] = []
  84. new_blob = b"line1\nline2"
  85. new_history_data = ("commit1", "entry1")
  86. result = update_lines(old_lines, new_history_data, new_blob) # type: ignore[arg-type]
  87. expected = [
  88. (("commit1", "entry1"), b"line1"),
  89. (("commit1", "entry1"), b"line2"),
  90. ]
  91. self.assertEqual(expected, result)
  92. def test_update_lines_empty_new(self) -> None:
  93. """Test update_lines with empty new blob."""
  94. old_lines: list[tuple[tuple[Any, Any], bytes]] = [
  95. (("commit1", "entry1"), b"line1")
  96. ]
  97. new_blob = b""
  98. new_history_data = ("commit2", "entry2")
  99. result = update_lines(old_lines, new_history_data, new_blob) # type: ignore[arg-type]
  100. self.assertEqual([], result)
  101. class AnnotateLinesTestCase(TestCase):
  102. """Tests for annotate_lines function."""
  103. def setUp(self) -> None:
  104. self.temp_dir = tempfile.mkdtemp()
  105. self.repo = Repo.init(self.temp_dir)
  106. def tearDown(self) -> None:
  107. self.repo.close()
  108. import shutil
  109. shutil.rmtree(self.temp_dir)
  110. def _make_commit(
  111. self, blob_content: bytes, message: str, parent: bytes | None = None
  112. ) -> bytes:
  113. """Helper to create a commit with a single file."""
  114. # Create blob
  115. blob = Blob()
  116. blob.data = blob_content
  117. self.repo.object_store.add_object(blob)
  118. # Create tree
  119. tree = Tree()
  120. tree.add(b"test.txt", 0o100644, blob.id)
  121. self.repo.object_store.add_object(tree)
  122. # Create commit
  123. commit = Commit()
  124. commit.tree = tree.id
  125. commit.author = commit.committer = b"Test Author <test@example.com>"
  126. commit.author_time = commit.commit_time = 1000000000
  127. commit.author_timezone = commit.commit_timezone = 0
  128. commit.encoding = b"UTF-8"
  129. commit.message = message.encode("utf-8")
  130. if parent:
  131. commit.parents = [parent]
  132. else:
  133. commit.parents = []
  134. self.repo.object_store.add_object(commit)
  135. return commit.id
  136. def test_annotate_lines_single_commit(self) -> None:
  137. """Test annotating a file with a single commit."""
  138. commit_id = self._make_commit(b"line1\nline2\nline3\n", "Initial commit")
  139. result = annotate_lines(self.repo.object_store, commit_id, b"test.txt")
  140. self.assertEqual(3, len(result))
  141. for (commit, entry), line in result:
  142. self.assertEqual(commit_id, commit.id)
  143. self.assertIn(line, [b"line1\n", b"line2\n", b"line3\n"])
  144. def test_annotate_lines_multiple_commits(self) -> None:
  145. """Test annotating a file with multiple commits."""
  146. # First commit
  147. commit1_id = self._make_commit(b"line1\nline2\n", "Initial commit")
  148. # Second commit - add a line
  149. commit2_id = self._make_commit(
  150. b"line1\nline1.5\nline2\n", "Add line between", parent=commit1_id
  151. )
  152. # Third commit - modify a line
  153. commit3_id = self._make_commit(
  154. b"line1_modified\nline1.5\nline2\n", "Modify first line", parent=commit2_id
  155. )
  156. result = annotate_lines(self.repo.object_store, commit3_id, b"test.txt")
  157. self.assertEqual(3, len(result))
  158. # First line should be from commit3
  159. self.assertEqual(commit3_id, result[0][0][0].id)
  160. self.assertEqual(b"line1_modified\n", result[0][1])
  161. # Second line should be from commit2
  162. self.assertEqual(commit2_id, result[1][0][0].id)
  163. self.assertEqual(b"line1.5\n", result[1][1])
  164. # Third line should be from commit1
  165. self.assertEqual(commit1_id, result[2][0][0].id)
  166. self.assertEqual(b"line2\n", result[2][1])
  167. def test_annotate_lines_nonexistent_path(self) -> None:
  168. """Test annotating a nonexistent file."""
  169. commit_id = self._make_commit(b"content\n", "Initial commit")
  170. result = annotate_lines(self.repo.object_store, commit_id, b"nonexistent.txt")
  171. self.assertEqual([], result)