Skip to content

Commit 543d19b

Browse files
committed
refactor(bump): extract option validation and new version resolution to new functions
1 parent 9f3ec86 commit 543d19b

File tree

2 files changed

+82
-86
lines changed

2 files changed

+82
-86
lines changed

commitizen/commands/bump.py

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
Increment,
3131
InvalidVersion,
3232
Prerelease,
33+
VersionProtocol,
3334
get_version_scheme,
3435
)
3536

@@ -158,41 +159,91 @@ def _find_increment(self, commits: list[git.GitCommit]) -> Increment | None:
158159
)
159160
return bump.find_increment(commits, regex=bump_pattern, increments_map=bump_map)
160161

161-
def __call__(self) -> None:
162-
"""Steps executed to bump."""
163-
provider = get_provider(self.config)
164-
current_version = self.scheme(provider.get_version())
165-
166-
increment = self.arguments["increment"]
167-
prerelease = self.arguments["prerelease"]
168-
devrelease = self.arguments["devrelease"]
169-
is_local_version = self.arguments["local_version"]
170-
manual_version = self.arguments["manual_version"]
171-
build_metadata = self.arguments["build_metadata"]
172-
get_next = self.arguments["get_next"]
173-
allow_no_commit = self.arguments["allow_no_commit"]
174-
major_version_zero = self.arguments["major_version_zero"]
175-
176-
if manual_version:
162+
def _validate_arguments(self, current_version: VersionProtocol) -> None:
163+
errors: list[str] = []
164+
if self.arguments["manual_version"]:
177165
for val, option in (
178-
(increment, "--increment"),
179-
(prerelease, "--prerelease"),
180-
(devrelease is not None, "--devrelease"),
181-
(is_local_version, "--local-version"),
182-
(build_metadata, "--build-metadata"),
183-
(major_version_zero, "--major-version-zero"),
184-
(get_next, "--get-next"),
166+
(self.arguments["increment"], "--increment"),
167+
(self.arguments["prerelease"], "--prerelease"),
168+
(self.arguments["devrelease"] is not None, "--devrelease"),
169+
(self.arguments["local_version"], "--local-version"),
170+
(self.arguments["build_metadata"], "--build-metadata"),
171+
(self.arguments["major_version_zero"], "--major-version-zero"),
185172
):
186173
if val:
187-
raise NotAllowed(f"{option} cannot be combined with MANUAL_VERSION")
174+
errors.append(f"{option} cannot be combined with MANUAL_VERSION")
188175

