Skip to content

Commit 397a374

Browse files
committed
fix: skip unnecessary subprocess call in _should_skip_branch_validation
fix: add test for _resolve_current_author fallback path (coverage)
1 parent 4442f98 commit 397a374

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎commit_check/engine.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,10 @@ def _should_skip_branch_validation(self, context: ValidationContext) -> bool:
200200
or if no stdin_text and no commits exist.
201201
"""
202202
ignore_authors = context.config.get("branch", {}).get("ignore_authors", [])
203-
current_author = self._resolve_current_author(context)
204-
if current_author and current_author in ignore_authors:
205-
return True
203+
if ignore_authors:
204+
current_author = self._resolve_current_author(context)
205+
if current_author and current_author in ignore_authors:
206+
return True
206207
return context.stdin_text is None and not has_commits()
207208

208209
def _print_failure(self, actual_value: str, regex_or_constraint: str = "") -> None:

‎tests/engine_test.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,38 @@ def test_author_in_ignore_list_uses_commit_author_when_no_stdin(self):
13631363
# Skipped — the commit's author (dependabot[bot]) is in ignore_authors
13641364
assert result == ValidationResult.PASS
13651365

1366+
@pytest.mark.benchmark
1367+
def test_author_in_ignore_list_falls_back_to_git_config_when_commit_info_empty(
1368+
self,
1369+
):
1370+
"""
1371+
Coverage guard: when no stdin/commit_file and get_commit_info("an")
1372+
returns empty, _resolve_current_author must fall back to
1373+
get_git_config_value("user.name").
1374+
"""
1375+
rule = ValidationRule(
1376+
check="message",
1377+
regex=CONVENTIONAL_COMMIT_REGEX,
1378+
error=BAD_COMMIT_MSG,
1379+
suggest=USE_CONVENTIONAL_FORMAT,
1380+
)
1381+
validator = CommitMessageValidator(rule)
1382+
1383+
config = {"commit": {"ignore_authors": ["Developer Bot"]}}
1384+
context = ValidationContext(config=config)
1385+
1386+
with (
1387+
patch("commit_check.engine.has_commits", return_value=True),
1388+
patch("commit_check.engine.get_commit_info", return_value=""),
1389+
patch(
1390+
"commit_check.engine.get_git_config_value",
1391+
return_value="Developer Bot",
1392+
),
1393+
):
1394+
result = validator.validate(context)
1395+
# Skipped — fallback author (Developer Bot) is in ignore_authors
1396+
assert result == ValidationResult.PASS
1397+
13661398

13671399
class TestGetGitConfigValue:
13681400
"""Tests for the AuthorValidator using git config (Issue #298)."""

0 commit comments

Comments
 (0)