From 2090b7c34a13ad604e7d78a12cfb9da97227bb77 Mon Sep 17 00:00:00 2001 From: Yusin Huang Date: Tue, 15 Apr 2025 23:00:50 +0800 Subject: [PATCH 1/3] fix(changelog.py): modify the CHANGELOG.md generated by cz bump --changelog to the right place --- commitizen/commands/changelog.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index 80a72651e..71bc93265 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -32,21 +32,28 @@ def __init__(self, config: BaseConfig, args): raise NotAGitProjectError() self.config: BaseConfig = config - self.encoding = self.config.settings["encoding"] - self.cz = factory.commiter_factory(self.config) - - self.start_rev = args.get("start_rev") or self.config.settings.get( - "changelog_start_rev" - ) - self.file_name = args.get("file_name") or cast( + changelog_file_name = args.get("file_name") or cast( str, self.config.settings.get("changelog_file") ) - if not isinstance(self.file_name, str): + if not isinstance(changelog_file_name, str): raise NotAllowed( "Changelog file name is broken.\n" "Check the flag `--file-name` in the terminal " f"or the setting `changelog_file` in {self.config.path}" ) + self.file_name = ( + str(Path(self.config.path.parent) / changelog_file_name) + if self.config.path is not None + else changelog_file_name + ) + + self.encoding = self.config.settings["encoding"] + self.cz = factory.commiter_factory(self.config) + + self.start_rev = args.get("start_rev") or self.config.settings.get( + "changelog_start_rev" + ) + self.changelog_format = get_changelog_format(self.config, self.file_name) self.incremental = args["incremental"] or self.config.settings.get( From b91ce9fcf19aa1697c92c35b548a58871da1503f Mon Sep 17 00:00:00 2001 From: Yusin Huang Date: Tue, 15 Apr 2025 23:01:40 +0800 Subject: [PATCH 2/3] test(test_changelog_command.py): add test for changelog file_name construction from args and config --- tests/test_changelog.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_changelog.py b/tests/test_changelog.py index accbf5d33..3dd336cf5 100644 --- a/tests/test_changelog.py +++ b/tests/test_changelog.py @@ -2,12 +2,15 @@ from dataclasses import dataclass from pathlib import Path from typing import Optional +from unittest.mock import Mock import pytest from jinja2 import FileSystemLoader from commitizen import changelog, git from commitizen.changelog_formats import ChangelogFormat +from commitizen.commands.changelog import Changelog +from commitizen.config import BaseConfig from commitizen.cz.conventional_commits.conventional_commits import ( ConventionalCommitsCz, ) @@ -1560,3 +1563,32 @@ def test_tags_rules_get_version_tags(capsys: pytest.CaptureFixture): captured = capsys.readouterr() assert captured.err.count("InvalidVersion") == 2 assert captured.err.count("not-a-version") == 2 + + +def test_changelog_file_name_from_args_and_config(): + mock_config = Mock(spec=BaseConfig) + mock_config.path.parent = "/my/project/" + mock_config.settings = { + "name": "cz_conventional_commits", + "changelog_file": "CHANGELOG.md", + "encoding": "utf-8", + "changelog_start_rev": "v1.0.0", + "tag_format": "$version", + "legacy_tag_formats": [], + "ignored_tag_formats": [], + "incremental": True, + "changelog_merge_prerelease": True, + } + + args = { + "file_name": "CUSTOM.md", + "incremental": None, + "dry_run": False, + "unreleased_version": "1.0.1", + } + changelog = Changelog(mock_config, args) + assert changelog.file_name == "/my/project/CUSTOM.md" + + args = {"incremental": None, "dry_run": False, "unreleased_version": "1.0.1"} + changelog = Changelog(mock_config, args) + assert changelog.file_name == "/my/project/CHANGELOG.md" From c2375251960e6ba186bf55d92c6950a680ca20da Mon Sep 17 00:00:00 2001 From: Yusin Huang Date: Thu, 17 Apr 2025 01:02:27 +0800 Subject: [PATCH 3/3] fix(changelog.py): cross-platform path handling using os.path.join and modify the path linter and test parameter --- commitizen/commands/changelog.py | 3 ++- tests/test_changelog.py | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index 71bc93265..3b8f43e37 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import os.path from difflib import SequenceMatcher from operator import itemgetter @@ -42,7 +43,7 @@ def __init__(self, config: BaseConfig, args): f"or the setting `changelog_file` in {self.config.path}" ) self.file_name = ( - str(Path(self.config.path.parent) / changelog_file_name) + os.path.join(str(self.config.path.parent), changelog_file_name) if self.config.path is not None else changelog_file_name ) diff --git a/tests/test_changelog.py b/tests/test_changelog.py index c832a7e7a..1569b77aa 100644 --- a/tests/test_changelog.py +++ b/tests/test_changelog.py @@ -1,8 +1,9 @@ +import os import re from dataclasses import dataclass from pathlib import Path -from unittest.mock import Mock from typing import Any, Optional +from unittest.mock import Mock import pytest from jinja2 import FileSystemLoader @@ -1638,7 +1639,7 @@ def test_tags_rules_get_version_tags(capsys: pytest.CaptureFixture): def test_changelog_file_name_from_args_and_config(): mock_config = Mock(spec=BaseConfig) - mock_config.path.parent = "/my/project/" + mock_config.path.parent = "/my/project" mock_config.settings = { "name": "cz_conventional_commits", "changelog_file": "CHANGELOG.md", @@ -1658,8 +1659,12 @@ def test_changelog_file_name_from_args_and_config(): "unreleased_version": "1.0.1", } changelog = Changelog(mock_config, args) - assert changelog.file_name == "/my/project/CUSTOM.md" + assert os.path.normpath(changelog.file_name) == os.path.normpath( + os.path.join("/my/project", "CUSTOM.md") + ) args = {"incremental": None, "dry_run": False, "unreleased_version": "1.0.1"} changelog = Changelog(mock_config, args) - assert changelog.file_name == "/my/project/CHANGELOG.md" + assert os.path.normpath(changelog.file_name) == os.path.normpath( + os.path.join("/my/project", "CHANGELOG.md") + )