diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index cb34c41a50..78a48b9a4f 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -11,7 +11,14 @@ from commitizen import factory, git, out from commitizen.config import BaseConfig from commitizen.cz.exceptions import CzException -from commitizen.cz.utils import get_backup_file_path +from commitizen.cz.utils import ( + break_multiple_line, + get_backup_file_path, + required_validator, + required_validator_scope, + required_validator_subject_strip, + required_validator_title_strip, +) from commitizen.exceptions import ( CommitError, CommitMessageLengthExceededError, @@ -51,9 +58,27 @@ def read_backup_message(self) -> str | None: def prompt_commit_questions(self) -> str: # Prompt user for the commit message cz = self.cz - questions = cz.questions() + questions = [dict(question) for question in cz.questions()] + for question in filter(lambda q: q["type"] == "list", questions): question["use_shortcuts"] = self.config.settings["use_shortcuts"] + + for question in filter( + lambda q: isinstance(q.get("filter", None), str), questions + ): + if question["filter"] == "break_multiple_line": + question["filter"] = break_multiple_line + elif question["filter"] == "required_validator": + question["filter"] = required_validator + elif question["filter"] == "required_validator_scope": + question["filter"] = required_validator_scope + elif question["filter"] == "required_validator_subject_strip": + question["filter"] = required_validator_subject_strip + elif question["filter"] == "required_validator_title_strip": + question["filter"] = required_validator_title_strip + else: + raise NotAllowed(f"Unknown value filter: {question['filter']}") + try: answers = questionary.prompt(questions, style=cz.style) except ValueError as err: diff --git a/commitizen/cz/conventional_commits/conventional_commits.py b/commitizen/cz/conventional_commits/conventional_commits.py index af29a209fc..1e72db66ea 100644 --- a/commitizen/cz/conventional_commits/conventional_commits.py +++ b/commitizen/cz/conventional_commits/conventional_commits.py @@ -3,7 +3,7 @@ from commitizen import defaults from commitizen.cz.base import BaseCommitizen -from commitizen.cz.utils import multiple_line_breaker, required_validator +from commitizen.cz.utils import break_multiple_line, required_validator from commitizen.defaults import Questions __all__ = ["ConventionalCommitsCz"] @@ -129,7 +129,7 @@ def questions(self) -> Questions: "message": ( "Provide additional contextual information about the code changes: (press [enter] to skip)\n" ), - "filter": multiple_line_breaker, + "filter": break_multiple_line, }, { "type": "confirm", diff --git a/commitizen/cz/utils.py b/commitizen/cz/utils.py index 7bc89673c6..4a304f6c98 100644 --- a/commitizen/cz/utils.py +++ b/commitizen/cz/utils.py @@ -6,13 +6,34 @@ from commitizen.cz import exceptions -def required_validator(answer, msg=None): +def required_validator(answer: str, msg=None) -> str: if not answer: raise exceptions.AnswerRequiredError(msg) return answer -def multiple_line_breaker(answer, sep="|"): +def required_validator_scope( + answer: str, + msg: str = "! Error: Scope is required", +) -> str: + return required_validator(answer, msg) + + +def required_validator_subject_strip( + answer: str, + msg: str = "! Error: Subject is required", +) -> str: + return required_validator(answer.strip(".").strip(), msg) + + +def required_validator_title_strip( + answer: str, + msg: str = "! Error: Title is required", +) -> str: + return required_validator(answer.strip(".").strip(), msg) + + +def break_multiple_line(answer: str, sep: str = "|") -> str: return "\n".join(line.strip() for line in answer.split(sep) if line) diff --git a/docs/customization.md b/docs/customization.md index e97558a308..e17a09a4fe 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -110,13 +110,13 @@ And the correspondent example for a yaml file: commitizen: name: cz_customize customize: - message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}" + message_template: '{{change_type}}:{% if show_message %} {{message}}{% endif %}' example: 'feature: this feature enable customize through config file' - schema: ": " - schema_pattern: "(feature|bug fix):(\\s.*)" - bump_pattern: "^(break|new|fix|hotfix)" - commit_parser: "^(?Pfeature|bug fix):\\s(?P.*)?" - changelog_pattern: "^(feature|bug fix)?(!)?" + schema: ': ' + schema_pattern: '(feature|bug fix):(\\s.*)' + bump_pattern: '^(break|new|fix|hotfix)' + commit_parser: '^(?Pfeature|bug fix):\\s(?P.*)?' + changelog_pattern: '^(feature|bug fix)?(!)?' change_type_map: feature: Feat bug fix: Fix @@ -125,7 +125,7 @@ commitizen: new: MINOR fix: PATCH hotfix: PATCH - change_type_order: ["BREAKING CHANGE", "feat", "fix", "refactor", "perf"] + change_type_order: ['BREAKING CHANGE', 'feat', 'fix', 'refactor', 'perf'] info_path: cz_customize_info.txt info: This is customized info questions: @@ -168,17 +168,18 @@ commitizen: #### Detailed `questions` content -| Parameter | Type | Default | Description | -| ----------- | ------ | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `type` | `str` | `None` | The type of questions. Valid types: `list`, `select`, `input`, etc. The `select` type provides an interactive searchable list interface. [See More][different-question-types] | -| `name` | `str` | `None` | The key for the value answered by user. It's used in `message_template` | -| `message` | `str` | `None` | Detail description for the question. | -| `choices` | `list` | `None` | (OPTIONAL) The choices when `type = list` or `type = select`. Either use a list of values or a list of dictionaries with `name` and `value` keys. Keyboard shortcuts can be defined via `key`. See examples above. | -| `default` | `Any` | `None` | (OPTIONAL) The default value for this question. | -| `filter` | `str` | `None` | (OPTIONAL) Validator for user's answer. **(Work in Progress)** | -| `multiline` | `bool` | `False` | (OPTIONAL) Enable multiline support when `type = input`. | -| `use_search_filter` | `bool` | `False` | (OPTIONAL) Enable search/filter functionality for list/select type questions. This allows users to type and filter through the choices. | -| `use_jk_keys` | `bool` | `True` | (OPTIONAL) Enable/disable j/k keys for navigation in list/select type questions. Set to false if you prefer arrow keys only. | +| Parameter | Type | Default | Description | +| ------------------- | ------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `type` | `str` | `None` | The type of questions. Valid types: `list`, `select`, `input`, etc. The `select` type provides an interactive searchable list interface. [See More][different-question-types] | +| `name` | `str` | `None` | The key for the value answered by user. It's used in `message_template` | +| `message` | `str` | `None` | Detail description for the question. | +| `choices` | `list` | `None` | (OPTIONAL) The choices when `type = list` or `type = select`. Either use a list of values or a list of dictionaries with `name` and `value` keys. Keyboard shortcuts can be defined via `key`. See examples above. | +| `default` | `Any` | `None` | (OPTIONAL) The default value for this question. | +| `filter` | `str` | `None` | (OPTIONAL) Validator for user's answer. The string is the name of a `commitizen.cz.utils.NAME(answer...)` function like `break_multiple_line` | +| `multiline` | `bool` | `False` | (OPTIONAL) Enable multiline support when `type = input`. | +| `multiline` | `bool` | `False` | (OPTIONAL) Enable multiline support when `type = input`. | +| `use_search_filter` | `bool` | `False` | (OPTIONAL) Enable search/filter functionality for list/select type questions. This allows users to type and filter through the choices. | +| `use_jk_keys` | `bool` | `True` | (OPTIONAL) Enable/disable j/k keys for navigation in list/select type questions. Set to false if you prefer arrow keys only. | [different-question-types]: https://github.com/tmbo/questionary#different-question-types diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 3a92f5af48..1e8d8a6179 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -324,6 +324,68 @@ def test_commit_when_nothing_to_commit(config, mocker: MockFixture): assert "No files added to staging!" in str(excinfo.value) +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_when_nothing_added_to_commit(config, mocker: MockFixture): + prompt_mock = mocker.patch("questionary.prompt") + prompt_mock.return_value = { + "prefix": "feat", + "subject": "user created", + "scope": "", + "is_breaking_change": False, + "body": "", + "footer": "", + } + + commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command( + 'nothing added to commit but untracked files present (use "git add" to track)', + "", + b"", + b"", + 0, + ) + + error_mock = mocker.patch("commitizen.out.error") + + commands.Commit(config, {"all": False})() + + prompt_mock.assert_called_once() + error_mock.assert_called_once() + + assert "nothing added" in error_mock.call_args[0][0] + + +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_when_no_changes_added_to_commit(config, mocker: MockFixture): + prompt_mock = mocker.patch("questionary.prompt") + prompt_mock.return_value = { + "prefix": "feat", + "subject": "user created", + "scope": "", + "is_breaking_change": False, + "body": "", + "footer": "", + } + + commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command( + 'no changes added to commit (use "git add" and/or "git commit -a")', + "", + b"", + b"", + 0, + ) + + error_mock = mocker.patch("commitizen.out.error") + + commands.Commit(config, {"all": False})() + + prompt_mock.assert_called_once() + error_mock.assert_called_once() + + assert "no changes added to commit" in error_mock.call_args[0][0] + + @pytest.mark.usefixtures("staging_is_clean") def test_commit_with_allow_empty(config, mocker: MockFixture): prompt_mock = mocker.patch("questionary.prompt") diff --git a/tests/test_cz_customize.py b/tests/test_cz_customize.py index 933b1aa065..7100457bd6 100644 --- a/tests/test_cz_customize.py +++ b/tests/test_cz_customize.py @@ -1,10 +1,27 @@ import pytest +from pytest_mock import MockFixture +from commitizen import cmd, commands from commitizen.config import BaseConfig, JsonConfig, TomlConfig, YAMLConfig from commitizen.cz.customize import CustomizeCommitsCz -from commitizen.exceptions import MissingCzCustomizeConfigError +from commitizen.cz.utils import ( + break_multiple_line, + required_validator, + required_validator_scope, + required_validator_subject_strip, + required_validator_title_strip, +) +from commitizen.exceptions import MissingCzCustomizeConfigError, NotAllowed TOML_STR = r""" + [tool.commitizen] + name = "cz_customize" + version = "1.0.0" + version_files = [ + "commitizen/__version__.py", + "pyproject.toml" + ] + [tool.commitizen.customize] message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}" example = "feature: this feature enables customization through a config file" @@ -28,10 +45,17 @@ ] message = "Select the type of change you are committing" + [[tool.commitizen.customize.questions]] + type = "input" + name = "subject" + message = "Subject." + filter = "required_validator_subject_strip" + [[tool.commitizen.customize.questions]] type = "input" name = "message" message = "Body." + filter = "break_multiple_line" [[tool.commitizen.customize.questions]] type = "confirm" @@ -42,7 +66,7 @@ JSON_STR = r""" { "commitizen": { - "name": "cz_jira", + "name": "cz_customize", "version": "1.0.0", "version_files": [ "commitizen/__version__.py", @@ -81,10 +105,17 @@ ], "message": "Select the type of change you are committing" }, + { + "type": "input", + "name": "subject", + "message": "Subject.", + "filter": "required_validator_subject_strip" + }, { "type": "input", "name": "message", - "message": "Body." + "message": "Body.", + "filter": "break_multiple_line" }, { "type": "confirm", @@ -99,23 +130,28 @@ YAML_STR = """ commitizen: - name: cz_jira + name: cz_customize version: 1.0.0 version_files: - commitizen/__version__.py - pyproject.toml customize: - message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}" + message_template: '{{change_type}}:{% if show_message %} {{message}}{% endif %}' example: 'feature: this feature enables customization through a config file' - schema: ": " - schema_pattern: "(feature|bug fix):(\\s.*)" - bump_pattern: "^(break|new|fix|hotfix)" + schema: ': ' + schema_pattern: '(feature|bug fix):(\\s.*)' + bump_pattern: '^(break|new|fix|hotfix)' + commit_parser: '^(?Pfeature|bug fix):\\s(?P.*)?' + changelog_pattern: '^(feature|bug fix)?(!)?' + change_type_map: + feature: Feat + bug fix: Fix bump_map: break: MAJOR new: MINOR fix: PATCH hotfix: PATCH - change_type_order: ["perf", "BREAKING CHANGE", "feat", "fix", "refactor"] + change_type_order: ['perf', 'BREAKING CHANGE', 'feat', 'fix', 'refactor'] info: This is a customized cz. questions: - type: list @@ -126,9 +162,14 @@ - value: bug fix name: 'bug fix: A bug fix.' message: Select the type of change you are committing + - type: input + name: subject + message: Subject. + filter: required_validator_subject_strip - type: input name: message message: Body. + filter: break_multiple_line - type: confirm name: show_message message: Do you want to add body message in commit? @@ -317,10 +358,18 @@ """ +@pytest.fixture +def staging_is_clean(mocker: MockFixture, tmp_git_project): + is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") + is_staging_clean_mock.return_value = False + return tmp_git_project + + @pytest.fixture( params=[ TomlConfig(data=TOML_STR, path="not_exist.toml"), JsonConfig(data=JSON_STR, path="not_exist.json"), + YAMLConfig(data=YAML_STR, path="not_exist.yaml"), ] ) def config(request): @@ -332,6 +381,15 @@ def config(request): return request.param +@pytest.fixture( + params=[ + YAMLConfig(data=YAML_STR, path="not_exist.yaml"), + ] +) +def config_filters(request): + return request.param + + @pytest.fixture( params=[ TomlConfig(data=TOML_STR_INFO_PATH, path="not_exist.toml"), @@ -423,7 +481,7 @@ def test_change_type_order_unicode(config_with_unicode): ] -def test_questions(config): +def test_questions_default(config): cz = CustomizeCommitsCz(config) questions = cz.questions() expected_questions = [ @@ -436,7 +494,18 @@ def test_questions(config): ], "message": "Select the type of change you are committing", }, - {"type": "input", "name": "message", "message": "Body."}, + { + "type": "input", + "name": "subject", + "message": "Subject.", + "filter": "required_validator_subject_strip", + }, + { + "type": "input", + "name": "message", + "message": "Body.", + "filter": "break_multiple_line", + }, { "type": "confirm", "name": "show_message", @@ -446,6 +515,83 @@ def test_questions(config): assert list(questions) == expected_questions +@pytest.mark.usefixtures("staging_is_clean") +def test_questions_filter_default(config, mocker: MockFixture): + is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") + is_staging_clean_mock.return_value = False + + prompt_mock = mocker.patch("questionary.prompt") + prompt_mock.return_value = { + "change_type": "feature", + "subject": "user created", + "message": "body of the commit", + "show_message": True, + } + + commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) + + commands.Commit(config, {})() + + prompts_questions = prompt_mock.call_args[0][0] + assert prompts_questions[0]["type"] == "list" + assert prompts_questions[0]["name"] == "change_type" + assert prompts_questions[0]["use_shortcuts"] is False + assert prompts_questions[1]["type"] == "input" + assert prompts_questions[1]["name"] == "subject" + assert prompts_questions[1]["filter"] == required_validator_subject_strip + assert prompts_questions[2]["type"] == "input" + assert prompts_questions[2]["name"] == "message" + assert prompts_questions[2]["filter"] == break_multiple_line + assert prompts_questions[3]["type"] == "confirm" + assert prompts_questions[3]["name"] == "show_message" + + +@pytest.mark.usefixtures("staging_is_clean") +def test_questions_filter_values(config_filters, mocker: MockFixture): + is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") + is_staging_clean_mock.return_value = False + + prompt_mock = mocker.patch("questionary.prompt") + prompt_mock.return_value = { + "change_type": "feature", + "subject": "user created", + "message": "body of the commit", + "show_message": True, + } + + commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) + + commit_cmd = commands.Commit(config_filters, {}) + + assert isinstance(commit_cmd.cz, CustomizeCommitsCz) + + for filter_desc in [ + ("break_multiple_line", break_multiple_line), + ("required_validator", required_validator), + ("required_validator_scope", required_validator_scope), + ("required_validator_subject_strip", required_validator_subject_strip), + ("required_validator_title_strip", required_validator_title_strip), + ]: + commit_cmd.cz.custom_settings["questions"][1]["filter"] = filter_desc[0] # type: ignore[index] + commit_cmd() + + assert filter_desc[1]("input") + + prompts_questions = prompt_mock.call_args[0][0] + assert prompts_questions[1]["filter"] == filter_desc[1] + + for filter_name in [ + "", + "faulty_value", + ]: + commit_cmd.cz.custom_settings["questions"][1]["filter"] = filter_name # type: ignore[index] + + with pytest.raises(NotAllowed): + commit_cmd() + + def test_questions_unicode(config_with_unicode): cz = CustomizeCommitsCz(config_with_unicode) questions = cz.questions() diff --git a/tests/test_cz_utils.py b/tests/test_cz_utils.py index 25c960c9a7..6f72ef4f55 100644 --- a/tests/test_cz_utils.py +++ b/tests/test_cz_utils.py @@ -11,12 +11,12 @@ def test_required_validator(): utils.required_validator("") -def test_multiple_line_breaker(): +def test_break_multiple_line(): message = "this is the first line | and this is the second line " - result = utils.multiple_line_breaker(message) + result = utils.break_multiple_line(message) assert result == "this is the first line\nand this is the second line" - result = utils.multiple_line_breaker(message, "is") + result = utils.break_multiple_line(message, "is") assert result == "th\n\nthe first line | and th\n\nthe second line"