|
| 1 | +"""Tests for the ForbiddenTypeScope gitlint rule.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from gitlint_rules.forbidden_type_scope import ForbiddenTypeScope |
| 6 | +from gitlint.rules import RuleViolation |
| 7 | + |
| 8 | + |
| 9 | +class FakeCommit: |
| 10 | + """Minimal stand-in for a gitlint commit object.""" |
| 11 | + |
| 12 | + def __init__(self, title): |
| 13 | + self.message = type("msg", (), {"title": title})() |
| 14 | + |
| 15 | + |
| 16 | +def run_rule(title): |
| 17 | + """Run ForbiddenTypeScope against a commit title and return violations.""" |
| 18 | + rule = ForbiddenTypeScope() |
| 19 | + commit = FakeCommit(title) |
| 20 | + return rule.validate(commit) |
| 21 | + |
| 22 | + |
| 23 | +# --- should be rejected --- |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize( |
| 27 | + "title", |
| 28 | + [ |
| 29 | + "fix(ci): update workflow", |
| 30 | + "feat(ci): add new job", |
| 31 | + "fix(e2e): repair test", |
| 32 | + "feat(e2e): add new test", |
| 33 | + ], |
| 34 | +) |
| 35 | +def test_rejects_forbidden_combinations(title): |
| 36 | + violations = run_rule(title) |
| 37 | + assert violations, f"Expected violation for {title!r}" |
| 38 | + assert len(violations) == 1 |
| 39 | + assert isinstance(violations[0], RuleViolation) |
| 40 | + |
| 41 | + |
| 42 | +def test_fix_ci_suggests_ci_subsystem(): |
| 43 | + violations = run_rule("fix(ci): update workflow") |
| 44 | + assert "ci(<subsystem>)" in violations[0].message.lower() |
| 45 | + |
| 46 | + |
| 47 | +def test_feat_e2e_suggests_ci_e2e(): |
| 48 | + violations = run_rule("feat(e2e): add new test") |
| 49 | + assert "ci(e2e)" in violations[0].message.lower() |
| 50 | + |
| 51 | + |
| 52 | +# --- should be allowed --- |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize( |
| 56 | + "title", |
| 57 | + [ |
| 58 | + "ci(lint): update linter config", |
| 59 | + "ci(e2e): fix flaky test", |
| 60 | + "fix(mint): correct token refresh", |
| 61 | + "feat(review-agent): add outcome labels", |
| 62 | + "chore(ci): bump action version", |
| 63 | + "test(e2e): add new scenario", |
| 64 | + "refactor(ci): simplify matrix", |
| 65 | + "docs: update readme", |
| 66 | + "fix(#123): handle nil pointer", |
| 67 | + ], |
| 68 | +) |
| 69 | +def test_allows_valid_combinations(title): |
| 70 | + violations = run_rule(title) |
| 71 | + assert not violations, f"Unexpected violation for {title!r}: {violations}" |
| 72 | + |
| 73 | + |
| 74 | +# --- should not crash on non-conventional titles --- |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize( |
| 78 | + "title", |
| 79 | + [ |
| 80 | + "just a plain message", |
| 81 | + "WIP", |
| 82 | + "", |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_ignores_non_conventional(title): |
| 86 | + violations = run_rule(title) |
| 87 | + assert not violations |
0 commit comments