Source code for tests.test_mailmap

# Copyright Kevin Deldycke <[email protected]> and contributors.
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

"""Tests for `repomatic.mailmap`: `.mailmap` parsing and contributor sync."""

from __future__ import annotations

import subprocess
from textwrap import dedent

from click.testing import CliRunner

from repomatic.cli import repomatic
from repomatic.mailmap import Mailmap, Record, remove_header


[docs] def test_sync_mailmap_bootstraps_missing_file(tmp_path, monkeypatch, hermetic_git): """A repository with no `.mailmap` yet gets one created instead of crashing. The `--create-if-missing` default path used to read an unbound `content` variable when the source file did not exist. """ monkeypatch.chdir(tmp_path) subprocess.run(["git", "init", "--quiet"], check=True, encoding="UTF-8") subprocess.run( ["git", "config", "user.email", "[email protected]"], check=True, encoding="UTF-8", ) subprocess.run( ["git", "config", "user.name", "Kiwi Papaya"], check=True, encoding="UTF-8" ) (tmp_path / "fruit.txt").write_text("papaya\n", encoding="UTF-8") subprocess.run(["git", "add", "fruit.txt"], check=True, encoding="UTF-8") subprocess.run( ["git", "commit", "--quiet", "--message", "Add fruit", "--no-gpg-sign"], check=True, encoding="UTF-8", ) result = CliRunner().invoke(repomatic, ["sync-mailmap"]) assert result.exit_code == 0, result.output mailmap = (tmp_path / ".mailmap").read_text(encoding="UTF-8") assert "Kiwi Papaya" in mailmap
[docs] def test_remove_header(): content = dedent("""\ # Generated by repomatic sync-mailmap v4.4.3 - https://github.com/kdeldycke/repomatic # Timestamp: 2024-08-12T08:15:41.083405 # Format is: # Preferred Name <preferred e-mail> Other Name <other e-mail> # # Reference: https://git-scm.com/docs/git-blame#_mapping_authors GitHub <[email protected]> """) assert remove_header(content) == dedent("""\ # Format is: # Preferred Name <preferred e-mail> Other Name <other e-mail> # # Reference: https://git-scm.com/docs/git-blame#_mapping_authors GitHub <[email protected]>""")
[docs] def test_mailmap(): mailmap = Mailmap() mailmap.parse( dedent( """ github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> GitHub Actions <[email protected]> GitHub <[email protected]> GitHub <[email protected]> GitHub <[email protected]> dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> GitHub <[email protected]> GitHub <[email protected]> """ ) ) assert mailmap.render() == dedent( """\ GitHub <[email protected]> GitHub <[email protected]> GitHub <[email protected]> dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> GitHub <[email protected]> github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> GitHub Actions <[email protected]>""" )
[docs] def test_new_entry(): mailmap = Mailmap() header = dedent( """\ # Generated by repomatic sync-mailmap v4.16.2 - https://github.com/kdeldycke/repomatic # Timestamp: 2025-05-13T13:14:51.128939 # Format is: # Preferred Name <preferred e-mail> Other Name <other e-mail> # # Reference: https://git-scm.com/docs/git-blame#_mapping_authors""" ) mailmap.parse( dedent( f"""\ {header} dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> """ ) ) assert len(mailmap.records) == 1 assert ( mailmap.records[0].canonical == "dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>" ) assert mailmap.records[0].aliases == set() assert mailmap.records[0].pre_comment == header # Add new record manually new_contributor = "AA BB <[email protected]>" mailmap.records.append(Record(canonical=new_contributor)) assert mailmap.render() == ( header + "\n" + new_contributor + "\n" + "dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>" )