File: test_mailmap.py

package info (click to toggle)
dulwich 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,388 kB
  • sloc: python: 99,991; makefile: 163; sh: 67
file content (244 lines) | stat: -rw-r--r-- 9,268 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# test_mailmap.py -- Tests for dulwich.mailmap
# Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
# 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 dulwich.mailmap."""

from io import BytesIO
from unittest import TestCase

from dulwich.mailmap import Mailmap, read_mailmap


class ReadMailmapTests(TestCase):
    def test_read(self) -> None:
        b = BytesIO(
            b"""\
Jane Doe         <jane@desktop.(none)>
Joe R. Developer <joe@example.com>
# A comment
<cto@company.xx>                       <cto@coompany.xx> # Comment
Some Dude <some@dude.xx>         nick1 <bugs@company.xx>
Other Author <other@author.xx>   nick2 <bugs@company.xx>
Other Author <other@author.xx>         <nick2@company.xx>
Santa Claus <santa.claus@northpole.xx> <me@company.xx>
"""
        )
        self.assertEqual(
            [
                ((b"Jane Doe", b"jane@desktop.(none)"), None),
                ((b"Joe R. Developer", b"joe@example.com"), None),
                ((None, b"cto@company.xx"), (None, b"cto@coompany.xx")),
                (
                    (b"Some Dude", b"some@dude.xx"),
                    (b"nick1", b"bugs@company.xx"),
                ),
                (
                    (b"Other Author", b"other@author.xx"),
                    (b"nick2", b"bugs@company.xx"),
                ),
                (
                    (b"Other Author", b"other@author.xx"),
                    (None, b"nick2@company.xx"),
                ),
                (
                    (b"Santa Claus", b"santa.claus@northpole.xx"),
                    (None, b"me@company.xx"),
                ),
            ],
            list(read_mailmap(b)),
        )


