Skip to content

Commit 11b177f

Browse files
committed
fix(changelog): empty lines at the beginning of the CHANGELOG
Closes #192
1 parent 601bd29 commit 11b177f

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

CHANGELOG.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
2-
3-
4-
5-
6-
71
## v1.22.1 (2020-05-23)
82

93
### Fix

commitizen/commands/changelog.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def __call__(self):
100100
changelog_message_builder_hook=changelog_message_builder_hook,
101101
)
102102
changelog_out = changelog.render_changelog(tree)
103+
changelog_out = changelog_out.lstrip("\n")
103104

104105
if self.dry_run:
105106
out.write(changelog_out)

tests/commands/test_changelog_command.py

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
from tests.utils import create_file_and_commit
1010

1111

12+
@pytest.fixture()
13+
def changelog_path() -> str:
14+
return os.path.join(os.getcwd(), "CHANGELOG.md")
15+
16+
1217
@pytest.mark.usefixtures("tmp_commitizen_project")
1318
def test_changlog_on_empty_project(mocker):
1419
testargs = ["cz", "changelog", "--dry-run"]
@@ -38,7 +43,7 @@ def test_changlog_from_version_zero_point_two(mocker, capsys):
3843
cli.main()
3944

4045
out, _ = capsys.readouterr()
41-
assert out == "\n## Unreleased\n\n### Feat\n\n- after 0.2\n- after 0.2.0\n\n"
46+
assert out == "## Unreleased\n\n### Feat\n\n- after 0.2\n- after 0.2.0\n\n"
4247

4348

4449
@pytest.mark.usefixtures("tmp_commitizen_project")
@@ -54,13 +59,12 @@ def test_changlog_with_different_cz(mocker, capsys):
5459
out, _ = capsys.readouterr()
5560
assert (
5661
out
57-
== "\n## Unreleased\n\n\n- JRA-35 #time 1w 2d 4h 30m Total work logged\n- JRA-34 #comment corrected indent issue\n\n"
62+
== "## Unreleased\n\n\n- JRA-35 #time 1w 2d 4h 30m Total work logged\n- JRA-34 #comment corrected indent issue\n\n"
5863
)
5964

6065

6166
@pytest.mark.usefixtures("tmp_commitizen_project")
62-
def test_changlog_from_start(mocker, capsys):
63-
changelog_path = os.path.join(os.getcwd(), "CHANGELOG.md")
67+
def test_changlog_from_start(mocker, capsys, changelog_path):
6468
create_file_and_commit("feat: new file")
6569
create_file_and_commit("refactor: is in changelog")
6670
create_file_and_commit("Merge into master")
@@ -74,14 +78,14 @@ def test_changlog_from_start(mocker, capsys):
7478

7579
assert (
7680
out
77-
== "\n## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n"
81+
== "## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n"
7882
)
7983

8084

8185
@pytest.mark.usefixtures("tmp_commitizen_project")
82-
def test_changlog_replacing_unreleased_using_incremental(mocker, capsys):
83-
changelog_path = os.path.join(os.getcwd(), "CHANGELOG.md")
84-
86+
def test_changlog_replacing_unreleased_using_incremental(
87+
mocker, capsys, changelog_path
88+
):
8589
create_file_and_commit("feat: add new output")
8690
create_file_and_commit("fix: output glitch")
8791
create_file_and_commit("Merge into master")
@@ -108,13 +112,12 @@ def test_changlog_replacing_unreleased_using_incremental(mocker, capsys):
108112
today = date.today().isoformat()
109113
assert (
110114
out
111-
== f"\n\n## Unreleased\n\n### Feat\n\n- add more stuff\n\n### Fix\n\n- mama gotta work\n\n## 0.2.0 ({today})\n\n### Fix\n\n- output glitch\n\n### Feat\n\n- add new output\n"
115+
== f"## Unreleased\n\n### Feat\n\n- add more stuff\n\n### Fix\n\n- mama gotta work\n\n## 0.2.0 ({today})\n\n### Fix\n\n- output glitch\n\n### Feat\n\n- add new output\n"
112116
)
113117

114118

115119
@pytest.mark.usefixtures("tmp_commitizen_project")
116-
def test_changlog_is_persisted_using_incremental(mocker, capsys):
117-
changelog_path = os.path.join(os.getcwd(), "CHANGELOG.md")
120+
def test_changlog_is_persisted_using_incremental(mocker, capsys, changelog_path):
118121

119122
create_file_and_commit("feat: add new output")
120123
create_file_and_commit("fix: output glitch")
@@ -146,13 +149,12 @@ def test_changlog_is_persisted_using_incremental(mocker, capsys):
146149
today = date.today().isoformat()
147150
assert (
148151
out
149-
== f"\n\n## Unreleased\n\n### Feat\n\n- add more stuff\n\n### Fix\n\n- mama gotta work\n\n## 0.2.0 ({today})\n\n### Fix\n\n- output glitch\n\n### Feat\n\n- add new output\n\nnote: this should be persisted using increment\n"
152+
== f"## Unreleased\n\n### Feat\n\n- add more stuff\n\n### Fix\n\n- mama gotta work\n\n## 0.2.0 ({today})\n\n### Fix\n\n- output glitch\n\n### Feat\n\n- add new output\n\nnote: this should be persisted using increment\n"
150153
)
151154

152155

