|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | from pytest_mock import MockFixture |
| 7 | +from dateutil import relativedelta |
7 | 8 |
|
8 | 9 | from commitizen import cli, git |
9 | 10 | from commitizen.commands.changelog import Changelog |
|
17 | 18 | from tests.utils import ( |
18 | 19 | create_branch, |
19 | 20 | create_file_and_commit, |
| 21 | + create_tag, |
20 | 22 | get_current_branch, |
21 | 23 | merge_branch, |
22 | 24 | switch_branch, |
@@ -1267,3 +1269,88 @@ def test_changelog_prerelease_rev_with_use_scheme_semver( |
1267 | 1269 | out, _ = capsys.readouterr() |
1268 | 1270 |
|
1269 | 1271 | file_regression.check(out, extension=".second-prerelease.md") |
| 1272 | + |
| 1273 | + |
| 1274 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 1275 | +def test_changelog_uses_version_tags_for_header(mocker: MockFixture, config): |
| 1276 | + """Tests that changelog headers always use version tags even if there are non-version tags |
| 1277 | +
|
| 1278 | + This tests a scenario fixed in this commit: |
| 1279 | + The first header was using a non-version tag and outputting "## 0-not-a-version" instead of "## 1.0.0""" |
| 1280 | + create_file_and_commit("feat: commit in 1.0.0") |
| 1281 | + create_tag("0-not-a-version") |
| 1282 | + create_tag("1.0.0") |
| 1283 | + create_tag("also-not-a-version") |
| 1284 | + |
| 1285 | + write_patch = mocker.patch("commitizen.commands.changelog.out.write") |
| 1286 | + |
| 1287 | + changelog = Changelog( |
| 1288 | + config, {"dry_run": True, "incremental": True, "unreleased_version": None} |
| 1289 | + ) |
| 1290 | + |
| 1291 | + with pytest.raises(DryRunExit): |
| 1292 | + changelog() |
| 1293 | + |
| 1294 | + assert write_patch.call_args[0][0].startswith("## 1.0.0") |
| 1295 | + |
| 1296 | + |
| 1297 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 1298 | +def test_changelog_from_current_version_tag_with_nonversion_tag( |
| 1299 | + mocker: MockFixture, config |
| 1300 | +): |
| 1301 | + """Tests that changelog generation for a single version works even if |
| 1302 | + there is a non-version tag in the list of tags |
| 1303 | +
|
| 1304 | + This tests a scenario which is fixed in this commit: |
| 1305 | + You have a commit in between two versions (1.0.0..2.0.0) which is tagged with a non-version tag (not-a-version). |
| 1306 | + In this case commitizen should disregard the non-version tag when determining the rev-range & generating the changelog. |
| 1307 | + """ |
| 1308 | + create_file_and_commit( |
| 1309 | + "feat: initial commit", |
| 1310 | + committer_date=( |
| 1311 | + datetime.now() - relativedelta.relativedelta(seconds=3) |
| 1312 | + ).isoformat(), |
| 1313 | + ) |
| 1314 | + create_tag("1.0.0") |
| 1315 | + |
| 1316 | + create_file_and_commit( |
| 1317 | + "feat: commit 1", |
| 1318 | + committer_date=( |
| 1319 | + datetime.now() - relativedelta.relativedelta(seconds=2) |
| 1320 | + ).isoformat(), |
| 1321 | + ) |
| 1322 | + create_tag("1-not-a-version") |
| 1323 | + |
| 1324 | + create_file_and_commit( |
| 1325 | + "feat: commit 2", |
| 1326 | + committer_date=( |
| 1327 | + datetime.now() - relativedelta.relativedelta(seconds=1) |
| 1328 | + ).isoformat(), |
| 1329 | + ) |
| 1330 | + |
| 1331 | + create_file_and_commit("bump: version 1.0.0 → 2.0.0") |
| 1332 | + create_tag("2.0.0") |
| 1333 | + |
| 1334 | + mocker.patch("commitizen.git.GitTag.date", "2022-02-13") |
| 1335 | + write_patch = mocker.patch("commitizen.commands.changelog.out.write") |
| 1336 | + |
| 1337 | + changelog = Changelog( |
| 1338 | + config, |
| 1339 | + { |
| 1340 | + "dry_run": True, |
| 1341 | + "incremental": False, |
| 1342 | + "unreleased_version": None, |
| 1343 | + "rev_range": "2.0.0", |
| 1344 | + }, |
| 1345 | + ) |
| 1346 | + |
| 1347 | + with pytest.raises(DryRunExit): |
| 1348 | + changelog() |
| 1349 | + |
| 1350 | + full_changelog = "\ |
| 1351 | +## 2.0.0 (2022-02-13)\n\n\ |
| 1352 | +### Feat\n\n\ |
| 1353 | +- commit 2\n\ |
| 1354 | +- commit 1\n" |
| 1355 | + |
| 1356 | + write_patch.assert_called_with(full_changelog) |
0 commit comments