| 1 |
import sys |
| 2 |
from pathlib import Path |
| 3 |
from textwrap import dedent |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from commitizen import bump, cmd, exceptions |
| 8 |
from tests.utils import UtilFixture |
| 9 |
|
| 10 |
conversion = [ |
| 11 |
( |
| 12 |
("1.2.3", "1.3.0", "bump: $current_version -> $new_version [skip ci]"), |
| 13 |
"bump: 1.2.3 -> 1.3.0 [skip ci]", |
| 14 |
), |
| 15 |
(("1.2.3", "1.3.0", None), "bump: version 1.2.3 → 1.3.0"), |
| 16 |
(("1.2.3", "1.3.0", "release $new_version"), "release 1.3.0"), |
| 17 |
] |
| 18 |
|
| 19 |
|
| 20 |
@pytest.mark.parametrize(("test_input", "expected"), conversion) |
| 21 |
def test_create_tag(test_input, expected): |
| 22 |
current_version, new_version, message_template = test_input |
| 23 |
new_tag = bump.create_commit_message(current_version, new_version, message_template) |
| 24 |
assert new_tag == expected |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize("hook_runner", ["pre-commit", "prek"]) |
| 28 |
@pytest.mark.parametrize( |
| 29 |
"retry", |
| 30 |
[ |
| 31 |
pytest.param( |
| 32 |
True, |
| 33 |
marks=pytest.mark.skipif( |
| 34 |
sys.version_info >= (3, 13), |
| 35 |
reason="mirrors-prettier is not supported with Python 3.13 or higher", |
| 36 |
), |
| 37 |
), |
| 38 |
False, |
| 39 |
], |
| 40 |
) |
| 41 |
@pytest.mark.usefixtures("tmp_commitizen_project") |
| 42 |
def test_bump_pre_commit_changelog(util: UtilFixture, retry, hook_runner): |
| 43 |
util.freezer.move_to("2022-04-01") |
| 44 |
bump_args = ["bump", "--changelog", "--yes"] |
| 45 |
if retry: |
| 46 |
bump_args.append("--retry") |
| 47 |
else: |
| 48 |
pytest.xfail("it will fail because pre-commit will reformat CHANGELOG.md") |
| 49 |
# Configure prettier as a pre-commit hook |
| 50 |
Path(".pre-commit-config.yaml").write_text( |
| 51 |
dedent( |
| 52 |
"""\ |
| 53 |
repos: |
| 54 |
- repo: https://github.com/pre-commit/mirrors-prettier |
| 55 |
rev: v3.0.3 |
| 56 |
hooks: |
| 57 |
- id: prettier |
| 58 |
stages: [commit] |
| 59 |
""" |
| 60 |
) |
| 61 |
) |
| 62 |
# Prettier inherits editorconfig |
| 63 |
Path(".editorconfig").write_text( |
| 64 |
dedent( |
| 65 |
"""\ |
| 66 |
[*] |
| 67 |
indent_size = 4 |
| 68 |
""" |
| 69 |
) |
| 70 |
) |
| 71 |
cmd.run(["git", "add", "-A"]) |
| 72 |
cmd.run(["git", "commit", "-m", "fix: _test"]) |
| 73 |
cmd.run_shell(f"{hook_runner} install") |
| 74 |
util.run_cli(*bump_args) |
| 75 |
# Pre-commit fixed last line adding extra indent and "\" char |
| 76 |
assert Path("CHANGELOG.md").read_text() == dedent( |
| 77 |
"""\ |
| 78 |
## 0.1.1 (2022-04-01) |
| 79 |
|
| 80 |
### Fix |
| 81 |
|
| 82 |
- \\_test |
| 83 |
""" |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.parametrize("hook_runner", ["pre-commit", "prek"]) |
| 88 |
@pytest.mark.parametrize("retry", [True, False]) |
| 89 |
@pytest.mark.usefixtures("tmp_commitizen_project") |
| 90 |
def test_bump_pre_commit_changelog_fails_always(util: UtilFixture, retry, hook_runner): |
| 91 |
util.freezer.move_to("2022-04-01") |
| 92 |
bump_args = ["bump", "--changelog", "--yes"] |
| 93 |
if retry: |
| 94 |
bump_args.append("--retry") |
| 95 |
Path(".pre-commit-config.yaml").write_text( |
| 96 |
dedent( |
| 97 |
"""\ |
| 98 |
repos: |
| 99 |
- repo: local |
| 100 |
hooks: |
| 101 |
- id: forbid-changelog |
| 102 |
name: changelogs are forbidden |
| 103 |
entry: changelogs are forbidden |
| 104 |
language: fail |
| 105 |
files: CHANGELOG.md |
| 106 |
""" |
| 107 |
) |
| 108 |
) |
| 109 |
cmd.run(["git", "add", "-A"]) |
| 110 |
cmd.run(["git", "commit", "-m", "feat: forbid changelogs"]) |
| 111 |
cmd.run_shell(f"{hook_runner} install") |
| 112 |
with pytest.raises(exceptions.BumpCommitFailedError): |
| 113 |
util.run_cli(*bump_args) |
| 114 |
|
| 115 |
|
| 116 |
@pytest.mark.usefixtures("tmp_commitizen_project") |
| 117 |
def test_bump_with_build_metadata(util: UtilFixture): |
| 118 |
def _add_entry(test_str: str, args: list): |
| 119 |
Path(test_str).write_text("") |
| 120 |
cmd.run(["git", "add", "-A"]) |
| 121 |
cmd.run(["git", "commit", "-m", f"fix: test-{test_str}"]) |
| 122 |
cz_args = ["bump", "--changelog", "--yes"] + args |
| 123 |
util.run_cli(*cz_args) |
| 124 |
|
| 125 |
util.freezer.move_to("2024-01-01") |
| 126 |
|
| 127 |
_add_entry("a", ["--build-metadata", "a.b.c"]) |
| 128 |
_add_entry("b", []) |
| 129 |
_add_entry("c", ["--build-metadata", "alongmetadatastring"]) |
| 130 |
_add_entry("d", []) |
| 131 |
|
| 132 |
# Pre-commit fixed last line adding extra indent and "\" char |
| 133 |
assert Path("CHANGELOG.md").read_text() == dedent( |
| 134 |
"""\ |
| 135 |
## 0.1.4 (2024-01-01) |
| 136 |
|
| 137 |
### Fix |
| 138 |
|
| 139 |
- test-d |
| 140 |
|
| 141 |
## 0.1.3+alongmetadatastring (2024-01-01) |
| 142 |
|
| 143 |
### Fix |
| 144 |
|
| 145 |
- test-c |
| 146 |
|
| 147 |
## 0.1.2 (2024-01-01) |
| 148 |
|
| 149 |
### Fix |
| 150 |
|
| 151 |
- test-b |
| 152 |
|
| 153 |
## 0.1.1+a.b.c (2024-01-01) |
| 154 |
|
| 155 |
### Fix |
| 156 |
|
| 157 |
- test-a |
| 158 |
""" |
| 159 |
) |