Skip to content

Ensure At Least One Blank Line Between Old and New Changelog Content #511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 10, 2022
Merged
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
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Please fill in the following content to let us know better about this change.
## Checklist

- [ ] Add test cases to all the changes you introduce
- [ ] Run `./script/format` and `./script/test` locally to ensure this change passes linter check and test
- [ ] Run `./scripts/format` and `./scripts/test` locally to ensure this change passes linter check and test
- [ ] Test the changes on the local machine manually
- [ ] Update the documentation for the changes

Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ repos:
hooks:
- id: check-vcs-permalinks
- id: end-of-file-fixer
exclude: "tests/[test_*|data|commands/tests_*]/*"
exclude: "tests/((commands|data)/|test_).+"
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- id: debug-statements
- id: no-commit-to-branch

- repo: https://github.com/Woile/commitizen
rev: v1.23.0
- repo: https://github.com/commitizen-tools/commitizen
rev: v2.24.0 # automatically updated by Commitizen
hooks:
- id: commitizen
stages: [commit-msg]
Expand Down
20 changes: 9 additions & 11 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ def get_metadata(filepath: str) -> Dict:
}


def incremental_build(new_content: str, lines: List, metadata: Dict) -> List:
def incremental_build(new_content: str, lines: List[str], metadata: Dict) -> List[str]:
"""Takes the original lines and updates with new_content.

The metadata holds information enough to remove the old unreleased and
where to place the new content
The metadata governs how to remove the old unreleased section and where to place the
new content.

Args:
lines: The lines from the changelog
Expand All @@ -253,7 +253,7 @@ def incremental_build(new_content: str, lines: List, metadata: Dict) -> List:
unreleased_end = metadata.get("unreleased_end")
latest_version_position = metadata.get("latest_version_position")
skip = False
output_lines: List = []
output_lines: List[str] = []
for index, line in enumerate(lines):
if index == unreleased_start:
skip = True
Expand All @@ -270,16 +270,14 @@ def incremental_build(new_content: str, lines: List, metadata: Dict) -> List:
if skip:
continue

if (
isinstance(latest_version_position, int)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering why this condition is removed

and index == latest_version_position
):

output_lines.append(new_content)
output_lines.append("\n")
if index == latest_version_position:
output_lines.extend([new_content, "\n"])

output_lines.append(line)
if not isinstance(latest_version_position, int):
if output_lines and output_lines[-1].strip():
# Ensure at least one blank line between existing and new content.
output_lines.append("\n")
output_lines.append(new_content)
return output_lines

Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ version = "2.24.0"
tag_format = "v$version"
version_files = [
"pyproject.toml:version",
"commitizen/__version__.py"
"commitizen/__version__.py",
".pre-commit-config.yaml:rev.\\s+(?=[^\\n]+Commitizen)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is neat!

]

[tool.black]
Expand Down
24 changes: 24 additions & 0 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,30 @@ def test_changelog_multiple_incremental_do_not_add_new_lines(
assert out.startswith("#")


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_changelog_incremental_newline_separates_new_content_from_old(
mocker, changelog_path
):
"""Test for https://github.com/commitizen-tools/commitizen/issues/509"""
with open(changelog_path, "w") as f:
f.write("Pre-existing content that should be kept\n")

create_file_and_commit("feat: add more cat videos")

testargs = ["cz", "changelog", "--incremental"]

mocker.patch.object(sys, "argv", testargs)
cli.main()

with open(changelog_path, "r") as f:
out = f.read()

assert (
out
== "Pre-existing content that should be kept\n\n## Unreleased\n\n### Feat\n\n- add more cat videos\n"
)


def test_changelog_without_revision(mocker, tmp_commitizen_project):
changelog_file = tmp_commitizen_project.join("CHANGELOG.md")
changelog_file.write(
Expand Down