1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
from __future__ import annotations
import os
import re
import tempfile
from collections.abc import Iterator, Mapping
from pathlib import Path
import pytest
from pytest_mock import MockerFixture
from commitizen import cmd, defaults
from commitizen.changelog_formats import (
ChangelogFormat,
get_changelog_format,
)
from commitizen.config import BaseConfig
from commitizen.cz import registry
from commitizen.cz.base import BaseCommitizen
from commitizen.question import CzQuestion
from tests.utils import create_file_and_commit
SIGNER = "GitHub Action"
SIGNER_MAIL = "action@github.com"
@pytest.fixture(autouse=True)
def git_sandbox(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
"""Ensure git commands are executed without the current user settings"""
# Clear any GIT_ prefixed environment variable
for var in os.environ:
if var.startswith("GIT_"):
monkeypatch.delenv(var)
# Define a dedicated temporary git config
gitconfig = tmp_path / ".git" / "config"
if not gitconfig.parent.exists():
gitconfig.parent.mkdir()
monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(gitconfig))
r = cmd.run(f"git config --file {gitconfig} user.name {SIGNER}")
assert r.return_code == 0, r.err
r = cmd.run(f"git config --file {gitconfig} user.email {SIGNER_MAIL}")
assert r.return_code == 0, r.err
r = cmd.run(f"git config --file {gitconfig} safe.directory '*'")
assert r.return_code == 0, r.err
r = cmd.run("git config --global init.defaultBranch master")
assert r.return_code == 0, r.err
@pytest.fixture
def chdir(tmp_path: Path) -> Iterator[Path]:
cwd = os.getcwd()
os.chdir(tmp_path)
yield tmp_path
os.chdir(cwd)
@pytest.fixture(scope="function")
def tmp_git_project(tmpdir):
with tmpdir.as_cwd():
cmd.run("git init")
yield tmpdir
@pytest.fixture(scope="function")
def tmp_commitizen_project(tmp_git_project):
tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml")
tmp_commitizen_cfg_file.write('[tool.commitizen]\nversion="0.1.0"\n')
yield tmp_git_project
@pytest.fixture(scope="function")
def tmp_commitizen_project_initial(tmp_git_project):
def _initial(
config_extra: str | None = None,
version="0.1.0",
initial_commit="feat: new user interface",
):
with tmp_git_project.as_cwd():
tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml")
tmp_commitizen_cfg_file.write(f'[tool.commitizen]\nversion="{version}"\n')
tmp_version_file = tmp_git_project.join("__version__.py")
tmp_version_file.write(version)
tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml")
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
tmp_commitizen_cfg_file.write(
f"{tmp_commitizen_cfg_file.read()}\n"
f'version_files = ["{tmp_version_file_string}"]\n'
)
if config_extra:
tmp_commitizen_cfg_file.write(config_extra, mode="a")
create_file_and_commit(initial_commit)
return tmp_git_project
yield _initial
def _get_gpg_keyid(signer_mail):
_new_key = cmd.run(f"gpg --list-secret-keys {signer_mail}")
_m = re.search(
r"[a-zA-Z0-9 \[\]-_]*\n[ ]*([0-9A-Za-z]*)\n[\na-zA-Z0-9 \[\]-_<>@]*",
_new_key.out,
)
return _m.group(1) if _m else None
@pytest.fixture(scope="function")
def tmp_commitizen_project_with_gpg(tmp_commitizen_project):
# create a temporary GPGHOME to store a temporary keyring.
# Home path must be less than 104 characters
gpg_home = tempfile.TemporaryDirectory(suffix="_cz")
if os.name != "nt":
os.environ["GNUPGHOME"] = gpg_home.name # tempdir = temp keyring
# create a key (a keyring will be generated within GPUPGHOME)
c = cmd.run(
f"gpg --batch --yes --debug-quick-random --passphrase '' --quick-gen-key '{SIGNER} {SIGNER_MAIL}'"
)
if c.return_code != 0:
raise Exception(f"gpg keygen failed with err: '{c.err}'")
key_id = _get_gpg_keyid(SIGNER_MAIL)
assert key_id
# configure git to use gpg signing
cmd.run("git config commit.gpgsign true")
cmd.run(f"git config user.signingkey {key_id}")
yield tmp_commitizen_project
@pytest.fixture()
def config():
_config = BaseConfig()
_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
return _config
@pytest.fixture()
def config_path() -> str:
return os.path.join(os.getcwd(), "pyproject.toml")
class SemverCommitizen(BaseCommitizen):
"""A minimal cz rules used to test changelog and bump.
Samples:
```
minor(users): add email to user
major: removed user profile
patch(deps): updated dependency for security
```
"""
bump_pattern = r"^(patch|minor|major)"
bump_map = {
"major": "MAJOR",
"minor": "MINOR",
"patch": "PATCH",
}
bump_map_major_version_zero = {
"major": "MINOR",
"minor": "MINOR",
"patch": "PATCH",
}
changelog_pattern = r"^(patch|minor|major)"
commit_parser = r"^(?P<change_type>patch|minor|major)(?:\((?P<scope>[^()\r\n]*)\)|\()?:?\s(?P<message>.+)"
change_type_map = {
"major": "Breaking Changes",
"minor": "Features",
"patch": "Bugs",
}
def questions(self) -> list:
return [
{
"type": "list",
"name": "prefix",
"message": "Select the type of change you are committing",
"choices": [
{
"value": "patch",
"name": "patch: a bug fix",
"key": "p",
},
{
"value": "minor",
"name": "minor: a new feature, non-breaking",
"key": "m",
},
{
"value": "major",
"name": "major: a breaking change",
"key": "b",
},
],
},
{
"type": "input",
"name": "subject",
"message": (
"Write a short and imperative summary of the code changes: (lower case and no period)\n"
),
},
]
def message(self, answers: Mapping) -> str:
prefix = answers["prefix"]
subject = answers.get("subject", "default message").trim()
return f"{prefix}: {subject}"
@pytest.fixture()
def use_cz_semver(mocker):
new_cz = {**registry, "cz_semver": SemverCommitizen}
mocker.patch.dict("commitizen.cz.registry", new_cz)
class MockPlugin(BaseCommitizen):
def questions(self) -> list[CzQuestion]:
return []
def message(self, answers: Mapping) -> str:
return ""
@pytest.fixture
def mock_plugin(mocker: MockerFixture, config: BaseConfig) -> BaseCommitizen:
mock = MockPlugin(config)
mocker.patch("commitizen.factory.committer_factory", return_value=mock)
return mock
SUPPORTED_FORMATS = ("markdown", "textile", "asciidoc", "restructuredtext")
@pytest.fixture(params=SUPPORTED_FORMATS)
def changelog_format(
config: BaseConfig, request: pytest.FixtureRequest
) -> ChangelogFormat:
"""For tests relying on formats specifics"""
format: str = request.param
config.settings["changelog_format"] = format
if "tmp_commitizen_project" in request.fixturenames:
tmp_commitizen_project = request.getfixturevalue("tmp_commitizen_project")
pyproject = tmp_commitizen_project / "pyproject.toml"
pyproject.write(f'{pyproject.read()}\nchangelog_format = "{format}"\n')
return get_changelog_format(config)
@pytest.fixture
def any_changelog_format(config: BaseConfig) -> ChangelogFormat:
"""For test not relying on formats specifics, use the default"""
config.settings["changelog_format"] = defaults.CHANGELOG_FORMAT
return get_changelog_format(config)
|