class MailmapTests(TestCase):
    def test_lookup(self) -> None:
        m = Mailmap()
        m.add_entry((b"Jane Doe", b"jane@desktop.(none)"), (None, None))
        m.add_entry((b"Joe R. Developer", b"joe@example.com"), None)
        m.add_entry((None, b"cto@company.xx"), (None, b"cto@coompany.xx"))
        m.add_entry((b"Some Dude", b"some@dude.xx"), (b"nick1", b"bugs@company.xx"))
        m.add_entry(
            (b"Other Author", b"other@author.xx"),
            (b"nick2", b"bugs@company.xx"),
        )
        m.add_entry((b"Other Author", b"other@author.xx"), (None, b"nick2@company.xx"))
        m.add_entry(
            (b"Santa Claus", b"santa.claus@northpole.xx"),
            (None, b"me@company.xx"),
        )
        self.assertEqual(
            b"Jane Doe <jane@desktop.(none)>",
            m.lookup(b"Jane Doe <jane@desktop.(none)>"),
        )
        self.assertEqual(
            b"Jane Doe <jane@desktop.(none)>",
            m.lookup(b"Jane Doe <jane@example.com>"),
        )
        self.assertEqual(
            b"Jane Doe <jane@desktop.(none)>",
            m.lookup(b"Jane D. <jane@desktop.(none)>"),
        )
        self.assertEqual(
            b"Some Dude <some@dude.xx>", m.lookup(b"nick1 <bugs@company.xx>")
        )
        self.assertEqual(b"CTO <cto@company.xx>", m.lookup(b"CTO <cto@coompany.xx>"))

    def test_lookup_with_identity_tuple(self) -> None:
        """Test lookup using an identity tuple instead of a string."""
        m = Mailmap()
        m.add_entry(
            (b"Real Name", b"real@example.com"), (b"Alias", b"alias@example.com")
        )

        # Test lookup with a tuple
        self.assertEqual(
            (b"Real Name", b"real@example.com"),
            m.lookup((b"Alias", b"alias@example.com")),
        )

        # Test lookup with another tuple that doesn't match anything
        self.assertEqual(
            (b"Unknown", b"unknown@example.com"),
            m.lookup((b"Unknown", b"unknown@example.com")),
        )

    def test_lookup_with_no_match(self) -> None:
        """Test lookup when no match is found."""
        m = Mailmap()
        m.add_entry(
            (b"Real Name", b"real@example.com"), (b"Alias", b"alias@example.com")
        )

        # No match should return the original identity
        original = b"Unknown <unknown@example.com>"
        self.assertEqual(original, m.lookup(original))

    def test_lookup_partial_matches(self) -> None:
        """Test lookup with partial matches (name or email only)."""
        m = Mailmap()
        # Add entry with only name
        m.add_entry((b"Real Name", None), (b"Any Name", None))
        # Add entry with only email
        m.add_entry((None, b"real@example.com"), (None, b"other@example.com"))

        # Match by name
        self.assertEqual(
            b"Real Name <any@example.com>", m.lookup(b"Any Name <any@example.com>")
        )

        # Match by email
        self.assertEqual(
            b"Any Name <real@example.com>", m.lookup(b"Any Name <other@example.com>")
        )

    def test_add_entry_name_or_email_only(self) -> None:
        """Test adding entries with only name or only email."""
        m = Mailmap()

        # Entry with only canonical name
        m.add_entry((b"Real Name", None), (b"Alias", b"alias@example.com"))

        # Entry with only canonical email
        m.add_entry((None, b"real@example.com"), (b"Other", b"other@example.com"))

        # Lookup should properly combine the identity parts
        self.assertEqual(
            b"Real Name <alias@example.com>", m.lookup(b"Alias <alias@example.com>")
        )

        self.assertEqual(
            b"Other <real@example.com>", m.lookup(b"Other <other@example.com>")
        )

    def test_init_with_iterator(self) -> None:
        """Test initializing Mailmap with an iterator of entries."""
        entries = [
            ((b"Real Name", b"real@example.com"), (b"Alias", b"alias@example.com")),
            ((b"Another", b"another@example.com"), None),
        ]

        m = Mailmap(iter(entries))

        # Verify the entries were added
        self.assertEqual(
            b"Real Name <real@example.com>", m.lookup(b"Alias <alias@example.com>")
        )
        self.assertEqual(
            b"Another <another@example.com>",
            m.lookup(b"Another <another@example.com>"),
        )

    def test_lookup_with_none_name(self) -> None:
        """Test lookup when canonical name is None (only email is canonical)."""
        m = Mailmap()
        # Add entry with canonical email but no canonical name
        m.add_entry((None, b"real@example.com"), (b"Alias", b"alias@example.com"))

        # When canonical name is None, use original name
        result = m.lookup(b"Alias <alias@example.com>")
        self.assertEqual(b"Alias <real@example.com>", result)

        # Test formatting when name becomes empty
        m2 = Mailmap()
        m2.add_entry((None, b"real@example.com"), (None, b"alias@example.com"))
        result2 = m2.lookup(b" <alias@example.com>")
        # When both names are None/empty, result has empty name
        self.assertEqual(b" <real@example.com>", result2)

    def test_lookup_with_none_email(self) -> None:
        """Test lookup when canonical email is None (only name is canonical)."""
        m = Mailmap()
        # Add entry with canonical name but no canonical email
        m.add_entry((b"Real Name", None), (b"Alias", b"alias@example.com"))

        # When canonical email is None, use original email
        result = m.lookup(b"Alias <alias@example.com>")
        self.assertEqual(b"Real Name <alias@example.com>", result)

        # Test formatting when email becomes empty
        m2 = Mailmap()
        m2.add_entry((b"Real Name", None), (b"Alias", None))
        result2 = m2.lookup(b"Alias <>")
        # When both emails are None/empty, result has empty email
        self.assertEqual(b"Real Name <>", result2)

    def test_from_path(self) -> None:
        """Test creating Mailmap from a file path."""
        import os
        import tempfile

        # Create a temporary mailmap file
        with tempfile.NamedTemporaryFile(
            mode="wb", delete=False, suffix=".mailmap"
        ) as f:
            f.write(b"Real Name <real@example.com> <alias@example.com>\n")
            f.write(b"Another Person <another@example.com>\n")
            mailmap_path = f.name

        try:
            # Load from path
            m = Mailmap.from_path(mailmap_path)

            # Verify entries were loaded
            self.assertEqual(
                b"Real Name <real@example.com>", m.lookup(b"Alias <alias@example.com>")
            )
        finally:
            os.unlink(mailmap_path)