| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from commitizen.changelog import Metadata |
| 8 |
from commitizen.changelog_formats.markdown import Markdown |
| 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 [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), |
| 21 |
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). |
| 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 [@tylerfortune8](https://github.com/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 [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), |
| 86 |
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). |
| 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 [@tylerfortune8](https://github.com/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) -> Markdown: |
| 107 |
return Markdown(config) |
| 108 |
|
| 109 |
|
| 110 |
@pytest.fixture |
| 111 |
def format_with_tags(config: BaseConfig, request) -> Markdown: |
| 112 |
config.settings["tag_format"] = request.param |
| 113 |
config.settings["legacy_tag_formats"] = ["legacy-${version}"] |
| 114 |
return Markdown(config) |
| 115 |
|
| 116 |
|
| 117 |
VERSIONS_EXAMPLES = [ |
| 118 |
("## [1.0.0] - 2017-06-20", ("1.0.0", "1.0.0")), |
| 119 |
( |
| 120 |
"# [10.0.0-next.3](https://github.com/angular/angular/compare/10.0.0-next.2...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: Markdown |
| 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: Markdown |
| 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: Markdown, 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 |
@pytest.mark.parametrize( |
| 177 |
("format_with_tags", "tag_string", "expected"), |
| 178 |
[ |
| 179 |
pytest.param("${version}-example", "1.0.0-example", "1.0.0"), |
| 180 |
pytest.param("${version}example", "1.0.0example", "1.0.0"), |
| 181 |
pytest.param("example${version}", "example1.0.0", "1.0.0"), |
| 182 |
pytest.param("example-${version}", "example-1.0.0", "1.0.0"), |
| 183 |
pytest.param("example-${major}-${minor}-${patch}", "example-1-0-0", "1.0.0"), |
| 184 |
pytest.param("example-${major}-${minor}", "example-1-0-0", None), |
| 185 |
pytest.param( |
| 186 |
"${major}-${minor}-${patch}-${prerelease}-example", |
| 187 |
"1-0-0-rc1-example", |
| 188 |
"1.0.0-rc1", |
| 189 |
), |
| 190 |
pytest.param( |
| 191 |
"${major}-${minor}-${patch}-${prerelease}-example", |
| 192 |
"1-0-0-a1-example", |
| 193 |
"1.0.0-a1", |
| 194 |
), |
| 195 |
pytest.param( |
| 196 |
"${major}-${minor}-${patch}-${prerelease}${devrelease}-example", |
| 197 |
"1-0-0-a1.dev1-example", |
| 198 |
"1.0.0-a1.dev1", |
| 199 |
), |
| 200 |
pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), |
| 201 |
], |
| 202 |
indirect=["format_with_tags"], |
| 203 |
) |
| 204 |
def test_get_metadata_custom_tag_format( |
| 205 |
tmp_path: Path, format_with_tags: Markdown, tag_string: str, expected: Metadata |
| 206 |
): |
| 207 |
content = CHANGELOG_E.format(tag_formatted_version=tag_string) |
| 208 |
changelog = tmp_path / format_with_tags.default_changelog_file |
| 209 |
changelog.write_text(content) |
| 210 |
|
| 211 |
assert format_with_tags.get_metadata(str(changelog)).latest_version == expected |