|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from ops.lintcommit import validate_message, validate_subject |
| 4 | + |
| 5 | + |
| 6 | +# region validate_subject: valid subjects |
| 7 | + |
| 8 | + |
| 9 | +def test_valid_feat() -> None: |
| 10 | + assert validate_subject("feat: add new feature") is None |
| 11 | + |
| 12 | + |
| 13 | +def test_valid_fix() -> None: |
| 14 | + assert validate_subject("fix: resolve issue") is None |
| 15 | + |
| 16 | + |
| 17 | +def test_valid_fix_with_scope() -> None: |
| 18 | + assert validate_subject("fix(sdk): resolve issue") is None |
| 19 | + |
| 20 | + |
| 21 | +def test_valid_build() -> None: |
| 22 | + assert validate_subject("build: update build process") is None |
| 23 | + |
| 24 | + |
| 25 | +def test_valid_chore() -> None: |
| 26 | + assert validate_subject("chore: update dependencies") is None |
| 27 | + |
| 28 | + |
| 29 | +def test_valid_ci() -> None: |
| 30 | + assert validate_subject("ci: configure ci/cd") is None |
| 31 | + |
| 32 | + |
| 33 | +def test_valid_deps() -> None: |
| 34 | + assert validate_subject("deps: bump aws-sdk group with 5 updates") is None |
| 35 | + |
| 36 | + |
| 37 | +def test_valid_docs() -> None: |
| 38 | + assert validate_subject("docs: update documentation") is None |
| 39 | + |
| 40 | + |
| 41 | +def test_valid_feat_with_scope() -> None: |
| 42 | + assert validate_subject("feat(sdk): add new feature") is None |
| 43 | + |
| 44 | + |
| 45 | +def test_valid_feat_scope_bar() -> None: |
| 46 | + assert validate_subject("feat(sdk): bar") is None |
| 47 | + |
| 48 | + |
| 49 | +def test_valid_feat_foo() -> None: |
| 50 | + assert validate_subject("feat: foo") is None |
| 51 | + |
| 52 | + |
| 53 | +def test_valid_fix_foo() -> None: |
| 54 | + assert validate_subject("fix: foo") is None |
| 55 | + |
| 56 | + |
| 57 | +# region validate_subject: invalid subjects |
| 58 | + |
| 59 | + |
| 60 | +def test_invalid_type() -> None: |
| 61 | + assert validate_subject("config: foo") == 'invalid type "config"' |
| 62 | + |
| 63 | + |
| 64 | +def test_missing_colon() -> None: |
| 65 | + assert validate_subject("invalid title") == "missing colon (:) char" |
| 66 | + |
| 67 | + |
| 68 | +def test_period_at_end() -> None: |
| 69 | + assert validate_subject("feat: add thing.") == "subject must not end with a period" |
| 70 | + |
| 71 | + |
| 72 | +def test_empty_subject() -> None: |
| 73 | + assert validate_subject("feat: ") == "empty subject" |
| 74 | + |
| 75 | + |
| 76 | +def test_subject_too_long() -> None: |
| 77 | + long_subject: str = "feat: " + "a" * 51 |
| 78 | + result = validate_subject(long_subject) |
| 79 | + assert result is not None |
| 80 | + assert "invalid subject" in result |
| 81 | + |
| 82 | + |
| 83 | +def test_type_with_whitespace() -> None: |
| 84 | + assert validate_subject("fe at: foo") == 'type contains whitespace: "fe at"' |
| 85 | + |
| 86 | + |
| 87 | +def test_scope_not_closed() -> None: |
| 88 | + assert validate_subject("feat(sdk: foo") == "must be formatted like type(scope):" |
| 89 | + |
| 90 | + |
| 91 | +def test_scope_too_long() -> None: |
| 92 | + long_scope: str = "a" * 31 |
| 93 | + result = validate_subject(f"feat({long_scope}): foo") |
| 94 | + assert result is not None |
| 95 | + assert "invalid scope" in result |
| 96 | + |
| 97 | + |
| 98 | +def test_scope_uppercase() -> None: |
| 99 | + result = validate_subject("feat(SDK): foo") |
| 100 | + assert result is not None |
| 101 | + assert "invalid scope" in result |
| 102 | + |
| 103 | + |
| 104 | +def test_subject_uppercase() -> None: |
| 105 | + assert validate_subject("feat: Add new feature") == "subject must be lowercase" |
| 106 | + |
| 107 | + |
| 108 | +def test_subject_uppercase_acronym_rejected() -> None: |
| 109 | + assert validate_subject("ci: configure CI/CD") == "subject must be lowercase" |
| 110 | + |
| 111 | + |
| 112 | +# region validate_message |
| 113 | + |
| 114 | + |
| 115 | +def test_valid_subject_only() -> None: |
| 116 | + error, warnings = validate_message("feat: add thing") |
| 117 | + assert error is None |
| 118 | + assert warnings == [] |
| 119 | + |
| 120 | + |
| 121 | +def test_valid_with_body() -> None: |
| 122 | + error, warnings = validate_message("feat: add thing\n\nThis is the body.") |
| 123 | + assert error is None |
| 124 | + assert warnings == [] |
| 125 | + |
| 126 | + |
| 127 | +def test_missing_blank_line() -> None: |
| 128 | + _, warnings = validate_message("feat: add thing\nNo blank line.") |
| 129 | + assert "missing blank line between subject and body" in warnings |
| 130 | + |
| 131 | + |
| 132 | +def test_missing_blank_line_body_still_checked() -> None: |
| 133 | + _, warnings = validate_message("feat: add thing\n" + "x" * 80) |
| 134 | + assert "missing blank line between subject and body" in warnings |
| 135 | + assert any("exceeds 72 chars" in w for w in warnings), ( |
| 136 | + "body line length should be checked even without blank line" |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +def test_long_body_line() -> None: |
| 141 | + _, warnings = validate_message("feat: add thing\n\n" + "x" * 80) |
| 142 | + assert len(warnings) == 1 |
| 143 | + assert "exceeds 72 chars" in warnings[0] |
| 144 | + |
| 145 | + |
| 146 | +def test_empty_message() -> None: |
| 147 | + error, _ = validate_message("") |
| 148 | + assert error == "empty commit message" |
| 149 | + |
| 150 | + |
| 151 | +def test_invalid_subject_in_message() -> None: |
| 152 | + error, _ = validate_message("invalid title") |
| 153 | + assert error == "missing colon (:) char" |
0 commit comments