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

import pytest
from click.testing import CliRunner

from repomatic.cli.main import repomatic
from repomatic.file_ops import generated_header, strip_generated_header
from repomatic.mailmap import Mailmap, Record


[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] @pytest.mark.parametrize( ("mailmap", "args", "message"), ( pytest.param( None, ("--skip-if-missing", "papaya.mailmap"), "papaya.mailmap does not exist, stop the sync process.", id="skip-if-missing", ), pytest.param( "Kiwi Papaya <[email protected]>\n", (), "Nothing to update, stop the sync process.", id="up-to-date", ), pytest.param(None, ("-",), "\nKiwi Papaya <[email protected]>\n", id="stdout"), ), ) def test_sync_mailmap_leaves_the_tree_alone( tmp_path, monkeypatch, mailmap, args, message ): """A run with nothing to write, or writing to stdout, touches no file.""" orchard = tmp_path / "orchard" orchard.mkdir() if mailmap is not None: (orchard / ".mailmap").write_text(mailmap, encoding="UTF-8") before = {path.name: path.read_bytes() for path in orchard.iterdir()} monkeypatch.chdir(orchard) monkeypatch.setattr( "repomatic.mailmap.list_contributor_identities", lambda: {"Kiwi Papaya <[email protected]>"}, ) result = CliRunner().invoke(repomatic, ["sync-mailmap", *args]) assert result.exit_code == 0, result.output assert message in result.output assert {path.name: path.read_bytes() for path in orchard.iterdir()} == before
[docs] def test_strip_generated_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 strip_generated_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]> """ # noqa: E501 ) ) 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]>""" # noqa: E501 )
[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 a new record by hand. 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>" )
[docs] def test_sync_mailmap_reports_a_git_failure(tmp_path, monkeypatch, hermetic_git): """Outside a git repository the command fails with git's own message.""" monkeypatch.chdir(tmp_path) result = CliRunner().invoke(repomatic, ["sync-mailmap"]) assert result.exit_code == 1 assert "Error" in result.output assert not (tmp_path / ".mailmap").exists()
[docs] def test_strip_generated_header_reads_what_generated_header_writes(): """The reader drops exactly the block the writer stamps.""" body = "# Format is:\nKiwi <[email protected]>" stamped = generated_header("repomatic sync-mailmap") + body assert strip_generated_header(stamped) == body
[docs] def test_render_leaves_the_records_untouched(): """Rendering twice gives the same text: the top comment is not consumed.""" mailmap = Mailmap() mailmap.parse( "# Fruit stand.\nMelon <[email protected]>\nApricot <[email protected]>\n" ) first = mailmap.render() assert mailmap.render() == first assert first == ( "# Fruit stand.\nApricot <[email protected]>\nMelon <[email protected]>" )
[docs] def test_update_from_git_matches_identities_without_case(monkeypatch): """A contributor matching a record in another case is not added again.""" mailmap = Mailmap() mailmap.parse("Kiwi Fruit <[email protected]> Kiwi <[email protected]>\n") monkeypatch.setattr( "repomatic.mailmap.list_contributor_identities", lambda: { "kiwi <[email protected]>", "Melon <[email protected]>", "MELON <[email protected]>", }, ) mailmap.update_from_git() assert [record.canonical for record in mailmap.records] == [ "Kiwi Fruit <[email protected]>", "MELON <[email protected]>", ]
[docs] def test_update_from_git_raises_on_a_git_failure(monkeypatch): """A failing git call reaches the caller with its message.""" def fail() -> set[str]: msg = "fatal: not a git repository" raise RuntimeError(msg) monkeypatch.setattr("repomatic.mailmap.list_contributor_identities", fail) with pytest.raises(RuntimeError, match="not a git repository"): Mailmap().update_from_git()