Skip to content

Commit f94ab4a

Browse files
committed
refactor(defaults): move bump_map, bump_pattern, commit_parser from defaults to ConventionalCommitsCz
These defaults are not defaults for other cz
1 parent 36b7617 commit f94ab4a

10 files changed

+41
-32
lines changed

commitizen/bump.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,13 @@
66

77
from packaging.version import Version
88

9-
from commitizen.defaults import (
10-
MAJOR,
11-
MINOR,
12-
PATCH,
13-
bump_map,
14-
bump_message,
15-
bump_pattern,
16-
)
9+
from commitizen.defaults import MAJOR, MINOR, PATCH, bump_message
1710
from commitizen.exceptions import CurrentVersionNotFoundError
1811
from commitizen.git import GitCommit
1912

2013

2114
def find_increment(
22-
commits: List[GitCommit],
23-
regex: str = bump_pattern,
24-
increments_map: Union[dict, OrderedDict] = bump_map,
15+
commits: List[GitCommit], regex: str, increments_map: Union[dict, OrderedDict]
2516
) -> Optional[str]:
2617

2718
if isinstance(increments_map, dict):

commitizen/changelog.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def generate_tree_from_commits(
6969
commits: List[GitCommit],
7070
tags: List[GitTag],
7171
commit_parser: str,
72-
changelog_pattern: str = defaults.bump_pattern,
72+
changelog_pattern: str,
7373
unreleased_version: Optional[str] = None,
7474
change_type_map: Optional[Dict[str, str]] = None,
7575
changelog_message_builder_hook: Optional[Callable] = None,

commitizen/cz/conventional_commits/conventional_commits.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
from collections import OrderedDict
34
from typing import Any, Dict, List
45

56
from commitizen import defaults
@@ -28,10 +29,19 @@ def parse_subject(text):
2829

2930

3031
class ConventionalCommitsCz(BaseCommitizen):
31-
bump_pattern = defaults.bump_pattern
32-
bump_map = defaults.bump_map
33-
commit_parser = defaults.commit_parser
34-
changelog_pattern = defaults.bump_pattern
32+
bump_pattern = r"^(BREAKING[\-\ ]CHANGE|feat|fix|refactor|perf)(\(.+\))?(!)?"
33+
bump_map = OrderedDict(
34+
(
35+
(r"^.+!$", defaults.MAJOR),
36+
(r"^BREAKING[\-\ ]CHANGE", defaults.MAJOR),
37+
(r"^feat", defaults.MINOR),
38+
(r"^fix", defaults.PATCH),
39+
(r"^refactor", defaults.PATCH),
40+
(r"^perf", defaults.PATCH),
41+
)
42+
)
43+
commit_parser = r"^(?P<change_type>feat|fix|refactor|perf|BREAKING CHANGE)(?:\((?P<scope>[^()\r\n]*)\)|\()?(?P<breaking>!)?:\s(?P<message>.*)?" # noqa
44+
version_parser = defaults.version_parser
3545
change_type_map = {
3646
"feat": "Feat",
3747
"fix": "Fix",

tests/commands/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@pytest.fixture()
1010
def config():
1111
_config = BaseConfig()
12-
_config.settings.update({"name": defaults.name})
12+
_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
1313
return _config
1414

1515

tests/test_bump_find_increment.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from commitizen import bump
8+
from commitizen.cz import ConventionalCommitsCz
89
from commitizen.git import GitCommit
910

1011
NONE_INCREMENT_CC = ["docs(README): motivation", "ci: added travis"]
@@ -72,7 +73,11 @@
7273
)
7374
def test_find_increment(messages, expected_type):
7475
commits = [GitCommit(rev="test", title=message) for message in messages]
75-
increment_type = bump.find_increment(commits)
76+
increment_type = bump.find_increment(
77+
commits,
78+
regex=ConventionalCommitsCz.bump_pattern,
79+
increments_map=ConventionalCommitsCz.bump_map,
80+
)
7681
assert increment_type == expected_type
7782

7883

tests/test_changelog.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import pytest
22

33
from commitizen import changelog, defaults, git
4+
from commitizen.cz.conventional_commits.conventional_commits import (
5+
ConventionalCommitsCz,
6+
)
47
from commitizen.exceptions import InvalidConfigurationError
58

69
COMMITS_DATA = [
@@ -841,8 +844,8 @@ def test_order_changelog_tree_raises():
841844

842845

843846
def test_render_changelog(gitcommits, tags, changelog_content):
844-
parser = defaults.commit_parser
845-
changelog_pattern = defaults.bump_pattern
847+
parser = ConventionalCommitsCz.commit_parser
848+
changelog_pattern = ConventionalCommitsCz.bump_pattern
846849
tree = changelog.generate_tree_from_commits(
847850
gitcommits, tags, parser, changelog_pattern
848851
)
@@ -852,8 +855,8 @@ def test_render_changelog(gitcommits, tags, changelog_content):
852855

853856
def test_render_changelog_unreleased(gitcommits):
854857
some_commits = gitcommits[:7]
855-
parser = defaults.commit_parser
856-
changelog_pattern = defaults.bump_pattern
858+
parser = ConventionalCommitsCz.commit_parser
859+
changelog_pattern = ConventionalCommitsCz.bump_pattern
857860
tree = changelog.generate_tree_from_commits(
858861
some_commits, [], parser, changelog_pattern
859862
)
@@ -867,8 +870,8 @@ def test_render_changelog_tag_and_unreleased(gitcommits, tags):
867870
tag for tag in tags if tag.rev == "56c8a8da84e42b526bcbe130bd194306f7c7e813"
868871
]
869872

870-
parser = defaults.commit_parser
871-
changelog_pattern = defaults.bump_pattern
873+
parser = ConventionalCommitsCz.commit_parser
874+
changelog_pattern = ConventionalCommitsCz.bump_pattern
872875
tree = changelog.generate_tree_from_commits(
873876
some_commits, single_tag, parser, changelog_pattern
874877
)
@@ -881,8 +884,8 @@ def test_render_changelog_tag_and_unreleased(gitcommits, tags):
881884
def test_render_changelog_with_change_type(gitcommits, tags):
882885
new_title = ":some-emoji: feature"
883886
change_type_map = {"feat": new_title}
884-
parser = defaults.commit_parser
885-
changelog_pattern = defaults.bump_pattern
887+
parser = ConventionalCommitsCz.commit_parser
888+
changelog_pattern = ConventionalCommitsCz.bump_pattern
886889
tree = changelog.generate_tree_from_commits(
887890
gitcommits, tags, parser, changelog_pattern, change_type_map=change_type_map
888891
)
@@ -897,8 +900,8 @@ def changelog_message_builder_hook(message: dict, commit: git.GitCommit) -> dict
897900
] = f"{message['message']} [link](github.com/232323232) {commit.author} {commit.author_email}"
898901
return message
899902

