| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from textwrap import dedent |
| 4 |
from typing import TYPE_CHECKING |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from commitizen.changelog import Metadata |
| 9 |
from commitizen.changelog_formats.restructuredtext import ( |
| 10 |
RestructuredText, |
| 11 |
_is_overlined_title, |
| 12 |
_is_underlined_title, |
| 13 |
) |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from pathlib import Path |
| 17 |
|
| 18 |
from _pytest.mark.structures import ParameterSet |
| 19 |
|
| 20 |
from commitizen.config.base_config import BaseConfig |
| 21 |
|
| 22 |
|
| 23 |
CASES: list[ParameterSet] = [] |
| 24 |
|
| 25 |
|
| 26 |
def case( |
| 27 |
id: str, |
| 28 |
content: str, |
| 29 |
latest_version: str | None = None, |
| 30 |
latest_version_position: int | None = None, |
| 31 |
latest_version_tag: str | None = None, |
| 32 |
unreleased_start: int | None = None, |
| 33 |
unreleased_end: int | None = None, |
| 34 |
): |
| 35 |
CASES.append( |
| 36 |
pytest.param( |
| 37 |
dedent(content).strip(), |
| 38 |
Metadata( |
| 39 |
latest_version=latest_version, |
| 40 |
latest_version_tag=latest_version_tag, |
| 41 |
latest_version_position=latest_version_position, |
| 42 |
unreleased_start=unreleased_start, |
| 43 |
unreleased_end=unreleased_end, |
| 44 |
), |
| 45 |
id=id, |
| 46 |
) |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
case( |
| 51 |
"underlined title with intro and unreleased section", |
| 52 |
""" |
| 53 |
Changelog |
| 54 |
######### |
| 55 |
|
| 56 |
All notable changes to this project will be documented in this file. |
| 57 |
|
| 58 |
The format is based on `Keep a Changelog <https://keepachangelog.com/en/1.0.0/>`, |
| 59 |
and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0.html>`. |
| 60 |
|
| 61 |
Unreleased |
| 62 |
========== |
| 63 |
* Start using "changelog" over "change log" since it's the common usage. |
| 64 |
|
| 65 |
1.0.0 - 2017-06-20 |
| 66 |
================== |
| 67 |
Added |
| 68 |
----- |
| 69 |
* New visual identity by `@tylerfortune8 <https://github.com/tylerfortune8>`. |
| 70 |
* Version navigation. |
| 71 |
""", |
| 72 |
latest_version="1.0.0", |
| 73 |
latest_version_position=12, |
| 74 |
unreleased_start=8, |
| 75 |
unreleased_end=12, |
| 76 |
) |
| 77 |
|
| 78 |
case( |
| 79 |
"unreleased section without preamble", |
| 80 |
""" |
| 81 |
Unreleased |
| 82 |
========== |
| 83 |
* Start using "changelog" over "change log" since it's the common usage. |
| 84 |
|
| 85 |
1.2.0 |
| 86 |
===== |
| 87 |
""", |
| 88 |
latest_version="1.2.0", |
| 89 |
latest_version_position=4, |
| 90 |
unreleased_start=0, |
| 91 |
unreleased_end=4, |
| 92 |
) |
| 93 |
|
| 94 |
case( |
| 95 |
"basic underlined titles with v-prefixed version", |
| 96 |
""" |
| 97 |
Unreleased |
| 98 |
========== |
| 99 |
|
| 100 |
v1.0.0 |
| 101 |
====== |
| 102 |
""", |
| 103 |
latest_version="1.0.0", |
| 104 |
latest_version_tag="v1.0.0", |
| 105 |
latest_version_position=3, |
| 106 |
unreleased_start=0, |
| 107 |
unreleased_end=3, |
| 108 |
) |
| 109 |
|
| 110 |
case( |
| 111 |
"intermediate section in unreleased", |
| 112 |
""" |
| 113 |
Unreleased |
| 114 |
========== |
| 115 |
|
| 116 |
intermediate |
| 117 |
------------ |
| 118 |
|
| 119 |
1.0.0 |
| 120 |
===== |
| 121 |
""", |
| 122 |
latest_version="1.0.0", |
| 123 |
latest_version_position=6, |
| 124 |
unreleased_start=0, |
| 125 |
unreleased_end=6, |
| 126 |
) |
| 127 |
|
| 128 |
case( |
| 129 |
"weird section with different level than versions", |
| 130 |
""" |
| 131 |
Unreleased |
| 132 |
########## |
| 133 |
|
| 134 |
1.0.0 |
| 135 |
===== |
| 136 |
""", |
| 137 |
latest_version="1.0.0", |
| 138 |
latest_version_position=3, |
| 139 |
unreleased_start=0, |
| 140 |
unreleased_end=3, |
| 141 |
) |
| 142 |
|
| 143 |
case( |
| 144 |
"overlined title without release and intro", |
| 145 |
""" |
| 146 |
========== |
| 147 |
Unreleased |
| 148 |
========== |
| 149 |
* Start using "changelog" over "change log" since it's the common usage. |
| 150 |
""", |
| 151 |
unreleased_start=0, |
| 152 |
unreleased_end=4, |
| 153 |
) |
| 154 |
|
| 155 |
case( |
| 156 |
"underlined title with date", |
| 157 |
""" |
| 158 |
1.0.0 - 2017-06-20 |
| 159 |
================== |
| 160 |
""", |
| 161 |
latest_version="1.0.0", |
| 162 |
latest_version_position=0, |
| 163 |
) |
| 164 |
|
| 165 |
|
| 166 |
UNDERLINED_TITLES = ( |
| 167 |
""" |
| 168 |
title |
| 169 |
===== |
| 170 |
""", |
| 171 |
""" |
| 172 |
title |
| 173 |
====== |
| 174 |
""", |
| 175 |
""" |
| 176 |
title |
| 177 |
##### |
| 178 |
""", |
| 179 |
""" |
| 180 |
title |
| 181 |
..... |
| 182 |
""", |
| 183 |
""" |
| 184 |
title |
| 185 |
!!!!! |
| 186 |
""", |
| 187 |
) |
| 188 |
|
| 189 |
NOT_UNDERLINED_TITLES = ( |
| 190 |
""" |
| 191 |
title |
| 192 |
=.=.= |
| 193 |
""", |
| 194 |
""" |
| 195 |
title |
| 196 |
==== |
| 197 |
""", |
| 198 |
""" |
| 199 |
title |
| 200 |
aaaaa |
| 201 |
""", |
| 202 |
""" |
| 203 |
title |
| 204 |
|
| 205 |
""", |
| 206 |
) |
| 207 |
|
| 208 |
|
| 209 |
OVERLINED_TITLES = ( |
| 210 |
""" |
| 211 |
===== |
| 212 |
title |
| 213 |
===== |
| 214 |
""", |
| 215 |
""" |
| 216 |
====== |
| 217 |
title |
| 218 |
====== |
| 219 |
""", |
| 220 |
""" |
| 221 |
##### |
| 222 |
title |
| 223 |
##### |
| 224 |
""", |
| 225 |
""" |
| 226 |
..... |
| 227 |
title |
| 228 |
..... |
| 229 |
""", |
| 230 |
) |
| 231 |
|
| 232 |
NOT_OVERLINED_TITLES = ( |
| 233 |
""" |
| 234 |
==== |
| 235 |
title |
| 236 |
===== |
| 237 |
""", |
| 238 |
""" |
| 239 |
===== |
| 240 |
title |
| 241 |
==== |
| 242 |
""", |
| 243 |
""" |
| 244 |
==== |
| 245 |
title |
| 246 |
==== |
| 247 |
""", |
| 248 |
""" |
| 249 |
===== |
| 250 |
title |
| 251 |
##### |
| 252 |
""", |
| 253 |
""" |
| 254 |
##### |
| 255 |
title |
| 256 |
===== |
| 257 |
""", |
| 258 |
""" |
| 259 |
=.=.= |
| 260 |
title |
| 261 |
===== |
| 262 |
""", |
| 263 |
""" |
| 264 |
===== |
| 265 |
title |
| 266 |
=.=.= |
| 267 |
""", |
| 268 |
""" |
| 269 |
|
| 270 |
title |
| 271 |
===== |
| 272 |
""", |
| 273 |
""" |
| 274 |
===== |
| 275 |
title |
| 276 |
|
| 277 |
""", |
| 278 |
""" |
| 279 |
aaaaa |
| 280 |
title |
| 281 |
aaaaa |
| 282 |
""", |
| 283 |
) |
| 284 |
|
| 285 |
CHANGELOG = """ |
| 286 |
Changelog |
| 287 |
######### |
| 288 |
|
| 289 |
All notable changes to this project will be documented in this file. |
| 290 |
|
| 291 |
The format is based on `Keep a Changelog <https://keepachangelog.com/en/1.0.0/>`, |
| 292 |
and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0.html>`. |
| 293 |
|
| 294 |
Unreleased |
| 295 |
========== |
| 296 |
* Start using "changelog" over "change log" since it's the common usage. |
| 297 |
|
| 298 |
{tag_formatted_version} - 2017-06-20 |
| 299 |
{underline} |
| 300 |
Added |
| 301 |
----- |
| 302 |
* New visual identity by `@tylerfortune8 <https://github.com/tylerfortune8>`. |
| 303 |
* Version navigation. |
| 304 |
""".strip() |
| 305 |
|
| 306 |
|
| 307 |
@pytest.fixture |
| 308 |
def format(config: BaseConfig) -> RestructuredText: |
| 309 |
return RestructuredText(config) |
| 310 |
|
| 311 |
|
| 312 |
@pytest.fixture |
| 313 |
def format_with_tags(config: BaseConfig, request) -> RestructuredText: |
| 314 |
config.settings["tag_format"] = request.param |
| 315 |
config.settings["legacy_tag_formats"] = ["legacy-${version}"] |
| 316 |
return RestructuredText(config) |
| 317 |
|
| 318 |
|
| 319 |
@pytest.mark.parametrize(("content", "expected"), CASES) |
| 320 |
def test_get_metadata( |
| 321 |
tmp_path: Path, format: RestructuredText, content: str, expected: Metadata |
| 322 |
): |
| 323 |
changelog = tmp_path / format.default_changelog_file |
| 324 |
changelog.write_text(content) |
| 325 |
|
| 326 |
assert format.get_metadata(str(changelog)) == expected |
| 327 |
|
| 328 |
|
| 329 |
@pytest.mark.parametrize( |
| 330 |
("text", "expected"), |
| 331 |
[(text, True) for text in UNDERLINED_TITLES] |
| 332 |
+ [(text, False) for text in NOT_UNDERLINED_TITLES], |
| 333 |
) |
| 334 |
def test_is_underlined_title(text: str, expected: bool): |
| 335 |
_, first, second = dedent(text).splitlines() |
| 336 |
assert _is_underlined_title(first, second) is expected |
| 337 |
|
| 338 |
|
| 339 |
@pytest.mark.parametrize( |
| 340 |
("text", "expected"), |
| 341 |
[(text, True) for text in OVERLINED_TITLES] |
| 342 |
+ [(text, False) for text in NOT_OVERLINED_TITLES], |
| 343 |
) |
| 344 |
def test_is_overlined_title(text: str, expected: bool): |
| 345 |
_, first, second, third = dedent(text).splitlines() |
| 346 |
|
| 347 |
assert _is_overlined_title(first, second, third) is expected |
| 348 |
|
| 349 |
|
| 350 |
@pytest.mark.parametrize( |
| 351 |
("format_with_tags", "tag_string", "expected"), |
| 352 |
[ |
| 353 |
pytest.param("${version}-example", "1.0.0-example", "1.0.0"), |
| 354 |
pytest.param("${version}", "1.0.0", "1.0.0"), |
| 355 |
pytest.param("${version}example", "1.0.0example", "1.0.0"), |
| 356 |
pytest.param("example${version}", "example1.0.0", "1.0.0"), |
| 357 |
pytest.param("example-${version}", "example-1.0.0", "1.0.0"), |
| 358 |
pytest.param("example-${major}-${minor}-${patch}", "example-1-0-0", "1.0.0"), |
| 359 |
pytest.param("example-${major}-${minor}", "example-1-0-0", None), |
| 360 |
pytest.param( |
| 361 |
"${major}-${minor}-${patch}-${prerelease}-example", |
| 362 |
"1-0-0-rc1-example", |
| 363 |
"1.0.0-rc1", |
| 364 |
), |
| 365 |
pytest.param( |
| 366 |
"${major}-${minor}-${patch}-${prerelease}${devrelease}-example", |
| 367 |
"1-0-0-a1.dev1-example", |
| 368 |
"1.0.0-a1.dev1", |
| 369 |
), |
| 370 |
pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), |
| 371 |
], |
| 372 |
indirect=["format_with_tags"], |
| 373 |
) |
| 374 |
def test_get_metadata_custom_tag_format( |
| 375 |
tmp_path: Path, |
| 376 |
format_with_tags: RestructuredText, |
| 377 |
tag_string: str, |
| 378 |
expected: Metadata, |
| 379 |
): |
| 380 |
content = CHANGELOG.format( |
| 381 |
tag_formatted_version=tag_string, |
| 382 |
underline="=" * len(tag_string) + "=============", |
| 383 |
) |
| 384 |
changelog = tmp_path / format_with_tags.default_changelog_file |
| 385 |
changelog.write_text(content) |
| 386 |
|
| 387 |
assert format_with_tags.get_metadata(str(changelog)).latest_version == expected |