| 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.textile import Textile |
| 9 |
|
| 10 |
if TYPE_CHECKING: |
| 11 |
from pathlib import Path |
| 12 |
|
| 13 |
from commitizen.config.base_config import BaseConfig |
| 14 |
|
| 15 |
CHANGELOG_A = """ |
| 16 |
h1. 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 |
h2. [Unreleased] |
| 24 |
- Start using "changelog" over "change log" since it's the common usage. |
| 25 |
|
| 26 |
h2. [1.0.0] - 2017-06-20 |
| 27 |
h3. 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 |
h2. [Unreleased] |
| 42 |
* Start using "changelog" over "change log" since it's the common usage. |
| 43 |
|
| 44 |
h2. 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 |
h1. Unreleased |
| 57 |
|
| 58 |
h2. 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 |
h2. 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 |
h1. 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 |
h2. [Unreleased] |
| 89 |
- Start using "changelog" over "change log" since it's the common usage. |
| 90 |
|
| 91 |
h2. [{tag_formatted_version}] - 2017-06-20 |
| 92 |
h3. Added |
| 93 |
* New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). |
| 94 |
* Version navigation. |
| 95 |
""".strip() |
| 96 |
|
| 97 |
|
| 98 |
@pytest.fixture |
| 99 |
def format(config: BaseConfig) -> Textile: |
| 100 |
return Textile(config) |
| 101 |
|
| 102 |
|
| 103 |
@pytest.fixture |
| 104 |
def format_with_tags(config: BaseConfig, request) -> Textile: |
| 105 |
config.settings["tag_format"] = request.param |
| 106 |
config.settings["legacy_tag_formats"] = ["legacy-${version}"] |
| 107 |
return Textile(config) |
| 108 |
|
| 109 |
|
| 110 |
VERSIONS_EXAMPLES = [ |
| 111 |
("h2. [1.0.0] - 2017-06-20", ("1.0.0", "1.0.0")), |
| 112 |
( |
| 113 |
'h1. "10.0.0-next.3":https://github.com/angular/angular/compare/10.0.0-next.2...10.0.0-next.3 (2020-04-22)', |
| 114 |
("10.0.0-next.3", "10.0.0-next.3"), |
| 115 |
), |
| 116 |
("h3. 0.19.1 (Jan 7, 2020)", ("0.19.1", "0.19.1")), |
| 117 |
("h2. 1.0.0", ("1.0.0", "1.0.0")), |
| 118 |
("h2. v1.0.0", ("1.0.0", "v1.0.0")), |
| 119 |
("h2. v1.0.0 - (2012-24-32)", ("1.0.0", "v1.0.0")), |
| 120 |
("h1. version 2020.03.24", ("2020.03.24", "2020.03.24")), |
| 121 |
("h2. [Unreleased]", None), |
| 122 |
("All notable changes to this project will be documented in this file.", None), |
| 123 |
("h1. Changelog", None), |
| 124 |
("h3. Bug Fixes", None), |
| 125 |
] |
| 126 |
|
| 127 |
|
| 128 |
@pytest.mark.parametrize(("line_from_changelog", "output_version"), VERSIONS_EXAMPLES) |
| 129 |
def test_changelog_detect_version( |
| 130 |
line_from_changelog: str, output_version: tuple[str, str] | None, format: Textile |
| 131 |
): |
| 132 |
version = format.parse_version_from_title(line_from_changelog) |
| 133 |
assert version == output_version |
| 134 |
|
| 135 |
|
| 136 |
TITLES_EXAMPLES = [ |
| 137 |
("h2. [1.0.0] - 2017-06-20", 2), |
| 138 |
("h2. [Unreleased]", 2), |
| 139 |
("h1. Unreleased", 1), |
| 140 |
] |
| 141 |
|
| 142 |
|
| 143 |
@pytest.mark.parametrize(("line_from_changelog", "output_title"), TITLES_EXAMPLES) |
| 144 |
def test_parse_title_type_of_line( |
| 145 |
line_from_changelog: str, output_title: str, format: Textile |
| 146 |
): |
| 147 |
title = format.parse_title_level(line_from_changelog) |
| 148 |
assert title == output_title |
| 149 |
|
| 150 |
|
| 151 |
@pytest.mark.parametrize( |
| 152 |
("content", "expected"), |
| 153 |
[ |
| 154 |
pytest.param(CHANGELOG_A, EXPECTED_A, id="A"), |
| 155 |
pytest.param(CHANGELOG_B, EXPECTED_B, id="B"), |
| 156 |
pytest.param(CHANGELOG_C, EXPECTED_C, id="C"), |
| 157 |
pytest.param(CHANGELOG_D, EXPECTED_D, id="D"), |
| 158 |
], |
| 159 |
) |
| 160 |
def test_get_metadata( |
| 161 |
tmp_path: Path, format: Textile, content: str, expected: Metadata |
| 162 |
): |
| 163 |
changelog = tmp_path / format.default_changelog_file |
| 164 |
changelog.write_text(content) |
| 165 |
|
| 166 |
assert format.get_metadata(str(changelog)) == expected |
| 167 |
|
| 168 |
|
| 169 |
@pytest.mark.parametrize( |
| 170 |
("format_with_tags", "tag_string", "expected"), |
| 171 |
[ |
| 172 |
pytest.param("${version}-example", "1.0.0-example", "1.0.0"), |
| 173 |
pytest.param("${version}example", "1.0.0example", "1.0.0"), |
| 174 |
pytest.param("example${version}", "example1.0.0", "1.0.0"), |
| 175 |
pytest.param("example-${version}", "example-1.0.0", "1.0.0"), |
| 176 |
pytest.param("example-${major}-${minor}-${patch}", "example-1-0-0", "1.0.0"), |
| 177 |
pytest.param("example-${major}-${minor}", "example-1-0-0", None), |
| 178 |
pytest.param( |
| 179 |
"${major}-${minor}-${patch}-${prerelease}-example", |
| 180 |
"1-0-0-rc1-example", |
| 181 |
"1.0.0-rc1", |
| 182 |
), |
| 183 |
pytest.param( |
| 184 |
"${major}-${minor}-${patch}-${prerelease}${devrelease}-example", |
| 185 |
"1-0-0-a1.dev1-example", |
| 186 |
"1.0.0-a1.dev1", |
| 187 |
), |
| 188 |
pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), |
| 189 |
], |
| 190 |
indirect=["format_with_tags"], |
| 191 |
) |
| 192 |
def test_get_metadata_custom_tag_format( |
| 193 |
tmp_path: Path, format_with_tags: Textile, tag_string: str, expected: Metadata |
| 194 |
): |
| 195 |
content = CHANGELOG_E.format(tag_formatted_version=tag_string) |
| 196 |
changelog = tmp_path / format_with_tags.default_changelog_file |
| 197 |
changelog.write_text(content) |
| 198 |
|
| 199 |
assert format_with_tags.get_metadata(str(changelog)).latest_version == expected |