153156
@pytest.mark.usefixtures("tmp_commitizen_project")
154-
def test_changlog_incremental_angular_sample(mocker, capsys):
155-
changelog_path = os.path.join(os.getcwd(), "CHANGELOG.md")
157+
def test_changlog_incremental_angular_sample(mocker, capsys, changelog_path):
156158
with open(changelog_path, "w") as f:
157159
f.write(
158160
"# [10.0.0-next.3](https://github.com/angular/angular/compare/10.0.0-next.2...10.0.0-next.3) (2020-04-22)\n"
@@ -180,7 +182,7 @@ def test_changlog_incremental_angular_sample(mocker, capsys):
180182

181183
assert (
182184
out
183-
== "\n## Unreleased\n\n### Feat\n\n- add more stuff\n- add new output\n\n### Fix\n\n- mama gotta work\n- output glitch\n\n# [10.0.0-next.3](https://github.com/angular/angular/compare/10.0.0-next.2...10.0.0-next.3) (2020-04-22)\n\n### Bug Fixes\n* **common:** format day-periods that cross midnight ([#36611](https://github.com/angular/angular/issues/36611)) ([c6e5fc4](https://github.com/angular/angular/commit/c6e5fc4)), closes [#36566](https://github.com/angular/angular/issues/36566)\n"
185+
== "## Unreleased\n\n### Feat\n\n- add more stuff\n- add new output\n\n### Fix\n\n- mama gotta work\n- output glitch\n\n# [10.0.0-next.3](https://github.com/angular/angular/compare/10.0.0-next.2...10.0.0-next.3) (2020-04-22)\n\n### Bug Fixes\n* **common:** format day-periods that cross midnight ([#36611](https://github.com/angular/angular/issues/36611)) ([c6e5fc4](https://github.com/angular/angular/commit/c6e5fc4)), closes [#36566](https://github.com/angular/angular/issues/36566)\n"
184186
)
185187

186188

@@ -210,8 +212,7 @@ def test_changlog_incremental_angular_sample(mocker, capsys):
210212

211213

212214
@pytest.mark.usefixtures("tmp_commitizen_project")
213-
def test_changlog_incremental_keep_a_changelog_sample(mocker, capsys):
214-
changelog_path = os.path.join(os.getcwd(), "CHANGELOG.md")
215+
def test_changlog_incremental_keep_a_changelog_sample(mocker, capsys, changelog_path):
215216
with open(changelog_path, "w") as f:
216217
f.write(KEEP_A_CHANGELOG)
217218
create_file_and_commit("irrelevant commit")
@@ -233,7 +234,7 @@ def test_changlog_incremental_keep_a_changelog_sample(mocker, capsys):
233234

234235
assert (
235236
out
236-
== """# Changelog\nAll notable changes to this project will be documented in this file.\n\nThe format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),\nand this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n\n## Unreleased\n\n### Feat\n\n- add more stuff\n- add new output\n\n### Fix\n\n- mama gotta work\n- output glitch\n\n## [1.0.0] - 2017-06-20\n### Added\n- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).\n- Version navigation.\n\n### Changed\n- Start using "changelog" over "change log" since it\'s the common usage.\n\n### Removed\n- Section about "changelog" vs "CHANGELOG".\n\n## [0.3.0] - 2015-12-03\n### Added\n- RU translation from [@aishek](https://github.com/aishek).\n"""
237+
== """# Changelog\nAll notable changes to this project will be documented in this file.\n\nThe format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),\nand this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n## Unreleased\n\n### Feat\n\n- add more stuff\n- add new output\n\n### Fix\n\n- mama gotta work\n- output glitch\n\n## [1.0.0] - 2017-06-20\n### Added\n- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).\n- Version navigation.\n\n### Changed\n- Start using "changelog" over "change log" since it\'s the common usage.\n\n### Removed\n- Section about "changelog" vs "CHANGELOG".\n\n## [0.3.0] - 2015-12-03\n### Added\n- RU translation from [@aishek](https://github.com/aishek).\n"""
237238
)
238239

239240

@@ -251,6 +252,43 @@ def test_changlog_hook(mocker, config):
251252
)
252253
mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock)
253254
changelog()
254-
full_changelog = "\n## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n"
255+
full_changelog = (
256+
"## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n"
257+
)
255258

256259
changelog_hook_mock.assert_called_with(full_changelog, full_changelog)
260+
261+
262+
@pytest.mark.usefixtures("tmp_commitizen_project")
263+
def test_changlog_multiple_incremental_do_not_add_new_lines(
264+
mocker, capsys, changelog_path
265+
):
266+
"""Test for bug https://github.com/commitizen-tools/commitizen/issues/192"""
267+
create_file_and_commit("feat: add new output")
268+
269+
testargs = ["cz", "changelog", "--incremental"]
270+
mocker.patch.object(sys, "argv", testargs)
271+
cli.main()
272+
273+
create_file_and_commit("fix: output glitch")
274+
275+
testargs = ["cz", "changelog", "--incremental"]
276+
mocker.patch.object(sys, "argv", testargs)
277+
cli.main()
278+
279+
create_file_and_commit("fix: mama gotta work")
280+
281+
testargs = ["cz", "changelog", "--incremental"]
282+
mocker.patch.object(sys, "argv", testargs)
283+
cli.main()
284+
285+
create_file_and_commit("feat: add more stuff")
286+
287+
testargs = ["cz", "changelog", "--incremental"]
288+
mocker.patch.object(sys, "argv", testargs)
289+
cli.main()
290+
291+
with open(changelog_path, "r") as f:
292+
out = f.read()
293+
294+
assert out.startswith("#")

0 commit comments

Comments
 (0)