From a2188e5d96fe05172593375475c6dd49200b8a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Fraire=20Willemo=C3=ABs?= Date: Sun, 26 Jul 2020 15:25:29 +0200 Subject: [PATCH] feat(conventional_commits): use and proper support for conventional commits v1.0.0 --- commitizen/changelog.py | 4 +- .../conventional_commits.py | 16 ++++---- tests/commands/test_changelog_command.py | 40 +++++++++++++++++++ tests/test_cz_conventional_commits.py | 22 +++++++++- 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/commitizen/changelog.py b/commitizen/changelog.py index 52b8cf786b..9084561c15 100644 --- a/commitizen/changelog.py +++ b/commitizen/changelog.py @@ -73,7 +73,7 @@ def generate_tree_from_commits( changelog_message_builder_hook: Optional[Callable] = None, ) -> Iterable[Dict]: pat = re.compile(changelog_pattern) - map_pat = re.compile(commit_parser) + map_pat = re.compile(commit_parser, re.MULTILINE) # Check if the latest commit is not tagged latest_commit = commits[0] current_tag: Optional[GitTag] = get_commit_tag(latest_commit, tags) @@ -109,7 +109,7 @@ def generate_tree_from_commits( continue message = map_pat.match(commit.message) - message_body = map_pat.match(commit.body) + message_body = map_pat.search(commit.body) if message: parsed_message: Dict = message.groupdict() # change_type becomes optional by providing None diff --git a/commitizen/cz/conventional_commits/conventional_commits.py b/commitizen/cz/conventional_commits/conventional_commits.py index e3fbb8c269..0a95a09199 100644 --- a/commitizen/cz/conventional_commits/conventional_commits.py +++ b/commitizen/cz/conventional_commits/conventional_commits.py @@ -114,12 +114,6 @@ def questions(self) -> List[Dict[str, Any]]: "Imperative, lower case and no final dot:\n" ), }, - { - "type": "confirm", - "message": "Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer", - "name": "is_breaking_change", - "default": False, - }, { "type": "input", "name": "body", @@ -129,6 +123,12 @@ def questions(self) -> List[Dict[str, Any]]: ), "filter": multiple_line_breaker, }, + { + "type": "confirm", + "message": "Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer", + "name": "is_breaking_change", + "default": False, + }, { "type": "input", "name": "footer", @@ -150,10 +150,10 @@ def message(self, answers: dict) -> str: if scope: scope = f"({scope})" - if is_breaking_change: - body = f"BREAKING CHANGE: {body}" if body: body = f"\n\n{body}" + if is_breaking_change: + footer = f"BREAKING CHANGE: {footer}" if footer: footer = f"\n\n{footer}" diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 1091a80b33..09a96dfd72 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -349,3 +349,43 @@ def test_changelog_in_non_git_project(tmpdir, config, mocker): with tmpdir.as_cwd(): with pytest.raises(NotAGitProjectError): cli.main() + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_breaking_change_content_v1_beta(mocker, capsys): + commit_message = ( + "feat(users): email pattern corrected\n\n" + "BREAKING CHANGE: migrate by renaming user to users\n\n" + "footer content" + ) + create_file_and_commit(commit_message) + testargs = ["cz", "changelog", "--dry-run"] + mocker.patch.object(sys, "argv", testargs) + with pytest.raises(DryRunExit): + cli.main() + out, _ = capsys.readouterr() + + assert out == ( + "## Unreleased\n\n### Feat\n\n- **users**: email pattern corrected\n\n" + "### BREAKING CHANGE\n\n- migrate by renaming user to users\n\n" + ) + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_breaking_change_content_v1(mocker, capsys): + commit_message = ( + "feat(users): email pattern corrected\n\n" + "body content\n\n" + "BREAKING CHANGE: migrate by renaming user to users" + ) + create_file_and_commit(commit_message) + testargs = ["cz", "changelog", "--dry-run"] + mocker.patch.object(sys, "argv", testargs) + with pytest.raises(DryRunExit): + cli.main() + out, _ = capsys.readouterr() + + assert out == ( + "## Unreleased\n\n### Feat\n\n- **users**: email pattern corrected\n\n" + "### BREAKING CHANGE\n\n- migrate by renaming user to users\n\n" + ) diff --git a/tests/test_cz_conventional_commits.py b/tests/test_cz_conventional_commits.py index de6a3c30d4..62eef11a19 100644 --- a/tests/test_cz_conventional_commits.py +++ b/tests/test_cz_conventional_commits.py @@ -82,14 +82,32 @@ def test_long_answer(config): "prefix": "fix", "scope": "users", "subject": "email pattern corrected", - "is_breaking_change": True, + "is_breaking_change": False, "body": "complete content", "footer": "closes #24", } message = conventional_commits.message(answers) assert ( message - == "fix(users): email pattern corrected\n\nBREAKING CHANGE: complete content\n\ncloses #24" # noqa + == "fix(users): email pattern corrected\n\ncomplete content\n\ncloses #24" # noqa + ) + + +def test_breaking_change_in_footer(config): + conventional_commits = ConventionalCommitsCz(config) + answers = { + "prefix": "fix", + "scope": "users", + "subject": "email pattern corrected", + "is_breaking_change": True, + "body": "complete content", + "footer": "migrate by renaming user to users", + } + message = conventional_commits.message(answers) + print(message) + assert ( + message + == "fix(users): email pattern corrected\n\ncomplete content\n\nBREAKING CHANGE: migrate by renaming user to users" # noqa )