Skip to content

Commit 593a4ee

Browse files
committed
Fixed some tests
1 parent 0ac2cd8 commit 593a4ee

File tree

4 files changed

+137
-285
lines changed

4 files changed

+137
-285
lines changed

bumpversion/versioning/models.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class VersionComponentConfig(BaseModel):
108108
# source: Optional[str] = None # Name of environment variable or context variable to use as the source for value
109109
depends_on: Optional[str] = None # The name of the component this component depends on
110110

111-
def generate_component(self, value: Union[str, int, None] = None) -> VersionComponent:
111+
def create_component(self, value: Union[str, int, None] = None) -> VersionComponent:
112112
"""Generate a version component from the configuration."""
113113
return VersionComponent(
114114
values=self.values,
@@ -147,7 +147,7 @@ def __init__(self, components: Dict[str, VersionComponentConfig], order: Optiona
147147
def create_version(self, values: Dict[str, str]) -> "Version":
148148
"""Generate a version from the given values."""
149149
components = {
150-
key: comp_config.generate_component(value=values.get(key))
150+
key: comp_config.create_component(value=values.get(key))
151151
for key, comp_config in self.component_configs.items()
152152
}
153153
return Version(version_spec=self, components=components)

tests/test_version_part.py

-213
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import logging
21
from pathlib import Path
32

43
import pytest
54
from click import UsageError
6-
from pytest import LogCaptureFixture, param
75

86
from bumpversion import exceptions
97
from bumpversion.utils import get_context
@@ -76,153 +74,6 @@ def test_serialize_with_environment_var():
7674
del os.environ["BUILD_NUMBER"]
7775

7876

79-
def test_serialize_with_newlines():
80-
overrides = {
81-
"current_version": "MAJOR=31\nMINOR=0\nPATCH=3\n",
82-
"parse": r"MAJOR=(?P<major>\d+)\nMINOR=(?P<minor>\d+)\nPATCH=(?P<patch>\d+)\n",
83-
"serialize": ["MAJOR={major}\nMINOR={minor}\nPATCH={patch}\n"],
84-
}
85-
conf, version_config, current_version = get_config_data(overrides)
86-
new_version = current_version.bump("major")
87-
assert version_config.serialize(new_version, get_context(conf)) == "MAJOR=32\nMINOR=0\nPATCH=0\n"
88-
89-
90-
@pytest.mark.parametrize(
91-
["bump_type", "expected"],
92-
[
93-
param("patch", "0.9.1", id="patch"),
94-
param("minor", "0.10", id="minor"),
95-
param("major", "1", id="major"),
96-
],
97-
)
98-
def test_serialize_three_part(bump_type: str, expected: str):
99-
overrides = {
100-
"current_version": "0.9",
101-
"parse": r"(?P<major>\d+)(\.(?P<minor>\d+)(\.(?P<patch>\d+))?)?",
102-
"serialize": ["{major}.{minor}.{patch}", "{major}.{minor}", "{major}"],
103-
}
104-
conf, version_config, current_version = get_config_data(overrides)
105-
106-
new_version = current_version.bump(bump_type)
107-
assert version_config.serialize(new_version, get_context(conf)) == expected
108-
109-
110-
@pytest.mark.parametrize(
111-
["initial", "bump_type", "expected"],
112-
[
113-
param("1.5.dev", "release", "1.5", id="release"),
114-
param("1.5", "minor", "1.6.dev", id="minor"),
115-
],
116-
)
117-
def test_bump_non_numeric_parts(initial: str, bump_type: str, expected: str):
118-
overrides = {
119-
"current_version": initial,
120-
"parse": r"(?P<major>\d+)\.(?P<minor>\d+)(\.(?P<release>[a-z]+))?",
121-
"serialize": ["{major}.{minor}.{release}", "{major}.{minor}"],
122-
"parts": {
123-
"release": {
124-
"optional_value": "gamma",
125-
"values": ["dev", "gamma"],
126-
},
127-
},
128-
}
129-
conf, version_config, current_version = get_config_data(overrides)
130-
131-
new_version = current_version.bump(bump_type)
132-
assert version_config.serialize(new_version, get_context(conf)) == expected
133-
134-
135-
@pytest.mark.parametrize(
136-
["initial", "bump_type", "expected"],
137-
[
138-
param("1.alpha", "release", "1.beta", id="alpha-to-beta-release"),
139-
param("1.beta", "release", "1", id="beta-to-release"),
140-
],
141-
)
142-
def test_optional_value(initial: str, bump_type: str, expected: str):
143-
overrides = {
144-
"current_version": initial,
145-
"parse": r"(?P<num>\d+)(\.(?P<release>.*))?(\.)?",
146-
"serialize": ["{num}.{release}", "{num}"],
147-
"parts": {
148-
"release": {
149-
"optional_value": "gamma",
150-
"values": ["alpha", "beta", "gamma"],
151-
},
152-
},
153-
}
154-
conf, version_config, current_version = get_config_data(overrides)
155-
156-
new_version = current_version.bump(bump_type)
157-
assert version_config.serialize(new_version, get_context(conf)) == expected
158-
159-
160-
@pytest.mark.parametrize(
161-
["initial", "bump_type", "expected"],
162-
[
163-
param("1.0a", "prerel", "1.0b", id="a-to-b-release"),
164-
param("1.0b", "prerelversion", "1.0b1", id="prerelease-version"),
165-
param("1.0b1", "prerelversion", "1.0b2", id="prerelease-version2"),
166-
param("1.0b2", "prerel", "1.0c", id="b2-to-c-release"),
167-
param("1.0c", "prerel", "1.0rc", id="c-to-rc-release"),
168-
param("1.0rc", "prerel", "1.0", id="rc-to-d-release"),
169-
param("1.0", "minor", "1.1dev", id="minor-release"),
170-
param("1.1dev", "prerel", "1.1a", id="dev-to-a-release"),
171-
],
172-
)
173-
def test_python_pre_release_release_post_release(initial: str, bump_type: str, expected: str):
174-
# adapted from http://legacy.python.org/dev/peps/pep-0386/#the-new-versioning-algorithm
175-
overrides = {
176-
"current_version": initial,
177-
"parse": r"""^
178-
(?P<major>\d+)\.(?P<minor>\d+) # minimum 'N.N'
179-
(?:
180-
(?P<prerel>[abc]|rc|dev) # 'a' = alpha, 'b' = beta
181-
# 'c' or 'rc' = release candidate
182-
(?:
183-
(?P<prerelversion>\d+(?:\.\d+)*)
184-
)?
185-
)?
186-
(?P<postdev>(\.post(?P<post>\d+))?(\.dev(?P<dev>\d+))?)?""",
187-
"serialize": [
188-
"{major}.{minor}{prerel}{prerelversion}",
189-
"{major}.{minor}{prerel}",
190-
"{major}.{minor}",
191-
],
192-
"parts": {
193-
"prerel": {
194-
"optional_value": "d",
195-
"values": ["dev", "a", "b", "c", "rc", "d"],
196-
},
197-
},
198-
}
199-
conf, version_config, current_version = get_config_data(overrides)
200-
201-
new_version = current_version.bump(bump_type)
202-
assert version_config.serialize(new_version, get_context(conf)) == expected
203-
204-
205-
@pytest.mark.parametrize(
206-
["initial", "bump_type", "expected"],
207-
[
208-
param("0.9.4", "major", "1.1.0", id="first-value-1"),
209-
],
210-
)
211-
def test_part_first_value(initial: str, bump_type: str, expected: str):
212-
overrides = {
213-
"current_version": initial,
214-
"parts": {
215-
"minor": {
216-
"first_value": "1",
217-
},
218-
},
219-
}
220-
conf, version_config, current_version = get_config_data(overrides)
221-
222-
new_version = current_version.bump(bump_type)
223-
assert version_config.serialize(new_version, get_context(conf)) == expected
224-
225-
22677
def test_version_part_invalid_regex_exit(tmp_path: Path) -> None:
22778
"""A version part with an invalid regex should raise an exception."""
22879
# Arrange
@@ -233,67 +84,3 @@ def test_version_part_invalid_regex_exit(tmp_path: Path) -> None:
23384
with inside_dir(tmp_path):
23485
with pytest.raises(UsageError):
23586
get_config_data(overrides)
236-
237-
238-
def test_part_does_not_revert_to_zero_if_optional(tmp_path: Path) -> None:
239-
"""A non-numeric part with the optional value should not revert to zero."""
240-
# From https://github.com/c4urself/bump2version/issues/248
241-
# Arrange
242-
overrides = {
243-
"current_version": "0.3.1",
244-
"parse": r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)((?P<release>\D+)(?P<build>\d*))?",
245-
"serialize": [
246-
"{major}.{minor}.{patch}{release}{build}",
247-
"{major}.{minor}.{patch}{release}",
248-
"{major}.{minor}.{patch}",
249-
],
250-
"parts": {
251-
"release": {
252-
"optional_value": "g",
253-
"first_value": "g",
254-
"values": [
255-
"dev",
256-
"a",
257-
"b",
258-
"g",
259-
],
260-
},
261-
},
262-
}
263-
with inside_dir(tmp_path):
264-
conf, version_config, current_version = get_config_data(overrides)
265-
266-
new_version = current_version.bump("build")
267-
assert version_config.serialize(new_version, get_context(conf)) == "0.3.1g1"
268-
269-
270-
def test_order_of_serialization(tmp_path: Path, caplog: LogCaptureFixture) -> None:
271-
"""The order of serialization should be as specified in the config."""
272-
caplog.set_level(logging.DEBUG)
273-
overrides = {
274-
"current_version": "3.16.dev1",
275-
"parse": r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<release>[a-z]+)?(?P<patch>\d+)?",
276-
"serialize": [
277-
"{major}.{minor}.{release}{patch}",
278-
"{major}.{minor}.{release}",
279-
"{major}.{minor}.{patch}",
280-
],
281-
"parts": {
282-
"release": {
283-
"optional_value": "prod",
284-
"first_value": "dev",
285-
"values": [
286-
"dev",
287-
"prod",
288-
],
289-
},
290-
},
291-
}
292-
with inside_dir(tmp_path):
293-
conf, version_config, current_version = get_config_data(overrides)
294-
295-
new_version = current_version.bump("release")
296-
new_version_str = version_config.serialize(new_version, get_context(conf))
297-
for msg in caplog.messages:
298-
print(msg)
299-
assert new_version_str == "3.16.prod"

