Skip to content

Commit ea7ac93

Browse files
committed
refactor: minor review fixes
1 parent 5759f4e commit ea7ac93

10 files changed

+84
-89
lines changed

commitizen/cli.py

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

commitizen/commands/bump.py

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

6969
def is_initial_tag(self, current_tag_version: str, is_yes: bool = False) -> bool:
7070
"""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
@@ -51,7 +51,7 @@ def __init__(self, config: BaseConfig, args):
5151
)
5252

5353
version_type = self.config.settings.get("version_type")
54-
self.version_type = version_type and version_types.types[version_type]
54+
self.version_type = version_type and version_types.VERSION_TYPES[version_type]
5555

5656
def _find_incremental_rev(self, latest_version: str, tags: List[GitTag]) -> str:
5757
"""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
@@ -21,7 +21,7 @@
2121
| `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] |
2222
| `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] |
2323
| `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] |
24-
| `version_type` | `str` | `pep440` | Select a version type from the following options [`pep440`, `semver`]. Useful for non python projects. [See more][version_type] |
24+
| `version_type` | `str` | `pep440` | Select a version type from the following options [`pep440`, `semver`]. Useful for non-python projects. [See more][version_type] |
2525

2626
## pyproject.toml or .cz.toml
2727

tests/commands/test_bump_command.py

+34-72
Original file line numberDiff line numberDiff line change
@@ -834,19 +834,11 @@ def test_bump_manual_version_disallows_prerelease_offset(mocker):
834834

835835

836836
def test_bump_command_prelease_version_type_via_cli(
837-
tmp_commitizen_project, mocker: MockFixture
837+
tmp_commitizen_project_initial, mocker: MockFixture
838838
):
839-
# PRERELEASE
839+
tmp_commitizen_project = tmp_commitizen_project_initial()
840840
tmp_version_file = tmp_commitizen_project.join("__version__.py")
841-
tmp_version_file.write("0.1.0")
842841
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
843-
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
844-
tmp_commitizen_cfg_file.write(
845-
f"{tmp_commitizen_cfg_file.read()}\n"
846-
f'version_files = ["{tmp_version_file_string}"]'
847-
)
848-
849-
create_file_and_commit("feat: new user interface")
850842

851843
testargs = [
852844
"cz",
@@ -863,11 +855,9 @@ def test_bump_command_prelease_version_type_via_cli(
863855
tag_exists = git.tag_exist("0.2.0-a0")
864856
assert tag_exists is True
865857

866-
with open(tmp_version_file, "r") as f:
867-
assert "0.2.0-a0" in f.read()
868-
869-
with open(tmp_commitizen_cfg_file, "r") as f:
870-
assert "0.2.0-a0" in f.read()
858+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
859+
with open(version_file, "r") as f:
860+
assert "0.2.0-a0" in f.read()
871861

872862
# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
873863
testargs = ["cz", "bump", "--yes"]
@@ -877,28 +867,19 @@ def test_bump_command_prelease_version_type_via_cli(
877867
tag_exists = git.tag_exist("0.2.0")
878868
assert tag_exists is True
879869

880-
with open(tmp_version_file, "r") as f:
881-
assert "0.2.0" in f.read()
882-
883-
with open(tmp_commitizen_cfg_file, "r") as f:
884-
assert "0.2.0" in f.read()
870+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
871+
with open(version_file, "r") as f:
872+
assert "0.2.0" in f.read()
885873

886874

887875
def test_bump_command_prelease_version_type_via_config(
888-
tmp_commitizen_project, mocker: MockFixture
876+
tmp_commitizen_project_initial, mocker: MockFixture
889877
):
890-
# PRERELEASE
878+
tmp_commitizen_project = tmp_commitizen_project_initial(
879+
config_extra='version_type = "semver"\n',
880+
)
891881
tmp_version_file = tmp_commitizen_project.join("__version__.py")
892-
tmp_version_file.write("0.1.0")
893882
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
894-
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
895-
tmp_commitizen_cfg_file.write(
896-
f"{tmp_commitizen_cfg_file.read()}\n"
897-
f'version_files = ["{tmp_version_file_string}"]\n'
898-
f'version_type = "semver"'
899-
)
900-
901-
create_file_and_commit("feat: new user interface")
902883

903884
testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
904885
mocker.patch.object(sys, "argv", testargs)
@@ -907,11 +888,9 @@ def test_bump_command_prelease_version_type_via_config(
907888
tag_exists = git.tag_exist("0.2.0-a0")
908889
assert tag_exists is True
909890

910-
with open(tmp_version_file, "r") as f:
911-
assert "0.2.0-a0" in f.read()
912-
913-
with open(tmp_commitizen_cfg_file, "r") as f:
914-
assert "0.2.0-a0" in f.read()
891+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
892+
with open(version_file, "r") as f:
893+
assert "0.2.0-a0" in f.read()
915894

916895
testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
917896
mocker.patch.object(sys, "argv", testargs)
@@ -920,11 +899,9 @@ def test_bump_command_prelease_version_type_via_config(
920899
tag_exists = git.tag_exist("0.2.0-a1")
921900
assert tag_exists is True
922901

923-
with open(tmp_version_file, "r") as f:
924-
assert "0.2.0-a1" in f.read()
925-
926-
with open(tmp_commitizen_cfg_file, "r") as f:
927-
assert "0.2.0-a1" in f.read()
902+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
903+
with open(version_file, "r") as f:
904+
assert "0.2.0-a1" in f.read()
928905

929906
# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
930907
testargs = ["cz", "bump", "--yes"]
@@ -934,28 +911,19 @@ def test_bump_command_prelease_version_type_via_config(
934911
tag_exists = git.tag_exist("0.2.0")
935912
assert tag_exists is True
936913

937-
with open(tmp_version_file, "r") as f:
938-
assert "0.2.0" in f.read()
939-
940-
with open(tmp_commitizen_cfg_file, "r") as f:
941-
assert "0.2.0" in f.read()
914+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
915+
with open(version_file, "r") as f:
916+
assert "0.2.0" in f.read()
942917

943918

944919
def test_bump_command_prelease_version_type_check_old_tags(
945-
tmp_commitizen_project, mocker: MockFixture
920+
tmp_commitizen_project_initial, mocker: MockFixture
946921
):
947-
# PRERELEASE
922+
tmp_commitizen_project = tmp_commitizen_project_initial(
923+
config_extra=('tag_format = "v$version"\nversion_type = "semver"\n'),
924+
)
948925
tmp_version_file = tmp_commitizen_project.join("__version__.py")
949-
tmp_version_file.write("0.1.0")
950926
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
951-
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
952-
tmp_commitizen_cfg_file.write(
953-
f"{tmp_commitizen_cfg_file.read()}\n"
954-
f'version_files = ["{tmp_version_file_string}"]\n'
955-
f'tag_format = "v$version"\n'
956-
f'version_type = "semver"\n'
957-
)
958-
create_file_and_commit("feat: new user interface")
959927

960928
testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
961929
mocker.patch.object(sys, "argv", testargs)
@@ -964,11 +932,9 @@ def test_bump_command_prelease_version_type_check_old_tags(
964932
tag_exists = git.tag_exist("v0.2.0-a0")
965933
assert tag_exists is True
966934

967-
with open(tmp_version_file, "r") as f:
968-
assert "0.2.0-a0" in f.read()
969-
970-
with open(tmp_commitizen_cfg_file, "r") as f:
971-
assert "0.2.0-a0" in f.read()
935+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
936+
with open(version_file, "r") as f:
937+
assert "0.2.0-a0" in f.read()
972938

973939
testargs = ["cz", "bump", "--prerelease", "alpha"]
974940
mocker.patch.object(sys, "argv", testargs)
@@ -977,11 +943,9 @@ def test_bump_command_prelease_version_type_check_old_tags(
977943
tag_exists = git.tag_exist("v0.2.0-a1")
978944
assert tag_exists is True
979945

980-
with open(tmp_version_file, "r") as f:
981-
assert "0.2.0-a1" in f.read()
982-
983-
with open(tmp_commitizen_cfg_file, "r") as f:
984-
assert "0.2.0-a1" in f.read()
946+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
947+
with open(version_file, "r") as f:
948+
assert "0.2.0-a1" in f.read()
985949

986950
# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
987951
testargs = ["cz", "bump"]
@@ -991,8 +955,6 @@ def test_bump_command_prelease_version_type_check_old_tags(
991955
tag_exists = git.tag_exist("v0.2.0")
992956
assert tag_exists is True
993957

994-
with open(tmp_version_file, "r") as f:
995-
assert "0.2.0" in f.read()
996-
997-
with open(tmp_commitizen_cfg_file, "r") as f:
998-
assert "0.2.0" in f.read()
958+
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
959+
with open(version_file, "r") as f:
960+
assert "0.2.0" in f.read()

tests/commands/test_changelog_command.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1024,4 +1024,4 @@ def test_changelog_prerelease_rev_with_use_version_type_semver(
10241024

10251025
out, _ = capsys.readouterr()
10261026

1027-
assert out == "## 0.3.0-a1 (2022-02-13)\n\n"
1027+
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
@@ -1,11 +1,13 @@
11
import os
22
import re
33
import tempfile
4+
from typing import Optional
45

56
import pytest
67

78
from commitizen import cmd, defaults
89
from commitizen.config import BaseConfig
10+
from tests.utils import create_file_and_commit
911

1012

1113
@pytest.fixture(scope="function")
@@ -25,6 +27,35 @@ def tmp_commitizen_project(tmp_git_project):
2527
yield tmp_git_project
2628

2729

30+
@pytest.fixture(scope="function")
31+
def tmp_commitizen_project_initial(tmp_git_project):
32+
def _initial(
33+
config_extra: Optional[str] = None,
34+
version="0.1.0",
35+
initial_commit="feat: new user interface",
36+
):
37+
with tmp_git_project.as_cwd():
38+
tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml")
39+
tmp_commitizen_cfg_file.write(
40+
f"[tool.commitizen]\n" f'version="{version}"\n'
41+
)
42+
tmp_version_file = tmp_git_project.join("__version__.py")
43+
tmp_version_file.write(version)
44+
tmp_commitizen_cfg_file = tmp_git_project.join("pyproject.toml")
45+
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
46+
tmp_commitizen_cfg_file.write(
47+
f"{tmp_commitizen_cfg_file.read()}\n"
48+
f'version_files = ["{tmp_version_file_string}"]\n'
49+
)
50+
if config_extra:
51+
tmp_commitizen_cfg_file.write(config_extra, mode="a")
52+
create_file_and_commit(initial_commit)
53+
54+
return tmp_git_project
55+
56+
yield _initial
57+
58+
2859
def _get_gpg_keyid(signer_mail):
2960
_new_key = cmd.run(f"gpg --list-secret-keys {signer_mail}")
3061
_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)