Skip to content

Commit 5b3d4db

Browse files
committed
refactor(conventional_commits): remove unnecessary checks
1 parent 49b596e commit 5b3d4db

File tree

2 files changed

+30
-46
lines changed

2 files changed

+30
-46
lines changed

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,12 @@
99
__all__ = ["ConventionalCommitsCz"]
1010

1111

12-
def parse_scope(text: str) -> str:
13-
if not text:
14-
return ""
15-
16-
scope = text.strip().split()
17-
if len(scope) == 1:
18-
return scope[0]
19-
20-
return "-".join(scope)
21-
12+
def _parse_scope(text: str) -> str:
13+
return "-".join(text.strip().split())
2214

23-
def parse_subject(text: str) -> str:
24-
if isinstance(text, str):
25-
text = text.strip(".").strip()
2615

27-
return required_validator(text, msg="Subject is required.")
16+
def _parse_subject(text: str) -> str:
17+
return required_validator(text.strip(".").strip(), msg="Subject is required.")
2818

2919

3020
class ConventionalCommitsCz(BaseCommitizen):
@@ -113,12 +103,12 @@ def questions(self) -> Questions:
113103
"message": (
114104
"What is the scope of this change? (class or file name): (press [enter] to skip)\n"
115105
),
116-
"filter": parse_scope,
106+
"filter": _parse_scope,
117107
},
118108
{
119109
"type": "input",
120110
"name": "subject",
121-
"filter": parse_subject,
111+
"filter": _parse_subject,
122112
"message": (
123113
"Write a short and imperative summary of the code changes: (lower case and no period)\n"
124114
),

tests/test_cz_conventional_commits.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,42 @@
22

33
from commitizen.cz.conventional_commits.conventional_commits import (
44
ConventionalCommitsCz,
5-
parse_scope,
6-
parse_subject,
5+
_parse_scope,
6+
_parse_subject,
77
)
88
from commitizen.cz.exceptions import AnswerRequiredError
99

10-
valid_scopes = ["", "simple", "dash-separated", "camelCaseUPPERCASE"]
1110

12-
scopes_transformations = [["with spaces", "with-spaces"], [None, ""]]
13-
14-
valid_subjects = ["this is a normal text", "aword"]
15-
16-
subjects_transformations = [["with dot.", "with dot"]]
17-
18-
invalid_subjects = ["", " ", ".", " .", "", None]
19-
20-
21-
def test_parse_scope_valid_values():
22-
for valid_scope in valid_scopes:
23-
assert valid_scope == parse_scope(valid_scope)
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)
2416

2517

26-
def test_scopes_transformations():
27-
for scopes_transformation in scopes_transformations:
28-
invalid_scope, transformed_scope = scopes_transformation
29-
assert transformed_scope == parse_scope(invalid_scope)
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)
3024

3125

32-
def test_parse_subject_valid_values():
33-
for valid_subject in valid_subjects:
34-
assert valid_subject == parse_subject(valid_subject)
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)
3529

3630

37-
def test_parse_subject_invalid_values():
38-
for valid_subject in invalid_subjects:
39-
with pytest.raises(AnswerRequiredError):
40-
parse_subject(valid_subject)
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)
4135

4236

43-
def test_subject_transformations():
44-
for subject_transformation in subjects_transformations:
45-
invalid_subject, transformed_subject = subject_transformation
46-
assert transformed_subject == parse_subject(invalid_subject)
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)
4741

4842

4943
def test_questions(config):

0 commit comments

Comments
 (0)