diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index 33dca6b6c..89adddcf9 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -267,6 +267,7 @@ def __call__(self) -> None: tree, self.cz.template_loader, self.template, + incremental=self.incremental, # extra variable for the template **{ **self.cz.template_extras, **self.config.settings["extras"], diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 08a2b6cf7..5cecbeab0 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1728,3 +1728,57 @@ class FakeTemplate: assert not target.exists() assert "Template filename is not set" in str(exc_info.value) + + +def test_changelog_template_incremental_variable( + tmp_commitizen_project: Path, + any_changelog_format: ChangelogFormat, + util: UtilFixture, + file_regression: FileRegressionFixture, +): + """ + Test that the changelog template is not rendered when the incremental flag is not set. + Reference: https://github.com/commitizen-tools/commitizen/discussions/1620 + """ + project_root = Path(tmp_commitizen_project) + changelog_tpl = project_root / any_changelog_format.template + changelog_tpl.write_text( + dedent(""" + {% if not incremental %} + # CHANGELOG + {% endif %} + + {% for entry in tree %} + + ## {{ entry.version }}{% if entry.date %} ({{ entry.date }}){% endif %} + + {% for change_key, changes in entry.changes.items() %} + + {% if change_key %} + ### {{ change_key }} + {% endif %} + + {% for change in changes %} + {% if change.scope %} + - **{{ change.scope }}**: {{ change.message }} + {% elif change.message %} + - {{ change.message }} + {% endif %} + {% endfor %} + {% endfor %} + {% endfor %} + """) + ) + target = "CHANGELOG.md" + + util.create_file_and_commit("feat(foo): new file") + util.run_cli("changelog", "--file-name", target) + with open(target, encoding="utf-8") as f: + out = f.read() + file_regression.check(out, extension=".md") + + util.create_file_and_commit("refactor(bar): another new file") + util.run_cli("changelog", "--file-name", target, "--incremental") + with open(target, encoding="utf-8") as f: + out = f.read() + file_regression.check(out, extension=".incremental.md") diff --git a/tests/commands/test_changelog_command/test_changelog_template_incremental_variable.incremental.md b/tests/commands/test_changelog_command/test_changelog_template_incremental_variable.incremental.md new file mode 100644 index 000000000..4a851c662 --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_template_incremental_variable.incremental.md @@ -0,0 +1,12 @@ +# CHANGELOG + + +## Unreleased + +### Feat + +- **foo**: new file + +### Refactor + +- **bar**: another new file diff --git a/tests/commands/test_changelog_command/test_changelog_template_incremental_variable.md b/tests/commands/test_changelog_command/test_changelog_template_incremental_variable.md new file mode 100644 index 000000000..7c9034d27 --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_template_incremental_variable.md @@ -0,0 +1,8 @@ +# CHANGELOG + + +## Unreleased + +### Feat + +- **foo**: new file