tests/test_versioning/test_models_versioncomponent.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,59 @@
77

88
@pytest.fixture(
99
params=[
10-
None,
11-
("0", "1", "2"),
12-
("0", "3"),
10+
{"optional_value": "0", "first_value": "0"},
11+
{"first_value": "1"},
12+
{"values": ["alpha", "beta", "gamma"]},
13+
{"values": ["alpha", "gamma"]},
1314
]
1415
)
1516
def version_component_config(request):
1617
"""Return a three-part and a two-part version part configuration."""
17-
if request.param is None:
18-
return VersionComponentConfig(optional_value="0", first_value="0")
19-
else:
20-
return VersionComponentConfig(values=request.param)
18+
return VersionComponentConfig(**request.param)
2119

2220

2321
class TestVersionComponent:
2422
class TestCreation:
2523
def test_none_value_uses_optional_value(self, version_component_config):
26-
vp = version_component_config.generate_component()
24+
vp = version_component_config.create_component()
2725
assert vp.value == vp.func.optional_value
2826
assert vp._value is None
2927

3028
def test_config_with_values_selects_values_function(self):
3129
values = ["0", "1", "2"]
32-
vp = VersionComponentConfig(values=values).generate_component()
30+
vp = VersionComponentConfig(values=values).create_component()
3331
assert isinstance(vp.func, ValuesFunction)
3432

