|
5 | 5 | import pytest |
6 | 6 |
|
7 | 7 | from git_commit_guard import ( |
| 8 | + MAX_SUBJECT_LEN, |
8 | 9 | TYPES, |
9 | 10 | Result, |
10 | 11 | _download_if_missing, |
|
14 | 15 | _parse_checks, |
15 | 16 | _parse_config_checks, |
16 | 17 | _report, |
| 18 | + _resolve_max_subject_length, |
17 | 19 | _resolve_types, |
18 | 20 | _strip_comments, |
19 | 21 | check_body, |
@@ -148,6 +150,16 @@ def test_empty_allowlist_accepts_any_scope(self): |
148 | 150 | check_subject("fix(anything): add token", r, allowed_scopes=frozenset()) |
149 | 151 | assert r.ok |
150 | 152 |
|
| 153 | + def test_custom_max_length_enforced(self): |
| 154 | + r = Result() |
| 155 | + check_subject("fix: add thing", r, max_subject_length=10) |
| 156 | + assert not r.ok |
| 157 | + |
| 158 | + def test_custom_max_length_passes(self): |
| 159 | + r = Result() |
| 160 | + check_subject("fix: ok", r, max_subject_length=10) |
| 161 | + assert r.ok |
| 162 | + |
151 | 163 | def test_custom_type_passes(self): |
152 | 164 | r = Result() |
153 | 165 | check_subject("wip: add thing", r, allowed_types=frozenset(["wip"])) |
@@ -420,6 +432,28 @@ def test_invalid_check_name_exits(self): |
420 | 432 | _parse_config_checks({"disable": ["bogus"]}, "disable") |
421 | 433 |
|
422 | 434 |
|
| 435 | +class TestResolveMaxSubjectLength: |
| 436 | + def test_defaults_when_no_config_or_flag(self): |
| 437 | + result = _resolve_max_subject_length(Namespace(max_subject_length=None), {}) |
| 438 | + assert result == MAX_SUBJECT_LEN |
| 439 | + |
| 440 | + def test_cli_flag_overrides_default(self): |
| 441 | + result = _resolve_max_subject_length(Namespace(max_subject_length=50), {}) |
| 442 | + assert result == 50 # noqa: PLR2004 Magic value used in comparison, consider replacing 50 with a constant variable |
| 443 | + |
| 444 | + def test_config_overrides_default(self): |
| 445 | + result = _resolve_max_subject_length( |
| 446 | + Namespace(max_subject_length=None), {"max-subject-length": 60} |
| 447 | + ) |
| 448 | + assert result == 60 # noqa: PLR2004 Magic value used in comparison, consider replacing 60 with a constant variable |
| 449 | + |
| 450 | + def test_cli_overrides_config(self): |
| 451 | + result = _resolve_max_subject_length( |
| 452 | + Namespace(max_subject_length=50), {"max-subject-length": 60} |
| 453 | + ) |
| 454 | + assert result == 50 # noqa: PLR2004 Magic value used in comparison, consider replacing 50 with a constant variable |
| 455 | + |
| 456 | + |
423 | 457 | class TestResolveTypes: |
424 | 458 | def test_defaults_when_no_config_or_flag(self): |
425 | 459 | assert _resolve_types(Namespace(types=None), {}) == TYPES |
@@ -713,6 +747,70 @@ def test_types_from_config(self, tmp_path): |
713 | 747 | ): |
714 | 748 | assert main() == 0 |
715 | 749 |
|
| 750 | + def test_max_subject_length_flag_passes(self, tmp_path): |
| 751 | + f = tmp_path / "msg" |
| 752 | + f.write_text("fix: ok\n\nbody\n\nSigned-off-by: A User <a@b.com>") |
| 753 | + argv = [ |
| 754 | + "cg", |
| 755 | + "--message-file", |
| 756 | + str(f), |
| 757 | + "--disable", |
| 758 | + "signature", |
| 759 | + "--max-subject-length", |
| 760 | + "10", |
| 761 | + ] |
| 762 | + with patch("sys.argv", argv): |
| 763 | + assert main() == 0 |
| 764 | + |
| 765 | + def test_max_subject_length_flag_fails(self, tmp_path): |
| 766 | + f = tmp_path / "msg" |
| 767 | + f.write_text(_VALID_MSG) |
| 768 | + argv = [ |
| 769 | + "cg", |
| 770 | + "--message-file", |
| 771 | + str(f), |
| 772 | + "--disable", |
| 773 | + "signature", |
| 774 | + "--max-subject-length", |
| 775 | + "5", |
| 776 | + ] |
| 777 | + with patch("sys.argv", argv): |
| 778 | + assert main() == 1 |
| 779 | + |
| 780 | + def test_max_subject_length_from_config(self, tmp_path): |
| 781 | + f = tmp_path / "msg" |
| 782 | + f.write_text(_VALID_MSG) |
| 783 | + argv = ["cg", "--message-file", str(f), "--disable", "signature"] |
| 784 | + with ( |
| 785 | + patch("sys.argv", argv), |
| 786 | + patch( |
| 787 | + "git_commit_guard._load_config", |
| 788 | + return_value={"max-subject-length": 5}, |
| 789 | + ), |
| 790 | + ): |
| 791 | + assert main() == 1 |
| 792 | + |
| 793 | + def test_max_subject_length_cli_overrides_config(self, tmp_path): |
| 794 | + f = tmp_path / "msg" |
| 795 | + f.write_text(_VALID_MSG) |
| 796 | + argv = [ |
| 797 | + "cg", |
| 798 | + "--message-file", |
| 799 | + str(f), |
| 800 | + "--disable", |
| 801 | + "signature", |
| 802 | + "--max-subject-length", |
| 803 | + "100", |
| 804 | + ] |
| 805 | + with ( |
| 806 | + patch("sys.argv", argv), |
| 807 | + patch( |
| 808 | + "git_commit_guard._load_config", |
| 809 | + return_value={"max-subject-length": 5}, |
| 810 | + ), |
| 811 | + ): |
| 812 | + assert main() == 0 |
| 813 | + |
716 | 814 | def test_types_cli_overrides_config(self, tmp_path): |
717 | 815 | f = tmp_path / "msg" |
718 | 816 | f.write_text("wip: add thing\n\nbody\n\nSigned-off-by: A User <a@b.com>") |
|
0 commit comments