Debian Codegraph @

tests/test_changelog_format_asciidoc.py

raw
1 from __future__ import annotations
2
3 from typing import TYPE_CHECKING
4
5 import pytest
6
7 from commitizen.changelog import IncrementalMergeInfo, Metadata
8 from commitizen.changelog_formats.asciidoc import AsciiDoc
9
10 if TYPE_CHECKING:
11 from pathlib import Path
12
13 from commitizen.config.base_config import BaseConfig
14
15 CHANGELOG_A = """
16 = Changelog
17
18 All notable changes to this project will be documented in this file.
19
20 The format is based on https://keepachangelog.com/en/1.0.0/[Keep a Changelog],
21 and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Versioning].
22
23 == [Unreleased]
24 * Start using "changelog" over "change log" since it's the common usage.
25
26 == [1.0.0] - 2017-06-20
27 === Added
28 * New visual identity by https://github.com/tylerfortune8[@tylerfortune8].
29 * Version navigation.
30 """.strip()
31
32 EXPECTED_A = Metadata(
33 latest_version="1.0.0",
34 latest_version_position=10,
35 unreleased_end=10,
36 unreleased_start=7,
37 )
38
39
40 CHANGELOG_B = """
41 == [Unreleased]
42 * Start using "changelog" over "change log" since it's the common usage.
43
44 == 1.2.0
45 """.strip()
46
47 EXPECTED_B = Metadata(
48 latest_version="1.2.0",
49 latest_version_position=3,
50 unreleased_end=3,
51 unreleased_start=0,
52 )
53
54
55 CHANGELOG_C = """
56 = Unreleased
57
58 == v1.0.0
59 """
60 EXPECTED_C = Metadata(
61 latest_version="1.0.0",
62 latest_version_tag="v1.0.0",
63 latest_version_position=3,
64 unreleased_end=3,
65 unreleased_start=1,
66 )
67
68 CHANGELOG_D = """
69 == Unreleased
70 * Start using "changelog" over "change log" since it's the common usage.
71 """
72
73 EXPECTED_D = Metadata(
74 latest_version=None,
75 latest_version_position=None,
76 unreleased_end=2,
77 unreleased_start=1,
78 )
79
80 CHANGELOG_E = """
81 = Changelog
82
83 All notable changes to this project will be documented in this file.
84
85 The format is based on https://keepachangelog.com/en/1.0.0/[Keep a Changelog],
86 and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Versioning].
87
88 == [Unreleased]
89 * Start using "changelog" over "change log" since it's the common usage.
90
91 == [{tag_formatted_version}] - 2017-06-20
92 === Added
93 * New visual identity by https://github.com/tylerfortune8[@tylerfortune8].
94 * Version navigation.
95 """.strip()
96
97 EXPECTED_E = Metadata(
98 latest_version="1.0.0",
99 latest_version_position=10,
100 unreleased_end=10,
101 unreleased_start=7,
102 )
103
104
105 @pytest.fixture
106 def format(config: BaseConfig) -> AsciiDoc:
107 return AsciiDoc(config)
108
109
110 @pytest.fixture
111 def format_with_tags(config: BaseConfig, request) -> AsciiDoc:
112 config.settings["tag_format"] = request.param
113 config.settings["legacy_tag_formats"] = ["legacy-${version}"]
114 return AsciiDoc(config)
115
116
117 VERSIONS_EXAMPLES = [
118 ("== [1.0.0] - 2017-06-20", ("1.0.0", "1.0.0")),
119 (
120 "= https://github.com/angular/angular/compare/10.0.0-next.2...10.0.0-next.3[10.0.0-next.3] (2020-04-22)",
121 ("10.0.0-next.3", "10.0.0-next.3"),
122 ),
123 ("=== 0.19.1 (Jan 7, 2020)", ("0.19.1", "0.19.1")),
124 ("== 1.0.0", ("1.0.0", "1.0.0")),
125 ("== v1.0.0", ("1.0.0", "v1.0.0")),
126 ("== v1.0.0 - (2012-24-32)", ("1.0.0", "v1.0.0")),
127 ("= version 2020.03.24", ("2020.03.24", "2020.03.24")),
128 ("== [Unreleased]", None),
129 ("All notable changes to this project will be documented in this file.", None),
130 ("= Changelog", None),
131 ("=== Bug Fixes", None),
132 ]
133
134
135 @pytest.mark.parametrize(("line_from_changelog", "output_version"), VERSIONS_EXAMPLES)
136 def test_changelog_detect_version(
137 line_from_changelog: str, output_version: tuple[str, str] | None, format: AsciiDoc
138 ):
139 version = format.parse_version_from_title(line_from_changelog)
140 assert version == output_version
141
142
143 TITLES_EXAMPLES = [
144 ("== [1.0.0] - 2017-06-20", 2),
145 ("== [Unreleased]", 2),
146 ("= Unreleased", 1),
147 ]
148
149
150 @pytest.mark.parametrize(("line_from_changelog", "output_title"), TITLES_EXAMPLES)
151 def test_parse_title_type_of_line(
152 line_from_changelog: str, output_title: str, format: AsciiDoc
153 ):
154 title = format.parse_title_level(line_from_changelog)
155 assert title == output_title
156
157
158 @pytest.mark.parametrize(
159 ("content", "expected"),
160 [
161 pytest.param(CHANGELOG_A, EXPECTED_A, id="A"),
162 pytest.param(CHANGELOG_B, EXPECTED_B, id="B"),
163 pytest.param(CHANGELOG_C, EXPECTED_C, id="C"),
164 pytest.param(CHANGELOG_D, EXPECTED_D, id="D"),
165 ],
166 )
167 def test_get_metadata(
168 tmp_path: Path, format: AsciiDoc, content: str, expected: Metadata
169 ):
170 changelog = tmp_path / format.default_changelog_file
171 changelog.write_text(content)
172
173 assert format.get_metadata(str(changelog)) == expected
174
175
176 def test_get_latest_full_release_no_file(format: AsciiDoc):
177 assert format.get_latest_full_release("/nonexistent") == IncrementalMergeInfo()
178
179
180 @pytest.mark.parametrize(
181 ("format_with_tags", "tag_string", "expected"),
182 [
183 pytest.param("${version}-example", "1.0.0-example", "1.0.0"),
184 pytest.param("${version}example", "1.0.0example", "1.0.0"),
185 pytest.param("example${version}", "example1.0.0", "1.0.0"),
186 pytest.param("example-${version}", "example-1.0.0", "1.0.0"),
187 pytest.param("example-${major}-${minor}-${patch}", "example-1-0-0", "1.0.0"),
188 pytest.param("example-${major}-${minor}", "example-1-0-0", None),
189 pytest.param(
190 "${major}-${minor}-${patch}-${prerelease}-example",
191 "1-0-0-rc1-example",
192 "1.0.0-rc1",
193 ),
194 pytest.param(
195 "${major}-${minor}-${patch}-${prerelease}${devrelease}-example",
196 "1-0-0-a1.dev1-example",
197 "1.0.0-a1.dev1",
198 ),
199 pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"),
200 ],
201 indirect=["format_with_tags"],
202 )
203 def test_get_metadata_custom_tag_format(
204 tmp_path: Path, format_with_tags: AsciiDoc, tag_string: str, expected: Metadata
205 ):
206 content = CHANGELOG_E.format(tag_formatted_version=tag_string)
207 changelog = tmp_path / format_with_tags.default_changelog_file
208 changelog.write_text(content)
209 assert format_with_tags.get_metadata(str(changelog)).latest_version == expected