From d11ee9648c92b6e49cecb9094efc58bf396df424 Mon Sep 17 00:00:00 2001 From: Santiago Fraire Date: 2023年4月25日 15:58:44 +0200 Subject: [PATCH 1/2] test: add test for major_version_zero bug --- tests/commands/test_bump_command.py | 44 +++++++++++++++++ tests/conftest.py | 77 +++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index 59a0e01ef3..f11a485db4 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -980,3 +980,47 @@ def test_bump_command_prelease_version_type_check_old_tags( for version_file in [tmp_version_file, tmp_commitizen_cfg_file]: with open(version_file, "r") as f: assert "0.2.0" in f.read() + + +@pytest.mark.usefixtures("tmp_commitizen_project") +@pytest.mark.usefixtures("use_cz_semver") +@pytest.mark.parametrize( + "message, expected_tag", + [ + ("minor: add users", "0.2.0"), + ("patch: bug affecting users", "0.1.1"), + ("major: bug affecting users", "1.0.0"), + ], +) +def test_bump_with_plugin(mocker: MockFixture, message: str, expected_tag: str): + create_file_and_commit(message) + + testargs = ["cz", "--name", "cz_semver", "bump", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + tag_exists = git.tag_exist(expected_tag) + assert tag_exists is True + + +@pytest.mark.usefixtures("tmp_commitizen_project") +@pytest.mark.usefixtures("use_cz_semver") +@pytest.mark.parametrize( + "message, expected_tag", + [ + ("minor: add users", "0.2.0"), + ("patch: bug affecting users", "0.1.1"), + ("major: bug affecting users", "0.2.0"), + ], +) +def test_bump_with_major_version_zero_with_plugin( + mocker: MockFixture, message: str, expected_tag: str +): + create_file_and_commit(message) + + testargs = ["cz", "--name", "cz_semver", "bump", "--yes", "--major-version-zero"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + tag_exists = git.tag_exist(expected_tag) + assert tag_exists is True diff --git a/tests/conftest.py b/tests/conftest.py index 4daae7b80b..29f17caafb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,6 +8,8 @@ from commitizen import cmd, defaults from commitizen.config import BaseConfig +from commitizen.cz.base import BaseCommitizen +from commitizen.cz import registry from tests.utils import create_file_and_commit SIGNER = "GitHub Action" @@ -122,3 +124,78 @@ def config(): @pytest.fixture() def config_path() -> str: return os.path.join(os.getcwd(), "pyproject.toml") + + +class SemverCommitizen(BaseCommitizen): + """A minimal cz rules used to test changelog and bump. + + Samples: + ``` + minor(users): add email to user + major: removed user profile + patch(deps): updated dependency for security + ``` + """ + + bump_pattern = r"^(patch|minor|major)" + bump_map = { + "major": "MAJOR", + "minor": "MINOR", + "patch": "PATCH", + } + # bump_map_major_version_zero = { + # "major": "MINOR", + # "minor": "MINOR", + # "patch": "PATCH", + # } + changelog_pattern = r"^(patch|minor|major)" + commit_parser = r"^(?Ppatch|minor|major)(?:\((?P[^()\r\n]*)\)|\()?:?\s(?P.+)" # noqa + change_type_map = { + "major": "Breaking Changes", + "minor": "Features", + "patch": "Bugs", + } + + def questions(self) -> list: + return [ + { + "type": "list", + "name": "prefix", + "message": "Select the type of change you are committing", + "choices": [ + { + "value": "patch", + "name": "patch: a bug fix", + "key": "p", + }, + { + "value": "minor", + "name": "minor: a new feature, non-breaking", + "key": "m", + }, + { + "value": "major", + "name": "major: a breaking change", + "key": "b", + }, + ], + }, + { + "type": "input", + "name": "subject", + "message": ( + "Write a short and imperative summary of the code changes: (lower case and no period)\n" + ), + }, + ] + + def message(self, answers: dict) -> str: + prefix = answers["prefix"] + subject = answers.get("subject", "default message").trim() + return f"{prefix}: {subject}" + + +@pytest.fixture() +def use_cz_semver(mocker): + new_cz = {**registry, "cz_semver": SemverCommitizen} + mocker.patch.dict("commitizen.cz.registry", new_cz) From 6096b680561b94e188a70edfd61c318524f4e3aa Mon Sep 17 00:00:00 2001 From: Santiago Fraire Date: 2023年4月25日 15:59:40 +0200 Subject: [PATCH 2/2] feat: make `major_version_zero` customizable by third parties before this commit, major_version_zero would only work with conventional commits, as the value used was not exposed to other cz rules --- commitizen/commands/bump.py | 15 ++++++++++----- commitizen/cz/base.py | 1 + .../conventional_commits/conventional_commits.py | 1 + commitizen/cz/customize/customize.py | 7 +++++++ commitizen/defaults.py | 1 + tests/conftest.py | 10 +++++----- 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index 1477f79449..53e194bc6f 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -5,7 +5,7 @@ import questionary from packaging.version import InvalidVersion, Version -from commitizen import bump, cmd, defaults, factory, git, hooks, out, version_types +from commitizen import bump, cmd, factory, git, hooks, out, version_types from commitizen.commands.changelog import Changelog from commitizen.config import BaseConfig from commitizen.exceptions import ( @@ -86,8 +86,16 @@ def is_initial_tag(self, current_tag_version: str, is_yes: bool = False) -> bool return is_initial def find_increment(self, commits: List[git.GitCommit]) -> Optional[str]: + # Update the bump map to ensure major version doesn't increment. + is_major_version_zero: bool = self.bump_settings["major_version_zero"] + # self.cz.bump_map = defaults.bump_map_major_version_zero + bump_map = ( + self.cz.bump_map_major_version_zero + if is_major_version_zero + else self.cz.bump_map + ) bump_pattern = self.cz.bump_pattern - bump_map = self.cz.bump_map + if not bump_map or not bump_pattern: raise NoPatternMapError( f"'{self.config.settings['name']}' rule does not support bump" @@ -153,9 +161,6 @@ def __call__(self): # noqa: C901 f"--major-version-zero is meaningless for current version {current_version}" ) - # Update the bump map to ensure major version doesn't increment. - self.cz.bump_map = defaults.bump_map_major_version_zero - current_tag_version: str = bump.normalize_tag( current_version, tag_format=tag_format, diff --git a/commitizen/cz/base.py b/commitizen/cz/base.py index a5abe35f16..d24a2254f8 100644 --- a/commitizen/cz/base.py +++ b/commitizen/cz/base.py @@ -11,6 +11,7 @@ class BaseCommitizen(metaclass=ABCMeta): bump_pattern: Optional[str] = None bump_map: Optional[Dict[str, str]] = None + bump_map_major_version_zero: Optional[Dict[str, str]] = None default_style_config: List[Tuple[str, str]] = [ ("qmark", "fg:#ff9d00 bold"), ("question", "bold"), diff --git a/commitizen/cz/conventional_commits/conventional_commits.py b/commitizen/cz/conventional_commits/conventional_commits.py index 7989a17122..58026a72f3 100644 --- a/commitizen/cz/conventional_commits/conventional_commits.py +++ b/commitizen/cz/conventional_commits/conventional_commits.py @@ -30,6 +30,7 @@ def parse_subject(text): class ConventionalCommitsCz(BaseCommitizen): bump_pattern = defaults.bump_pattern bump_map = defaults.bump_map + bump_map_major_version_zero = defaults.bump_map_major_version_zero commit_parser = defaults.commit_parser version_parser = defaults.version_parser change_type_map = { diff --git a/commitizen/cz/customize/customize.py b/commitizen/cz/customize/customize.py index 994c21c714..3eac0a1df5 100644 --- a/commitizen/cz/customize/customize.py +++ b/commitizen/cz/customize/customize.py @@ -17,6 +17,7 @@ class CustomizeCommitsCz(BaseCommitizen): bump_pattern = defaults.bump_pattern bump_map = defaults.bump_map + bump_map_major_version_zero = defaults.bump_map_major_version_zero change_type_order = defaults.change_type_order def __init__(self, config: BaseConfig): @@ -34,6 +35,12 @@ def __init__(self, config: BaseConfig): if custom_bump_map: self.bump_map = custom_bump_map + custom_bump_map_major_version_zero = self.custom_settings.get( + "bump_map_major_version_zero" + ) + if custom_bump_map_major_version_zero: + self.bump_map_major_version_zero = custom_bump_map_major_version_zero + custom_change_type_order = self.custom_settings.get("change_type_order") if custom_change_type_order: self.change_type_order = custom_change_type_order diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 713cfc2a40..fb117547f8 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -15,6 +15,7 @@ class CzSettings(TypedDict, total=False): bump_pattern: str bump_map: "OrderedDict[str, str]" + bump_map_major_version_zero: "OrderedDict[str, str]" change_type_order: List[str] questions: Questions diff --git a/tests/conftest.py b/tests/conftest.py index 29f17caafb..e2177472db 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -143,11 +143,11 @@ class SemverCommitizen(BaseCommitizen): "minor": "MINOR", "patch": "PATCH", } - # bump_map_major_version_zero = { - # "major": "MINOR", - # "minor": "MINOR", - # "patch": "PATCH", - # } + bump_map_major_version_zero = { + "major": "MINOR", + "minor": "MINOR", + "patch": "PATCH", + } changelog_pattern = r"^(patch|minor|major)" commit_parser = r"^(?Ppatch|minor|major)(?:\((?P[^()\r\n]*)\)|\()?:?\s(?P.+)" # noqa change_type_map = {

AltStyle によって変換されたページ (->オリジナル) /