Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
54 changes: 54 additions & 0 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# CHANGELOG


## Unreleased

### Feat

- **foo**: new file

### Refactor

- **bar**: another new file
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# CHANGELOG


## Unreleased

### Feat

- **foo**: new file