Skip to content

Commit 931877e

Browse files
fix(changelog_formats): raise on explicit invalid format even with valid filename
Refactor get_changelog_format with explicit branching: when changelog_format is set, look it up directly and raise ChangelogFormatUnknown if missing, regardless of whether the filename extension is recognized. Previously the 'or _guess_changelog_format' fallback could silently ignore an invalid explicit configuration when the filename had a known extension. Add a regression test exercising changelog_format='invalidformat' with filename='CHANGELOG.md'. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f10ef61 commit 931877e

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

commitizen/changelog_formats/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,25 @@ def get_changelog_format(
7272
be matched to a known format and no ``changelog_format`` is set.
7373
"""
7474
name: str | None = config.settings.get("changelog_format")
75-
format = (
76-
name and KNOWN_CHANGELOG_FORMATS.get(name) or _guess_changelog_format(filename)
77-
)
75+
known = ", ".join(sorted(KNOWN_CHANGELOG_FORMATS)) or "(none registered)"
7876

79-
if not format:
80-
known = ", ".join(sorted(KNOWN_CHANGELOG_FORMATS)) or "(none registered)"
81-
if name:
77+
if name:
78+
format_cls = KNOWN_CHANGELOG_FORMATS.get(name)
79+
if format_cls is None:
8280
raise ChangelogFormatUnknown(
8381
f"Unknown changelog format '{name}'. Known formats: {known}."
8482
)
83+
return format_cls(config)
84+
85+
format_cls = _guess_changelog_format(filename)
86+
if format_cls is None:
8587
raise ChangelogFormatUnknown(
8688
"Cannot infer changelog format from filename "
8789
f"'{filename}'. Set the `changelog_format` setting "
8890
f"explicitly. Known formats: {known}."
8991
)
9092

91-
return format(config)
93+
return format_cls(config)
9294

9395

9496
def _guess_changelog_format(filename: str | None) -> type[ChangelogFormat] | None:

tests/test_changelog_formats.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,12 @@ def test_get_format_unknown_name_lists_known_formats(config: BaseConfig):
8383
msg = str(excinfo.value)
8484
assert "definitely-not-a-format" in msg
8585
assert "Known formats" in msg
86+
87+
88+
def test_get_format_unknown_name_with_known_filename_raises(config: BaseConfig):
89+
config.settings["changelog_format"] = "invalidformat"
90+
with pytest.raises(ChangelogFormatUnknown) as excinfo:
91+
get_changelog_format(config, "CHANGELOG.md")
92+
msg = str(excinfo.value)
93+
assert "invalidformat" in msg
94+
assert "Known formats" in msg

0 commit comments

Comments
 (0)