189-
if major_version_zero and current_version.release[0]:
190-
raise NotAllowed(
176+
if self.arguments["major_version_zero"] and current_version.release[0]:
177+
errors.append(
191178
f"--major-version-zero is meaningless for current version {current_version}"
192179
)
180+
if self.arguments["build_metadata"] and self.arguments["local_version"]:
181+
errors.append("--local-version cannot be combined with --build-metadata")
182+
183+
if errors:
184+
raise NotAllowed("\n".join(errors))
185+
186+
def _resolve_increment_and_new_version(
187+
self, current_version: VersionProtocol, current_tag: git.GitTag | None
188+
) -> tuple[Increment | None, VersionProtocol]:
189+
increment = self.arguments["increment"]
190+
if manual_version := self.arguments["manual_version"]:
191+
try:
192+
return increment, self.scheme(manual_version)
193+
except InvalidVersion as exc:
194+
raise InvalidManualVersion(
195+
"[INVALID_MANUAL_VERSION]\n"
196+
f"Invalid manual version: '{manual_version}'"
197+
) from exc
198+
199+
if increment is None:
200+
commits = git.get_commits(current_tag.name if current_tag else None)
201+
202+
# No commits, there is no need to create an empty tag.
203+
# Unless we previously had a prerelease.
204+
if (
205+
not commits
206+
and not current_version.is_prerelease
207+
and not self.arguments["allow_no_commit"]
208+
):
209+
raise NoCommitsFoundError("[NO_COMMITS_FOUND]\nNo new commits found.")
210+
211+
increment = self._find_increment(commits)
212+
213+
# It may happen that there are commits, but they are not eligible
214+
# for an increment, this generates a problem when using prerelease (#281)
215+
if (
216+
self.arguments["prerelease"]
217+
and increment is None
218+
and not current_version.is_prerelease
219+
):
220+
raise NoCommitsFoundError(
221+
"[NO_COMMITS_FOUND]\n"
222+
"No commits found to generate a pre-release.\n"
223+
"To avoid this error, manually specify the type of increment with `--increment`"
224+
)
193225

194-
if build_metadata and is_local_version:
195-
raise NotAllowed("--local-version cannot be combined with --build-metadata")
226+
# we create an empty PATCH increment for empty tag
227+
if increment is None and self.arguments["allow_no_commit"]:
228+
increment = "PATCH"
229+
230+
return increment, current_version.bump(
231+
increment,
232+
prerelease=self.arguments["prerelease"],
233+
prerelease_offset=self.bump_settings["prerelease_offset"],
234+
devrelease=self.arguments["devrelease"],
235+
is_local_version=self.arguments["local_version"],
236+
build_metadata=self.arguments["build_metadata"],
237+
exact_increment=self.arguments["increment_mode"] == "exact",
238+
)
239+
240+
def __call__(self) -> None:
241+
"""Steps executed to bump."""
242+
provider = get_provider(self.config)
243+
current_version = self.scheme(provider.get_version())
244+
self._validate_arguments(current_version)
245+
246+
get_next = self.arguments["get_next"]
196247

197248
if get_next:
198249
for value, option in (
@@ -221,53 +272,9 @@ def __call__(self) -> None:
221272

222273
is_initial = self._is_initial_tag(current_tag, self.arguments["yes"])
223274

224-
if manual_version:
225-
try:
226-
new_version = self.scheme(manual_version)
227-
except InvalidVersion as exc:
228-
raise InvalidManualVersion(
229-
"[INVALID_MANUAL_VERSION]\n"
230-
f"Invalid manual version: '{manual_version}'"
231-
) from exc
232-
else:
233-
if increment is None:
234-
commits = git.get_commits(current_tag.name if current_tag else None)
235-
236-
# No commits, there is no need to create an empty tag.
237-
# Unless we previously had a prerelease.
238-
if (
239-
not commits
240-
and not current_version.is_prerelease
241-
and not allow_no_commit
242-
):
243-
raise NoCommitsFoundError(
244-
"[NO_COMMITS_FOUND]\nNo new commits found."
245-
)
246-
247-
increment = self._find_increment(commits)
248-
249-
# It may happen that there are commits, but they are not eligible
250-
# for an increment, this generates a problem when using prerelease (#281)
251-
if prerelease and increment is None and not current_version.is_prerelease:
252-
raise NoCommitsFoundError(
253-
"[NO_COMMITS_FOUND]\n"
254-
"No commits found to generate a pre-release.\n"
255-
"To avoid this error, manually specify the type of increment with `--increment`"
256-
)
257-
258-
# we create an empty PATCH increment for empty tag
259-
if increment is None and allow_no_commit:
260-
increment = "PATCH"
261-
262-
new_version = current_version.bump(
263-
increment,
264-
prerelease=prerelease,
265-
prerelease_offset=self.bump_settings["prerelease_offset"],
266-
devrelease=devrelease,
267-
is_local_version=is_local_version,
268-
build_metadata=build_metadata,
269-
exact_increment=self.arguments["increment_mode"] == "exact",
270-
)
275+
increment, new_version = self._resolve_increment_and_new_version(
276+
current_version, current_tag
277+
)
271278

272279
new_tag_version = rules.normalize_tag(new_version)
273280
message = bump.create_commit_message(

tests/commands/test_bump_command.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,17 +1508,6 @@ def test_bump_get_next__changelog_to_stdout_is_not_allowed(mocker: MockFixture):
15081508
cli.main()
15091509

15101510

1511-
@pytest.mark.usefixtures("tmp_commitizen_project")
1512-
def test_bump_get_next__manual_version_is_not_allowed(mocker: MockFixture):
1513-
create_file_and_commit("feat: new file")
1514-
1515-
testargs = ["cz", "bump", "--yes", "--get-next", "0.2.1"]
1516-
mocker.patch.object(sys, "argv", testargs)
1517-
1518-
with pytest.raises(NotAllowed):
1519-
cli.main()
1520-
1521-
15221511
@pytest.mark.usefixtures("tmp_commitizen_project")
15231512
def test_bump_get_next__no_eligible_commits_raises(mocker: MockFixture):
15241513
create_file_and_commit("chore: new commit")

0 commit comments

Comments
 (0)