Skip to content

Commit 9e26cca

Browse files
committed
fix(changelog): add incremental parameter to changelog generation
Relate #1620
1 parent 38483e9 commit 9e26cca

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

commitizen/commands/changelog.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def __call__(self) -> None:
267267
tree,
268268
self.cz.template_loader,
269269
self.template,
270+
incremental=self.incremental, # extra variable for the template
270271
**{
271272
**self.cz.template_extras,
272273
**self.config.settings["extras"],

tests/commands/test_changelog_command.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,3 +1728,57 @@ class FakeTemplate:
17281728

17291729
assert not target.exists()
17301730
assert "Template filename is not set" in str(exc_info.value)
1731+
1732+
1733+
def test_changelog_template_incremental_variable(
1734+
tmp_commitizen_project: Path,
1735+
any_changelog_format: ChangelogFormat,
1736+
util: UtilFixture,
1737+
file_regression: FileRegressionFixture,
1738+
):
1739+
"""
1740+
Test that the changelog template is not rendered when the incremental flag is not set.
1741+
Reference: https://github.com/commitizen-tools/commitizen/discussions/1620
1742+
"""
1743+
project_root = Path(tmp_commitizen_project)
1744+
changelog_tpl = project_root / any_changelog_format.template
1745+
changelog_tpl.write_text(
1746+
dedent("""
1747+
{% if not incremental %}
1748+
# CHANGELOG
1749+
{% endif %}
1750+
1751+
{% for entry in tree %}
1752+
1753+
## {{ entry.version }}{% if entry.date %} ({{ entry.date }}){% endif %}
1754+
1755+
{% for change_key, changes in entry.changes.items() %}
1756+
1757+
{% if change_key %}
1758+
### {{ change_key }}
1759+
{% endif %}
1760+
1761+
{% for change in changes %}
1762+
{% if change.scope %}
1763+
- **{{ change.scope }}**: {{ change.message }}
1764+
{% elif change.message %}
1765+
- {{ change.message }}
1766+
{% endif %}
1767+
{% endfor %}
1768+
{% endfor %}
1769+
{% endfor %}
1770+
""")
1771+
)
1772+
target = "CHANGELOG.md"
1773+
1774+
util.create_file_and_commit("feat: new file")
1775+
util.run_cli("changelog", "--file-name", target)
1776+
with open(target, encoding="utf-8") as f:
1777+
out = f.read()
1778+
file_regression.check(out, extension=".md")
1779+
1780+
util.create_file_and_commit("feat: another new file")
1781+
util.run_cli("changelog", "--file-name", target, "--incremental")
1782+
with open(target, encoding="utf-8") as f:
1783+
out = f.read()
1784+
file_regression.check(out, extension=".incremental.md")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# CHANGELOG
2+
3+
4+
## Unreleased
5+
6+
### Feat
7+
8+
- another new file
9+
- new file
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# CHANGELOG
2+
3+
4+
## Unreleased
5+
6+
### Feat
7+
8+
- new file

0 commit comments

Comments
 (0)