Skip to content

Commit

Permalink
fix(verify-git-email): only fail if no email address matches, don't f…
Browse files Browse the repository at this point in the history
…ail eagerly on first
  • Loading branch information
lorenzwalthert committed Aug 21, 2023
1 parent cab2aa9 commit 06e9fc5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
11 changes: 7 additions & 4 deletions pep_pre_commit_hooks/verify_git_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ def verify_git_email(domains: str = _default_domains) -> None:
raise ValueError("`domains` is required") # noqa: EM101, TRY003
domains_ = domains.split(",")
command = ("git", "config", "--get", "user.email")
matched = 0
for domain in domains_:
output = subprocess.check_output(command).decode().strip()
if re.search(f".*@{re.escape(domain)}$", output):
return
raise DomainMisconfiguredError(command=command, output=output, domain=domain)
matched += 1

if not matched:
raise DomainMisconfiguredError(command=command, output=output, domains=domains_)


class DomainMisconfiguredError(Exception):
Expand All @@ -33,11 +36,11 @@ def __init__(
self: "DomainMisconfiguredError",
command: tuple[str, ...],
output: str,
domain: str,
domains: list[str],
) -> None:
"""Initiate the error with input command and output as well as the expected domain."""
msg = (
f"`{' '.join(command)}` returned {output}, "
f"but an email address matching `{domain}` was expected."
f"but an email address matching one of `{domains}` was expected."
)
super().__init__(msg)
2 changes: 1 addition & 1 deletion tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_not_correctly_configured_email():
assert isinstance(result.exception, verify_git_email.DomainMisconfiguredError)
expected_msg = (
"`git config --get user.email` returned [email protected], "
"but an email address matching `gmail.com` was expected.",
"but an email address matching one of `['gmail.com']` was expected.",
)
assert result.exception.args == expected_msg

Expand Down
1 change: 1 addition & 0 deletions tests/verify_git_email_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def test_verify_domain():
assert verify_git_email.verify_git_email("icloud.com") is None
# multiple domains
assert verify_git_email.verify_git_email("icloud.com,gmail.com") is None
assert verify_git_email.verify_git_email("gmail.com,icloud.com") is None


@pytest.mark.usefixtures("_ch_tempdir", "_git_init", "_git_config_icloud_email")
Expand Down

0 comments on commit 06e9fc5

Please sign in to comment.