-
-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathtest_changelog_formats.py
More file actions
94 lines (74 loc) · 3.36 KB
/
Copy pathtest_changelog_formats.py
File metadata and controls
94 lines (74 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from commitizen import defaults
from commitizen.changelog_formats import (
KNOWN_CHANGELOG_FORMATS,
ChangelogFormat,
_guess_changelog_format,
get_changelog_format,
)
from commitizen.exceptions import ChangelogFormatUnknown
if TYPE_CHECKING:
from commitizen.config.base_config import BaseConfig
@pytest.mark.parametrize("format", KNOWN_CHANGELOG_FORMATS.values())
def test_guess_format(format: type[ChangelogFormat]):
assert _guess_changelog_format(f"CHANGELOG.{format.extension}") is format
for ext in format.alternative_extensions:
assert _guess_changelog_format(f"CHANGELOG.{ext}") is format
@pytest.mark.parametrize("filename", ["CHANGELOG", "NEWS", "file.unknown", None])
def test_guess_format_unknown(filename: str):
assert _guess_changelog_format(filename) is None
@pytest.mark.parametrize(
("name", "expected"),
[
pytest.param(name, format, id=name)
for name, format in KNOWN_CHANGELOG_FORMATS.items()
],
)
def test_get_format(config: BaseConfig, name: str, expected: type[ChangelogFormat]):
config.settings["changelog_format"] = name
assert isinstance(get_changelog_format(config), expected)
@pytest.mark.parametrize("filename", [None, ""])
def test_get_format_empty_filename(config: BaseConfig, filename: str | None):
config.settings["changelog_format"] = defaults.CHANGELOG_FORMAT
assert isinstance(
get_changelog_format(config, filename),
KNOWN_CHANGELOG_FORMATS[defaults.CHANGELOG_FORMAT],
)
@pytest.mark.parametrize("filename", [None, ""])
def test_get_format_empty_filename_no_setting(config: BaseConfig, filename: str | None):
config.settings["changelog_format"] = None
with pytest.raises(ChangelogFormatUnknown) as excinfo:
get_changelog_format(config, filename)
# The error message should hint at setting `changelog_format` and list
# the known formats so users on non-standard file extensions know what
# to do (#894).
msg = str(excinfo.value)
assert "changelog_format" in msg
assert "Known formats" in msg
@pytest.mark.parametrize("filename", ["extensionless", "file.unknown"])
def test_get_format_unknown(config: BaseConfig, filename: str | None):
with pytest.raises(ChangelogFormatUnknown) as excinfo:
get_changelog_format(config, filename)
# Same hint when the filename extension is unknown.
msg = str(excinfo.value)
assert "changelog_format" in msg
assert "Known formats" in msg
def test_get_format_unknown_name_lists_known_formats(config: BaseConfig):
"""Regression test for #894: when ``changelog_format`` is set to an
unknown value, the error must list the registered formats so users
can self-correct."""
config.settings["changelog_format"] = "definitely-not-a-format"
with pytest.raises(ChangelogFormatUnknown) as excinfo:
get_changelog_format(config)
msg = str(excinfo.value)
assert "definitely-not-a-format" in msg
assert "Known formats" in msg
def test_get_format_unknown_name_with_known_filename_raises(config: BaseConfig):
config.settings["changelog_format"] = "invalidformat"
with pytest.raises(ChangelogFormatUnknown) as excinfo:
get_changelog_format(config, "CHANGELOG.md")
msg = str(excinfo.value)
assert "invalidformat" in msg
assert "Known formats" in msg