-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathconftest.py
More file actions
315 lines (244 loc) · 8.77 KB
/
conftest.py
File metadata and controls
315 lines (244 loc) · 8.77 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from __future__ import annotations
import os
import re
import sys
import tempfile
from pathlib import Path
from typing import TYPE_CHECKING, cast
import pytest
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
if TYPE_CHECKING:
from collections.abc import Iterator, Mapping
from pytest_mock import MockerFixture
from commitizen.question import CzQuestion
from tests.utils import UtilFixture
SIGNER = "GitHub Action"
SIGNER_MAIL = "action@github.com"
PYTHON_VERSION = f"{sys.version_info.major}.{sys.version_info.minor}"
pytest_plugins = [
"tests.utils",
]
@pytest.fixture
def repo_root() -> Path:
return Path(__file__).parent.parent
@pytest.fixture
def in_repo_root(repo_root: Path) -> Iterator[Path]:
cwd = os.getcwd()
os.chdir(repo_root)
yield repo_root
os.chdir(cwd)
@pytest.fixture
def data_dir(repo_root: Path) -> Path:
return repo_root / "tests" / "data"
@pytest.fixture(scope="session")
def set_default_gitconfig() -> dict[str, str]:
return {
"user.name": "SIGNER",
"user.email": SIGNER_MAIL,
"safe.directory": "*",
"init.defaultBranch": "master",
}
@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, util: UtilFixture):
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")
util.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[CzQuestion]:
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}"
def example(self) -> str:
return ""
def schema(self) -> str:
return ""
def schema_pattern(self) -> str:
return ""
def info(self) -> str:
return ""
@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 ""
def example(self) -> str:
return ""
def schema(self) -> str:
return ""
def schema_pattern(self) -> str:
return ""
def info(self) -> 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)
@pytest.fixture(params=[pytest.param(PYTHON_VERSION, id=f"py_{PYTHON_VERSION}")])
def python_version(request: pytest.FixtureRequest) -> str:
"""The current python version in '{major}.{minor}' format"""
return cast("str", request.param)
@pytest.fixture
def consistent_terminal_output(monkeypatch: pytest.MonkeyPatch):
"""Force consistent terminal output."""
monkeypatch.setenv("COLUMNS", "80")
monkeypatch.setenv("TERM", "dumb")
monkeypatch.setenv("LC_ALL", "C")
monkeypatch.setenv("LANG", "C")
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("PAGER", "cat")