| 1 |
import pytest |
| 2 |
|
| 3 |
from commitizen.cz.conventional_commits.conventional_commits import ( |
| 4 |
ConventionalCommitsCz, |
| 5 |
_parse_scope, |
| 6 |
_parse_subject, |
| 7 |
) |
| 8 |
from commitizen.cz.exceptions import AnswerRequiredError |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.parametrize( |
| 12 |
"valid_scope", ["", "simple", "dash-separated", "camelCaseUPPERCASE"] |
| 13 |
) |
| 14 |
def test_parse_scope_valid_values(valid_scope): |
| 15 |
assert valid_scope == _parse_scope(valid_scope) |
| 16 |
|
| 17 |
|
| 18 |
@pytest.mark.parametrize( |
| 19 |
"scopes_transformation", [["with spaces", "with-spaces"], ["", ""]] |
| 20 |
) |
| 21 |
def test_scopes_transformations(scopes_transformation): |
| 22 |
invalid_scope, transformed_scope = scopes_transformation |
| 23 |
assert transformed_scope == _parse_scope(invalid_scope) |
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.parametrize("valid_subject", ["this is a normal text", "aword"]) |
| 27 |
def test_parse_subject_valid_values(valid_subject): |
| 28 |
assert valid_subject == _parse_subject(valid_subject) |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.parametrize("invalid_subject", ["", " ", ".", " .", "\t\t."]) |
| 32 |
def test_parse_subject_invalid_values(invalid_subject): |
| 33 |
with pytest.raises(AnswerRequiredError): |
| 34 |
_parse_subject(invalid_subject) |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.parametrize("subject_transformation", [["with dot.", "with dot"]]) |
| 38 |
def test_subject_transformations(subject_transformation): |
| 39 |
invalid_subject, transformed_subject = subject_transformation |
| 40 |
assert transformed_subject == _parse_subject(invalid_subject) |
| 41 |
|
| 42 |
|
| 43 |
def test_questions(config): |
| 44 |
conventional_commits = ConventionalCommitsCz(config) |
| 45 |
questions = conventional_commits.questions() |
| 46 |
assert isinstance(questions, list) |
| 47 |
assert isinstance(questions[0], dict) |
| 48 |
|
| 49 |
|
| 50 |
def test_choices_all_have_keyboard_shortcuts(config): |
| 51 |
conventional_commits = ConventionalCommitsCz(config) |
| 52 |
questions = conventional_commits.questions() |
| 53 |
|
| 54 |
list_questions = (q for q in questions if q["type"] == "list") |
| 55 |
for select in list_questions: |
| 56 |
assert all("key" in choice for choice in select["choices"]) |
| 57 |
|
| 58 |
|
| 59 |
def test_small_answer(config): |
| 60 |
conventional_commits = ConventionalCommitsCz(config) |
| 61 |
answers = { |
| 62 |
"prefix": "fix", |
| 63 |
"scope": "users", |
| 64 |
"subject": "email pattern corrected", |
| 65 |
"is_breaking_change": False, |
| 66 |
"body": "", |
| 67 |
"footer": "", |
| 68 |
} |
| 69 |
message = conventional_commits.message(answers) |
| 70 |
assert message == "fix(users): email pattern corrected" |
| 71 |
|
| 72 |
|
| 73 |
def test_long_answer(config): |
| 74 |
conventional_commits = ConventionalCommitsCz(config) |
| 75 |
answers = { |
| 76 |
"prefix": "fix", |
| 77 |
"scope": "users", |
| 78 |
"subject": "email pattern corrected", |
| 79 |
"is_breaking_change": False, |
| 80 |
"body": "complete content", |
| 81 |
"footer": "closes #24", |
| 82 |
} |
| 83 |
message = conventional_commits.message(answers) |
| 84 |
assert ( |
| 85 |
message |
| 86 |
== "fix(users): email pattern corrected\n\ncomplete content\n\ncloses #24" |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
def test_breaking_change_in_footer(config): |
| 91 |
conventional_commits = ConventionalCommitsCz(config) |
| 92 |
answers = { |
| 93 |
"prefix": "fix", |
| 94 |
"scope": "users", |
| 95 |
"subject": "email pattern corrected", |
| 96 |
"is_breaking_change": True, |
| 97 |
"body": "complete content", |
| 98 |
"footer": "migrate by renaming user to users", |
| 99 |
} |
| 100 |
message = conventional_commits.message(answers) |
| 101 |
print(message) |
| 102 |
assert ( |
| 103 |
message |
| 104 |
== "fix(users): email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users" |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize( |
| 109 |
("scope", "breaking_change_exclamation_in_title", "expected_message"), |
| 110 |
[ |
| 111 |
# Test with scope and breaking_change_exclamation_in_title enabled |
| 112 |
( |
| 113 |
"users", |
| 114 |
True, |
| 115 |
"feat(users)!: email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users", |
| 116 |
), |
| 117 |
# Test without scope and breaking_change_exclamation_in_title enabled |
| 118 |
( |
| 119 |
"", |
| 120 |
True, |
| 121 |
"feat!: email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users", |
| 122 |
), |
| 123 |
# Test with scope and breaking_change_exclamation_in_title disabled |
| 124 |
( |
| 125 |
"users", |
| 126 |
False, |
| 127 |
"feat(users): email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users", |
| 128 |
), |
| 129 |
# Test without scope and breaking_change_exclamation_in_title disabled |
| 130 |
( |
| 131 |
"", |
| 132 |
False, |
| 133 |
"feat: email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users", |
| 134 |
), |
| 135 |
], |
| 136 |
) |
| 137 |
def test_breaking_change_message_formats( |
| 138 |
config, scope, breaking_change_exclamation_in_title, expected_message |
| 139 |
): |
| 140 |
# Set the breaking_change_exclamation_in_title setting |
| 141 |
config.settings["breaking_change_exclamation_in_title"] = ( |
| 142 |
breaking_change_exclamation_in_title |
| 143 |
) |
| 144 |
conventional_commits = ConventionalCommitsCz(config) |
| 145 |
answers = { |
| 146 |
"prefix": "feat", |
| 147 |
"scope": scope, |
| 148 |
"subject": "email pattern corrected", |
| 149 |
"is_breaking_change": True, |
| 150 |
"body": "complete content", |
| 151 |
"footer": "migrate by renaming user to users", |
| 152 |
} |
| 153 |
message = conventional_commits.message(answers) |
| 154 |
assert message == expected_message |
| 155 |
|
| 156 |
|
| 157 |
def test_example(config): |
| 158 |
"""just testing a string is returned. not the content""" |
| 159 |
conventional_commits = ConventionalCommitsCz(config) |
| 160 |
example = conventional_commits.example() |
| 161 |
assert isinstance(example, str) |
| 162 |
|
| 163 |
|
| 164 |
def test_schema(config): |
| 165 |
"""just testing a string is returned. not the content""" |
| 166 |
conventional_commits = ConventionalCommitsCz(config) |
| 167 |
schema = conventional_commits.schema() |
| 168 |
assert isinstance(schema, str) |
| 169 |
|
| 170 |
|
| 171 |
def test_info(config): |
| 172 |
"""just testing a string is returned. not the content""" |
| 173 |
conventional_commits = ConventionalCommitsCz(config) |
| 174 |
info = conventional_commits.info() |
| 175 |
assert isinstance(info, str) |