Skip to content

Commit f10ef61

Browse files
fix(changelog): clearer error when changelog format cannot be inferred
When `changelog_format` was unset and the changelog filename had an unknown extension (e.g. `CHANGELOG.md.sections`), commitizen raised `Unknown changelog format 'None'` -- which is confusing because the user never set anything to `None`. Improve the two failure paths: * If the user set `changelog_format` to an unknown value, the error echoes that value and lists the registered formats. * If the format had to be inferred from the filename and the inference failed, the error names the offending filename, points to the `changelog_format` setting and lists the registered formats. No behaviour change for the success paths. Closes #894 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4d99415 commit f10ef61

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

commitizen/changelog_formats/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,26 @@ def get_changelog_format(
6767
"""
6868
Get a format from its name
6969
70-
:raises FormatUnknown: if a non-empty name is provided but cannot be found in the known formats
70+
:raises ChangelogFormatUnknown: if a non-empty name is provided but cannot
71+
be found in the known formats, or if the filename's extension cannot
72+
be matched to a known format and no ``changelog_format`` is set.
7173
"""
7274
name: str | None = config.settings.get("changelog_format")
7375
format = (
7476
name and KNOWN_CHANGELOG_FORMATS.get(name) or _guess_changelog_format(filename)
7577
)
7678

7779
if not format:
78-
raise ChangelogFormatUnknown(f"Unknown changelog format '{name}'")
80+
known = ", ".join(sorted(KNOWN_CHANGELOG_FORMATS)) or "(none registered)"
81+
if name:
82+
raise ChangelogFormatUnknown(
83+
f"Unknown changelog format '{name}'. Known formats: {known}."
84+
)
85+
raise ChangelogFormatUnknown(
86+
"Cannot infer changelog format from filename "
87+
f"'{filename}'. Set the `changelog_format` setting "
88+
f"explicitly. Known formats: {known}."
89+
)
7990

8091
return format(config)
8192

tests/test_changelog_formats.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,33 @@ def test_get_format_empty_filename(config: BaseConfig, filename: str | None):
5353
@pytest.mark.parametrize("filename", [None, ""])
5454
def test_get_format_empty_filename_no_setting(config: BaseConfig, filename: str | None):
5555
config.settings["changelog_format"] = None
56-
with pytest.raises(ChangelogFormatUnknown):
56+
with pytest.raises(ChangelogFormatUnknown) as excinfo:
5757
get_changelog_format(config, filename)
58+
# The error message should hint at setting `changelog_format` and list
59+
# the known formats so users on non-standard file extensions know what
60+
# to do (#894).
61+
msg = str(excinfo.value)
62+
assert "changelog_format" in msg
63+
assert "Known formats" in msg
5864

5965

6066
@pytest.mark.parametrize("filename", ["extensionless", "file.unknown"])
6167
def test_get_format_unknown(config: BaseConfig, filename: str | None):
62-
with pytest.raises(ChangelogFormatUnknown):
68+
with pytest.raises(ChangelogFormatUnknown) as excinfo:
6369
get_changelog_format(config, filename)
70+
# Same hint when the filename extension is unknown.
71+
msg = str(excinfo.value)
72+
assert "changelog_format" in msg
73+
assert "Known formats" in msg
74+
75+
76+
def test_get_format_unknown_name_lists_known_formats(config: BaseConfig):
77+
"""Regression test for #894: when ``changelog_format`` is set to an
78+
unknown value, the error must list the registered formats so users
79+
can self-correct."""
80+
config.settings["changelog_format"] = "definitely-not-a-format"
81+
with pytest.raises(ChangelogFormatUnknown) as excinfo:
82+
get_changelog_format(config)
83+
msg = str(excinfo.value)
84+
assert "definitely-not-a-format" in msg
85+
assert "Known formats" in msg

0 commit comments

Comments
 (0)