File: test_notes.py

package info (click to toggle)
dulwich 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,388 kB
  • sloc: python: 99,991; makefile: 163; sh: 67
file content (219 lines) | stat: -rw-r--r-- 7,980 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# test_porcelain_notes.py -- Tests for porcelain notes functions
# Copyright (C) 2024 Jelmer Vernooij <jelmer@jelmer.uk>
#
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#

"""Tests for porcelain notes functions."""

import os
import tempfile
from unittest import TestCase

from dulwich import porcelain
from dulwich.repo import Repo


class TestPorcelainNotes(TestCase):
    """Test porcelain notes functions."""

    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.repo = Repo.init(self.test_dir)
        self.addCleanup(self.cleanup)

        # Create a test commit to annotate using porcelain
        with open(os.path.join(self.test_dir, "test.txt"), "wb") as f:
            f.write(b"Test content")

        porcelain.add(self.test_dir, ["test.txt"])
        self.test_commit_id = porcelain.commit(
            self.test_dir,
            message=b"Test commit",
            author=b"Test User <test@example.com>",
            committer=b"Test User <test@example.com>",
        )

    def cleanup(self):
        import shutil

        shutil.rmtree(self.test_dir)

    def test_notes_add_and_show(self):
        """Test adding and showing a note."""
        # Add a note
        note_commit = porcelain.notes_add(
            self.test_dir, self.test_commit_id, "This is a test note"
        )
        self.assertIsNotNone(note_commit)

        # Show the note
        note = porcelain.notes_show(self.test_dir, self.test_commit_id)
        self.assertEqual(b"This is a test note", note)

    def test_notes_add_bytes(self):
        """Test adding a note with bytes."""
        # Add a note with bytes
        note_commit = porcelain.notes_add(
            self.test_dir, self.test_commit_id, b"This is a byte note"
        )
        self.assertIsNotNone(note_commit)

        # Show the note
        note = porcelain.notes_show(self.test_dir, self.test_commit_id)
        self.assertEqual(b"This is a byte note", note)

    def test_notes_show_nonexistent(self):
        """Test showing a note that doesn't exist."""
        note = porcelain.notes_show(self.test_dir, self.test_commit_id)
        self.assertIsNone(note)

    def test_notes_remove(self):
        """Test removing a note."""
        # First add a note
        porcelain.notes_add(self.test_dir, self.test_commit_id, "Test note to remove")

        # Then remove it
        result = porcelain.notes_remove(self.test_dir, self.test_commit_id)
        self.assertIsNotNone(result)

        # Verify it's gone
        note = porcelain.notes_show(self.test_dir, self.test_commit_id)
        self.assertIsNone(note)

    def test_notes_remove_nonexistent(self):
        """Test removing a note that doesn't exist."""
        result = porcelain.notes_remove(self.test_dir, self.test_commit_id)
        self.assertIsNone(result)

    def test_notes_list_empty(self):
        """Test listing notes when there are none."""
        notes = porcelain.notes_list(self.test_dir)
        self.assertEqual([], notes)

    def test_notes_list(self):
        """Test listing notes."""
        # Create another commit to test multiple notes
        with open(os.path.join(self.test_dir, "test2.txt"), "wb") as f:
            f.write(b"Test content 2")

        porcelain.add(self.test_dir, ["test2.txt"])
        commit2_id = porcelain.commit(
            self.test_dir,
            message=b"Test commit 2",
            author=b"Test User <test@example.com>",
            committer=b"Test User <test@example.com>",
        )

        porcelain.notes_add(self.test_dir, self.test_commit_id, "Note 1")
        porcelain.notes_add(self.test_dir, commit2_id, "Note 2")

        # List notes
        notes = porcelain.notes_list(self.test_dir)
        self.assertEqual(2, len(notes))

        # Check content
        notes_dict = dict(notes)
        self.assertEqual(b"Note 1", notes_dict[self.test_commit_id])
        self.assertEqual(b"Note 2", notes_dict[commit2_id])

    def test_notes_custom_ref(self):
        """Test using a custom notes ref."""
        # Add note to custom ref
        porcelain.notes_add(
            self.test_dir, self.test_commit_id, "Custom ref note", ref="custom"
        )

        # Show from default ref (should not exist)
        note = porcelain.notes_show(self.test_dir, self.test_commit_id)
        self.assertIsNone(note)

        # Show from custom ref
        note = porcelain.notes_show(self.test_dir, self.test_commit_id, ref="custom")
        self.assertEqual(b"Custom ref note", note)

    def test_notes_update(self):
        """Test updating an existing note."""
        # Add initial note
        porcelain.notes_add(self.test_dir, self.test_commit_id, "Initial note")

        # Update the note
        porcelain.notes_add(self.test_dir, self.test_commit_id, "Updated note")

        # Verify update
        note = porcelain.notes_show(self.test_dir, self.test_commit_id)
        self.assertEqual(b"Updated note", note)

    def test_notes_custom_author_committer(self):
        """Test adding note with custom author and committer."""
        note_commit_id = porcelain.notes_add(
            self.test_dir,
            self.test_commit_id,
            "Test note",
            author=b"Custom Author <author@example.com>",
            committer=b"Custom Committer <committer@example.com>",
        )

        # Check the commit
        commit = self.repo[note_commit_id]
        self.assertEqual(b"Custom Author <author@example.com>", commit.author)
        self.assertEqual(b"Custom Committer <committer@example.com>", commit.committer)

    def test_notes_ref_already_qualified(self):
        """Test using a notes ref that's already fully qualified."""
        # Add note using fully qualified ref
        porcelain.notes_add(
            self.test_dir,
            self.test_commit_id,
            b"Note with qualified ref",
            ref=b"refs/notes/custom",
        )

        # Show using the same qualified ref
        note = porcelain.notes_show(
            self.test_dir, self.test_commit_id, ref=b"refs/notes/custom"
        )
        self.assertEqual(b"Note with qualified ref", note)

    def test_notes_remove_with_string_ref(self):
        """Test removing a note with string ref parameter."""
        # Add a note to custom ref
        porcelain.notes_add(
            self.test_dir, self.test_commit_id, b"Note to remove", ref="custom"
        )

        # Remove using string ref
        result = porcelain.notes_remove(
            self.test_dir, self.test_commit_id, ref="custom"
        )
        self.assertIsNotNone(result)

        # Verify it's gone
        note = porcelain.notes_show(self.test_dir, self.test_commit_id, ref="custom")
        self.assertIsNone(note)

    def test_notes_list_with_string_ref(self):
        """Test listing notes with string ref parameter."""
        # Add notes to custom ref
        porcelain.notes_add(
            self.test_dir, self.test_commit_id, b"Custom note", ref="myref"
        )

        # List using string ref
        notes = porcelain.notes_list(self.test_dir, ref="myref")
        self.assertEqual(1, len(notes))
        self.assertEqual(b"Custom note", notes[0][1])