900-
parser = defaults.commit_parser
901-
changelog_pattern = defaults.bump_pattern
903+
parser = ConventionalCommitsCz.commit_parser
904+
changelog_pattern = ConventionalCommitsCz.bump_pattern
902905
tree = changelog.generate_tree_from_commits(
903906
gitcommits,
904907
tags,

tests/test_cz_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@pytest.fixture()
99
def config():
1010
_config = BaseConfig()
11-
_config.settings.update({"name": defaults.name})
11+
_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
1212
return _config
1313

1414

tests/test_cz_conventional_commits.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
@pytest.fixture()
2424
def config():
2525
_config = BaseConfig()
26-
_config._settings["name"] = defaults.name
26+
_config._settings["name"] = defaults.DEFAULT_SETTINGS["name"]
2727
return _config
2828

2929

tests/test_cz_jira.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@pytest.fixture()
99
def config():
1010
_config = BaseConfig()
11-
_config.settings.update({"name": defaults.name})
11+
_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
1212
return _config
1313

1414

tests/test_factory.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
def test_factory():
99
config = BaseConfig()
10-
config.settings.update({"name": defaults.name})
10+
config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
1111
r = factory.commiter_factory(config)
1212
assert isinstance(r, BaseCommitizen)
1313

0 commit comments

Comments
 (0)