test_porcelain_notes.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # test_porcelain_notes.py -- Tests for porcelain notes functions
  2. # Copyright (C) 2024 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for porcelain notes functions."""
  21. import os
  22. import tempfile
  23. from unittest import TestCase
  24. from dulwich import porcelain
  25. from dulwich.repo import Repo
  26. class TestPorcelainNotes(TestCase):
  27. """Test porcelain notes functions."""
  28. def setUp(self):
  29. self.test_dir = tempfile.mkdtemp()
  30. self.repo = Repo.init(self.test_dir)
  31. self.addCleanup(self.cleanup)
  32. # Create a test commit to annotate using porcelain
  33. with open(os.path.join(self.test_dir, "test.txt"), "wb") as f:
  34. f.write(b"Test content")
  35. porcelain.add(self.test_dir, ["test.txt"])
  36. self.test_commit_id = porcelain.commit(
  37. self.test_dir,
  38. message=b"Test commit",
  39. author=b"Test User <test@example.com>",
  40. committer=b"Test User <test@example.com>",
  41. )
  42. def cleanup(self):
  43. import shutil
  44. shutil.rmtree(self.test_dir)
  45. def test_notes_add_and_show(self):
  46. """Test adding and showing a note."""
  47. # Add a note
  48. note_commit = porcelain.notes_add(
  49. self.test_dir, self.test_commit_id, "This is a test note"
  50. )
  51. self.assertIsNotNone(note_commit)
  52. # Show the note
  53. note = porcelain.notes_show(self.test_dir, self.test_commit_id)
  54. self.assertEqual(b"This is a test note", note)
  55. def test_notes_add_bytes(self):
  56. """Test adding a note with bytes."""
  57. # Add a note with bytes
  58. note_commit = porcelain.notes_add(
  59. self.test_dir, self.test_commit_id, b"This is a byte note"
  60. )
  61. self.assertIsNotNone(note_commit)
  62. # Show the note
  63. note = porcelain.notes_show(self.test_dir, self.test_commit_id)
  64. self.assertEqual(b"This is a byte note", note)
  65. def test_notes_show_nonexistent(self):
  66. """Test showing a note that doesn't exist."""
  67. note = porcelain.notes_show(self.test_dir, self.test_commit_id)
  68. self.assertIsNone(note)
  69. def test_notes_remove(self):
  70. """Test removing a note."""
  71. # First add a note
  72. porcelain.notes_add(self.test_dir, self.test_commit_id, "Test note to remove")
  73. # Then remove it
  74. result = porcelain.notes_remove(self.test_dir, self.test_commit_id)
  75. self.assertIsNotNone(result)
  76. # Verify it's gone
  77. note = porcelain.notes_show(self.test_dir, self.test_commit_id)
  78. self.assertIsNone(note)
  79. def test_notes_remove_nonexistent(self):
  80. """Test removing a note that doesn't exist."""
  81. result = porcelain.notes_remove(self.test_dir, self.test_commit_id)
  82. self.assertIsNone(result)
  83. def test_notes_list_empty(self):
  84. """Test listing notes when there are none."""
  85. notes = porcelain.notes_list(self.test_dir)
  86. self.assertEqual([], notes)
  87. def test_notes_list(self):
  88. """Test listing notes."""
  89. # Create another commit to test multiple notes
  90. with open(os.path.join(self.test_dir, "test2.txt"), "wb") as f:
  91. f.write(b"Test content 2")
  92. porcelain.add(self.test_dir, ["test2.txt"])
  93. commit2_id = porcelain.commit(
  94. self.test_dir,
  95. message=b"Test commit 2",
  96. author=b"Test User <test@example.com>",
  97. committer=b"Test User <test@example.com>",
  98. )
  99. porcelain.notes_add(self.test_dir, self.test_commit_id, "Note 1")
  100. porcelain.notes_add(self.test_dir, commit2_id, "Note 2")
  101. # List notes
  102. notes = porcelain.notes_list(self.test_dir)
  103. self.assertEqual(2, len(notes))
  104. # Check content
  105. notes_dict = dict(notes)
  106. self.assertEqual(b"Note 1", notes_dict[self.test_commit_id])
  107. self.assertEqual(b"Note 2", notes_dict[commit2_id])
  108. def test_notes_custom_ref(self):
  109. """Test using a custom notes ref."""
  110. # Add note to custom ref
  111. porcelain.notes_add(
  112. self.test_dir, self.test_commit_id, "Custom ref note", ref="custom"
  113. )
  114. # Show from default ref (should not exist)
  115. note = porcelain.notes_show(self.test_dir, self.test_commit_id)
  116. self.assertIsNone(note)
  117. # Show from custom ref
  118. note = porcelain.notes_show(self.test_dir, self.test_commit_id, ref="custom")
  119. self.assertEqual(b"Custom ref note", note)
  120. def test_notes_update(self):
  121. """Test updating an existing note."""
  122. # Add initial note
  123. porcelain.notes_add(self.test_dir, self.test_commit_id, "Initial note")
  124. # Update the note
  125. porcelain.notes_add(self.test_dir, self.test_commit_id, "Updated note")
  126. # Verify update
  127. note = porcelain.notes_show(self.test_dir, self.test_commit_id)
  128. self.assertEqual(b"Updated note", note)
  129. def test_notes_custom_author_committer(self):
  130. """Test adding note with custom author and committer."""
  131. note_commit_id = porcelain.notes_add(
  132. self.test_dir,
  133. self.test_commit_id,
  134. "Test note",
  135. author=b"Custom Author <author@example.com>",
  136. committer=b"Custom Committer <committer@example.com>",
  137. )
  138. # Check the commit
  139. commit = self.repo[note_commit_id]
  140. self.assertEqual(b"Custom Author <author@example.com>", commit.author)
  141. self.assertEqual(b"Custom Committer <committer@example.com>", commit.committer)