Skip to content

Commit 59d1432

Browse files
committed
refactor: minor review fixes
1 parent af176ce commit 59d1432

10 files changed

+85
-90
lines changed

commitizen/cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@
207207
"name": ["--version-type"],
208208
"help": "choose version type",
209209
"default": None,
210-
"choices": version_types.types,
210+
"choices": version_types.VERSION_TYPES,
211211
},
212212
],
213213
},

commitizen/commands/bump.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(self, config: BaseConfig, arguments: dict):
6565
version_type = arguments["version_type"] or self.config.settings.get(
6666
"version_type"
6767
)
68-
self.version_type = version_type and version_types.types[version_type]
68+
self.version_type = version_type and version_types.VERSION_TYPES[version_type]
6969

7070
def is_initial_tag(self, current_tag_version: str, is_yes: bool = False) -> bool:
7171
"""Check if reading the whole git tree up to HEAD is needed."""

commitizen/commands/changelog.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, config: BaseConfig, args):
5454
) or self.config.settings.get("changelog_merge_prerelease")
5555

5656
version_type = self.config.settings.get("version_type")
57-
self.version_type = version_type and version_types.types[version_type]
57+
self.version_type = version_type and version_types.VERSION_TYPES[version_type]
5858

5959
def _find_incremental_rev(self, latest_version: str, tags: List[GitTag]) -> str:
6060
"""Try to find the 'start_rev'.

commitizen/version_types.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
import typing
2+
from typing import Optional, Tuple, Union
33

44
if sys.version_info >= (3, 8):
55
from typing import Protocol as _Protocol
@@ -10,26 +10,26 @@
1010

1111

1212
class VersionProtocol(_Protocol):
13-
def __init__(self, _version: typing.Union[Version, str]):
13+
def __init__(self, _version: Union[Version, str]):
1414
raise NotImplementedError("must be implemented")
1515

1616
def __str__(self) -> str:
1717
raise NotImplementedError("must be implemented")
1818

1919
@property
20-
def release(self) -> typing.Tuple[int, ...]:
20+
def release(self) -> Tuple[int, ...]:
2121
raise NotImplementedError("must be implemented")
2222

2323
@property
2424
def is_prerelease(self) -> bool:
2525
raise NotImplementedError("must be implemented")
2626

2727
@property
28-
def pre(self) -> typing.Optional[typing.Tuple[str, int]]:
28+
def pre(self) -> Optional[Tuple[str, int]]:
2929
raise NotImplementedError("must be implemented")
3030

3131
@property
32-
def local(self) -> typing.Optional[str]:
32+
def local(self) -> Optional[str]:
3333
raise NotImplementedError("must be implemented")
3434

3535
@property
@@ -42,19 +42,19 @@ def __init__(self, version: str):
4242
self._version = Version(version)
4343

4444
@property
45-
def release(self) -> typing.Tuple[int, ...]:
45+
def release(self) -> Tuple[int, ...]:
4646
return self._version.release
4747

4848
@property
4949
def is_prerelease(self) -> bool:
5050
return self._version.is_prerelease
5151

5252
@property
53-
def pre(self) -> typing.Optional[typing.Tuple[str, int]]:
53+
def pre(self) -> Optional[Tuple[str, int]]:
5454
return self._version.pre
5555

5656
@property
57-
def local(self) -> typing.Optional[str]:
57+
def local(self) -> Optional[str]:
5858
return self._version.local
5959

6060
@property
@@ -74,7 +74,7 @@ def __str__(self) -> str:
7474
parts.append(".".join(str(x) for x in version.release))
7575

7676
# Pre-release
77-
if version.pre is not None:
77+
if version.pre:
7878
pre = "".join(str(x) for x in version.pre)
7979
parts.append(f"-{pre}")
8080

@@ -87,13 +87,13 @@ def __str__(self) -> str:
8787
parts.append(f"-dev{version.dev}")
8888

8989
# Local version segment
90-
if version.local is not None:
90+
if version.local:
9191
parts.append(f"+{version.local}")
9292

9393
return "".join(parts)
9494

9595

96-
types = {
96+
VERSION_TYPES = {
9797
"pep440": Version,
9898
"semver": SemVerVersion,
9999
}

docs/config.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
| `use_shortcuts` | `bool` | `false` | If enabled, commitizen will show keyboard shortcuts when selecting from a list. Define a `key` for each of your choices to set the key. [See more][shortcuts] |
2424
| `major_version_zero` | `bool` | `false` | When true, breaking changes on a `0.x` will remain as a `0.x` version. On `false`, a breaking change will bump a `0.x` version to `1.0`. [major-version-zero] |
2525
| `prerelease_offset` | `int` | `0` | In special cases it may be necessary that a prerelease cannot start with a 0, e.g. in an embedded project the individual characters are encoded in bytes. This can be done by specifying an offset from which to start counting. [prerelease-offset] |
26-
| `version_type` | `str` | `pep440` | Select a version type from the following options [`pep440`, `semver`]. Useful for non python projects. [See more][version_type] |
26+
| `version_type` | `str` | `pep440` | Select a version type from the following options [`pep440`, `semver`]. Useful for non-python projects. [See more][version_type] |
2727

2828
## pyproject.toml or .cz.toml
2929

tests/commands/test_bump_command.py

+35-73
Original file line numberDiff line numberDiff line change
@@ -855,20 +855,12 @@ def test_bump_use_version_provider(mocker: MockFixture):
855855
mock.set_version.assert_called_once_with("0.0.1")
856856

857857

858-
def test_bump_command_prelease_version_provider_via_cli(
859-
tmp_commitizen_project, mocker: MockFixture
858+
def test_bump_command_prelease_version_type_via_cli(
859+
tmp_commitizen_project_initial, mocker: MockFixture
860860
):
861-
# PRERELEASE
861+
tmp_commitizen_project = tmp_commitizen_project_initial()
862862
tmp_version_file = tmp_commitizen_project.join("__version__.py")
863-
tmp_version_file.write("0.1.0")
864863
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
865-
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
866-
tmp_commitizen_cfg_file.write(
867-
f"{tmp_commitizen_cfg_file.read()}\n"
868-
f'version_files = ["{tmp_version_file_string}"]'
869-
)
870-
871-
create_file_and_commit("feat: new user interface")
872864

873865
testargs = [
874866
"cz",
@@ -885,11 +877,9 @@ def test_bump_command_prelease_version_provider_via_cli(
885877
tag_exists = git.tag_exist("0.2.0-a0")
886878
assert tag_exists is True
887879

888-
with open(tmp_version_file, "r") as f:
889-
assert "0.2.0-a0" in f.read()
890-
891-
with open(tmp_commitizen_cfg_file, "r") as f:
892-
assert "0.2.0-a0" in f.read()
880+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
881+
with open(version_file, "r") as f:
882+
assert "0.2.0-a0" in f.read()
893883

894884
# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
895885
testargs = ["cz", "bump", "--yes"]
@@ -899,28 +889,19 @@ def test_bump_command_prelease_version_provider_via_cli(
899889
tag_exists = git.tag_exist("0.2.0")
900890
assert tag_exists is True
901891

902-
with open(tmp_version_file, "r") as f:
903-
assert "0.2.0" in f.read()
904-
905-
with open(tmp_commitizen_cfg_file, "r") as f:
906-
assert "0.2.0" in f.read()
892+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
893+
with open(version_file, "r") as f:
894+
assert "0.2.0" in f.read()
907895

908896

909897
def test_bump_command_prelease_version_type_via_config(
910-
tmp_commitizen_project, mocker: MockFixture
898+
tmp_commitizen_project_initial, mocker: MockFixture
911899
):
912-
# PRERELEASE
900+
tmp_commitizen_project = tmp_commitizen_project_initial(
901+
config_extra='version_type = "semver"\n',
902+
)
913903
tmp_version_file = tmp_commitizen_project.join("__version__.py")
914-
tmp_version_file.write("0.1.0")
915904
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
916-
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
917-
tmp_commitizen_cfg_file.write(
918-
f"{tmp_commitizen_cfg_file.read()}\n"
919-
f'version_files = ["{tmp_version_file_string}"]\n'
920-
f'version_type = "semver"'
921-
)
922-
923-
create_file_and_commit("feat: new user interface")
924905

925906
testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
926907
mocker.patch.object(sys, "argv", testargs)
@@ -929,11 +910,9 @@ def test_bump_command_prelease_version_type_via_config(
929910
tag_exists = git.tag_exist("0.2.0-a0")
930911
assert tag_exists is True
931912

932-
with open(tmp_version_file, "r") as f:
933-
assert "0.2.0-a0" in f.read()
934-
935-
with open(tmp_commitizen_cfg_file, "r") as f:
936-
assert "0.2.0-a0" in f.read()
913+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
914+
with open(version_file, "r") as f:
915+
assert "0.2.0-a0" in f.read()
937916

938917
testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
939918
mocker.patch.object(sys, "argv", testargs)
@@ -942,11 +921,9 @@ def test_bump_command_prelease_version_type_via_config(
942921
tag_exists = git.tag_exist("0.2.0-a1")
943922
assert tag_exists is True
944923

945-
with open(tmp_version_file, "r") as f:
946-
assert "0.2.0-a1" in f.read()
947-
948-
with open(tmp_commitizen_cfg_file, "r") as f:
949-
assert "0.2.0-a1" in f.read()
924+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
925+
with open(version_file, "r") as f:
926+
assert "0.2.0-a1" in f.read()
950927

951928
# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
952929
testargs = ["cz", "bump", "--yes"]
@@ -956,28 +933,19 @@ def test_bump_command_prelease_version_type_via_config(
956933
tag_exists = git.tag_exist("0.2.0")
957934
assert tag_exists is True
958935

959-
with open(tmp_version_file, "r") as f:
960-
assert "0.2.0" in f.read()
961-
962-
with open(tmp_commitizen_cfg_file, "r") as f:
963-
assert "0.2.0" in f.read()
936+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
937+
with open(version_file, "r") as f:
938+
assert "0.2.0" in f.read()
964939

965940

966941
def test_bump_command_prelease_version_type_check_old_tags(
967-
tmp_commitizen_project, mocker: MockFixture
942+
tmp_commitizen_project_initial, mocker: MockFixture
968943
):
969-
# PRERELEASE
944+
tmp_commitizen_project = tmp_commitizen_project_initial(
945+
config_extra=('tag_format = "v$version"\nversion_type = "semver"\n'),
946+
)
970947
tmp_version_file = tmp_commitizen_project.join("__version__.py")
971-
tmp_version_file.write("0.1.0")
972948
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
973-
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
974-
tmp_commitizen_cfg_file.write(
975-
f"{tmp_commitizen_cfg_file.read()}\n"
976-
f'version_files = ["{tmp_version_file_string}"]\n'
977-
f'tag_format = "v$version"\n'
978-
f'version_type = "semver"\n'
979-
)
980-
create_file_and_commit("feat: new user interface")
981949

982950
testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
983951
mocker.patch.object(sys, "argv", testargs)
@@ -986,11 +954,9 @@ def test_bump_command_prelease_version_type_check_old_tags(
986954
tag_exists = git.tag_exist("v0.2.0-a0")
987955
assert tag_exists is True
988956

989-
with open(tmp_version_file, "r") as f:
990-
assert "0.2.0-a0" in f.read()
991-
992-
with open(tmp_commitizen_cfg_file, "r") as f:
993-
assert "0.2.0-a0" in f.read()
957+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
958+
with open(version_file, "r") as f:
959+
assert "0.2.0-a0" in f.read()
994960

995961
testargs = ["cz", "bump", "--prerelease", "alpha"]
996962
mocker.patch.object(sys, "argv", testargs)
@@ -999,11 +965,9 @@ def test_bump_command_prelease_version_type_check_old_tags(
999965
tag_exists = git.tag_exist("v0.2.0-a1")
1000966
assert tag_exists is True
1001967

1002-
with open(tmp_version_file, "r") as f:
1003-
assert "0.2.0-a1" in f.read()
1004-
1005-
with open(tmp_commitizen_cfg_file, "r") as f:
1006-
assert "0.2.0-a1" in f.read()
968+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
969+
with open(version_file, "r") as f:
970+
assert "0.2.0-a1" in f.read()
1007971

1008972
# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
1009973
testargs = ["cz", "bump"]
@@ -1013,8 +977,6 @@ def test_bump_command_prelease_version_type_check_old_tags(
1013977
tag_exists = git.tag_exist("v0.2.0")
1014978
assert tag_exists is True
1015979

1016-
with open(tmp_version_file, "r") as f:
1017-
assert "0.2.0" in f.read()
1018-
1019-
with open(tmp_commitizen_cfg_file, "r") as f:
1020-
assert "0.2.0" in f.read()
980+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
981+
with open(version_file, "r") as f:
982+
assert "0.2.0" in f.read()

tests/commands/test_changelog_command.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1208,4 +1208,4 @@ def test_changelog_prerelease_rev_with_use_version_type_semver(
12081208

12091209
out, _ = capsys.readouterr()
12101210

1211-
assert out == "## 0.3.0-a1 (2022-02-13)\n\n"
1211+
file_regression.check(out, extension=".second-prerelease.md")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## 0.3.0-a1 (2022-02-13)
2+

tests/conftest.py

+31
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import re
33
import tempfile
44
from pathlib import Path
5+
from typing import Optional
56

67
import pytest
78

89
from commitizen import cmd, defaults
910
from commitizen.config import BaseConfig
11+
from tests.utils import create_file_and_commit
1012

1113
SIGNER = "GitHub Action"
1214
SIGNER_MAIL = "[email protected]"
@@ -42,6 +44,35 @@ def tmp_commitizen_project(tmp_git_project):
4244
yield tmp_git_project
4345

4446

47+
@pytest.fixture(scope="function")
48+
def tmp_commitizen_project_initial(tmp_git_project):
49+
def _initial(
50+
config_extra: Optional[str] = None,
51+
version="0.1.0",
52+
initial_commit="feat: new user interface",
53+
):
54+
with tmp_git_project.as_cwd():
55+
tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml")
56+
tmp_commitizen_cfg_file.write(
57+
f"[tool.commitizen]\n" f'version="{version}"\n'
58+
)
59+
tmp_version_file = tmp_git_project.join("__version__.py")
60+
tmp_version_file.write(version)
61+
tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml")
62+
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
63+
tmp_commitizen_cfg_file.write(
64+
f"{tmp_commitizen_cfg_file.read()}\n"
65+
f'version_files = ["{tmp_version_file_string}"]\n'
66+
)
67+
if config_extra:
68+
tmp_commitizen_cfg_file.write(config_extra, mode="a")
69+
create_file_and_commit(initial_commit)
70+
71+
return tmp_git_project
72+
73+
yield _initial
74+
75+
4576
def _get_gpg_keyid(signer_mail):
4677
_new_key = cmd.run(f"gpg --list-secret-keys {signer_mail}")
4778
_m = re.search(

tests/test_bump_find_version_type_semver.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878

7979

8080
@pytest.mark.parametrize(
81-
"test_input,expected",
81+
"test_input, expected",
8282
itertools.chain(tdd_cases, weird_cases, simple_flow, unexpected_cases),
8383
)
8484
def test_generate_version_type(test_input, expected):

0 commit comments

Comments
 (0)