Skip to content
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
24 changes: 16 additions & 8 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import os.path
from difflib import SequenceMatcher
from operator import itemgetter
Expand Down Expand Up @@ -32,21 +33,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 = (
os.path.join(str(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(
Expand Down
43 changes: 41 additions & 2 deletions tests/test_changelog.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from __future__ import annotations

import os
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Optional
from typing import Any
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,
)
Expand Down Expand Up @@ -1499,7 +1505,7 @@ def changelog_message_builder_hook(message: dict, commit: git.GitCommit):
def test_render_changelog_with_changelog_release_hook(
gitcommits, tags, any_changelog_format: ChangelogFormat
):
def changelog_release_hook(release: dict, tag: Optional[git.GitTag]) -> dict:
def changelog_release_hook(release: dict, tag: git.GitTag | None) -> dict:
release["extra"] = "whatever"
return release

Expand Down Expand Up @@ -1631,3 +1637,36 @@ 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 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 os.path.normpath(changelog.file_name) == os.path.normpath(
os.path.join("/my/project", "CHANGELOG.md")
)