3533
def test_config_without_values_selects_numeric_function(self):
36-
vp = VersionComponentConfig().generate_component()
34+
vp = VersionComponentConfig().create_component()
3735
assert isinstance(vp.func, NumericFunction)
3836

3937
def test_copy_returns_new_version_part(self, version_component_config):
40-
vp = version_component_config.generate_component(version_component_config.first_value)
38+
vp = version_component_config.create_component(version_component_config.first_value)
4139
vc = vp.copy()
4240
assert vp.value == vc.value
4341
assert id(vp) != id(vc)
4442

4543
def test_bump_increments_value(self, version_component_config):
46-
vp = version_component_config.generate_component(version_component_config.first_value)
44+
vp = version_component_config.create_component(version_component_config.first_value)
4745
vc = vp.bump()
4846
if version_component_config.values:
4947
assert vc.value == str(version_component_config.values[1])
5048
else:
51-
assert vc.value == "1"
49+
assert vc.value == str(int(version_component_config.first_value) + 1)
5250

5351
def test_non_first_value_is_not_optional(self, version_component_config):
54-
assert not version_component_config.generate_component(version_component_config.first_value).bump().is_optional
52+
assert not version_component_config.create_component(version_component_config.first_value).bump().is_optional
5553

5654
def test_first_value_is_optional(self, version_component_config):
57-
assert version_component_config.generate_component(version_component_config.first_value).is_optional
55+
assert version_component_config.create_component(version_component_config.first_value).is_optional
5856

5957
def test_versionparts_with_same_settings_are_equal(self, version_component_config):
60-
version1 = version_component_config.generate_component(version_component_config.first_value)
61-
version2 = version_component_config.generate_component(version_component_config.first_value)
58+
version1 = version_component_config.create_component(version_component_config.first_value)
59+
version2 = version_component_config.create_component(version_component_config.first_value)
6260
assert version1 == version2
6361

6462
def test_null_resets_value_to_first_value(self, version_component_config):
65-
version1 = version_component_config.generate_component(version_component_config.first_value)
63+
version1 = version_component_config.create_component(version_component_config.first_value)
6664
version2 = version1.bump().null()
6765
assert version2 == version1

0 commit comments

Comments
 (0)