Skip to content

Commit

Permalink
Fix: When updating a version ignore the current version in case of er…
Browse files Browse the repository at this point in the history
…rors

During a version update the project's current version is parsed. This
may result in VersionErrors if for example the versioning scheme is
changed and the new versioning scheme doesn't recognize the current
version format (PEP440 based 22.4.1.dev1 can't be parsed as semver).

The current version is just parsed during the update to check if it is a
real update or if the same version is set. Therefore ignore the
VersionErrors during the version update and just set the new version.
  • Loading branch information
bjoernricks committed Jul 3, 2023
1 parent f7a75b7 commit bfa662d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 34 deletions.
14 changes: 9 additions & 5 deletions pontos/version/commands/_cargo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from pathlib import Path
from typing import Iterator, Tuple, Union, Literal
from typing import Iterator, Literal, Tuple, Union

import tomlkit

from ..errors import VersionError
from ..version import Version, VersionUpdate
from ._command import VersionCommand
from ..errors import VersionError


class CargoVersionCommand(VersionCommand):
Expand Down Expand Up @@ -40,10 +40,14 @@ def __as_project_document(
def update_version(
self, new_version: Version, *, force: bool = False
) -> VersionUpdate:
previous_version = self.get_current_version()
try:
previous_version = self.get_current_version()

if not force and new_version == previous_version:
return VersionUpdate(previous=previous_version, new=new_version)
if not force and new_version == previous_version:
return VersionUpdate(previous=previous_version, new=new_version)
except VersionError:

Check warning on line 48 in pontos/version/commands/_cargo.py

View check run for this annotation

Codecov / codecov/patch

pontos/version/commands/_cargo.py#L48

Added line #L48 was not covered by tests
# just ignore current version and override it
previous_version = None

Check warning on line 50 in pontos/version/commands/_cargo.py

View check run for this annotation

Codecov / codecov/patch

pontos/version/commands/_cargo.py#L50

Added line #L50 was not covered by tests

changed_files = []
for project_path, project in self.__as_project_document(
Expand Down
11 changes: 8 additions & 3 deletions pontos/version/commands/_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ def update_version(
) -> VersionUpdate:
content = self.project_file_path.read_text(encoding="utf-8")
cmake_version_parser = CMakeVersionParser(content)
previous_version = self.get_current_version()

if not force and new_version == previous_version:
return VersionUpdate(previous=previous_version, new=new_version)
try:
previous_version = self.get_current_version()

if not force and new_version == previous_version:
return VersionUpdate(previous=previous_version, new=new_version)
except VersionError:

Check warning on line 42 in pontos/version/commands/_cmake.py

View check run for this annotation

Codecov / codecov/patch

pontos/version/commands/_cmake.py#L42

Added line #L42 was not covered by tests
# just ignore current version and override it
previous_version = None

Check warning on line 44 in pontos/version/commands/_cmake.py

View check run for this annotation

Codecov / codecov/patch

pontos/version/commands/_cmake.py#L44

Added line #L44 was not covered by tests

new_content = cmake_version_parser.update_version(new_version)
self.project_file_path.write_text(new_content, encoding="utf-8")
Expand Down
7 changes: 3 additions & 4 deletions pontos/version/commands/_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,12 @@ def update_version(
"""Update the current version of this project"""
try:
current_version = self.get_current_version()
if not force and new_version == current_version:
return VersionUpdate(previous=current_version, new=new_version)
except VersionError:
# likely the initial release
# just ignore current version and override it
current_version = None

if not force and new_version == current_version:
return VersionUpdate(previous=current_version, new=new_version)

self._update_version_file(new_version=new_version)

return VersionUpdate(
Expand Down
12 changes: 8 additions & 4 deletions pontos/version/commands/_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
def find_file(
filename: Path, search_path: str, search_glob: str
) -> Optional[Path]:
"""Find a file somewherre within an directory tree
"""Find a file somewhere within an directory tree
Arguments:
filename (Path) The file to look up
search_path (str) The path to look for the file
Expand Down Expand Up @@ -180,9 +180,13 @@ def verify_version(
def update_version(
self, new_version: Version, *, force: bool = False
) -> VersionUpdate:
package_version = self.get_current_version()
if not force and new_version == package_version:
return VersionUpdate(previous=package_version, new=new_version)
try:
package_version = self.get_current_version()
if not force and new_version == package_version:
return VersionUpdate(previous=package_version, new=new_version)
except VersionError:

Check warning on line 187 in pontos/version/commands/_java.py

View check run for this annotation

Codecov / codecov/patch

pontos/version/commands/_java.py#L187

Added line #L187 was not covered by tests
# just ignore current version and override it
package_version = None

Check warning on line 189 in pontos/version/commands/_java.py

View check run for this annotation

Codecov / codecov/patch

pontos/version/commands/_java.py#L189

Added line #L189 was not covered by tests

changed_files = [self.project_file_path]
self._update_pom_version(new_version=new_version)
Expand Down
10 changes: 7 additions & 3 deletions pontos/version/commands/_javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,13 @@ def _update_version_file(
def update_version(
self, new_version: Version, *, force: bool = False
) -> VersionUpdate:
package_version = self.get_current_version()
if not force and new_version == package_version:
return VersionUpdate(previous=package_version, new=new_version)
try:
package_version = self.get_current_version()
if not force and new_version == package_version:
return VersionUpdate(previous=package_version, new=new_version)
except VersionError:
# just ignore current version and override it
package_version = None

changed_files = [self.project_file_path]
self._update_package_json(new_version=new_version)
Expand Down
31 changes: 18 additions & 13 deletions pontos/version/commands/_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,28 @@ def verify_version(
def update_version(
self, new_version: Version, *, force: bool = False
) -> VersionUpdate:
try:
current_version = self.get_current_version()
except VersionError:
# maybe no version module exists yet. fallback to version from
# pyproject.toml
current_version = self._get_version_from_pyproject_toml()

new_pep440_version = PEP440VersioningScheme.from_version(new_version)
current_converted_version = self.versioning_scheme.from_version(
current_version
)

if not force and new_pep440_version == current_version:
return VersionUpdate(
previous=current_converted_version, new=new_version
try:
try:
current_version = self.get_current_version()
except VersionError:
# maybe no version module exists yet. fallback to version from
# pyproject.toml
current_version = self._get_version_from_pyproject_toml()

current_converted_version = self.versioning_scheme.from_version(
current_version
)

if not force and new_pep440_version == current_version:
return VersionUpdate(
previous=current_converted_version, new=new_version
)
except VersionError:
# just ignore current version and override it
current_converted_version = None

try:
self._update_pyproject_version(new_version=new_pep440_version)
except OSError as e:
Expand Down
4 changes: 2 additions & 2 deletions tests/version/commands/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_empty_pyproject_toml(self):
"", name="pyproject.toml", change_into=True
), self.assertRaisesRegex(
VersionError,
r"Version information not found in .*pyproject\.toml file\.",
r"\[tool.pontos.version\] section missing in .*pyproject\.toml\.",
):
cmd = PythonVersionCommand(PEP440VersioningScheme)
new_version = PEP440VersioningScheme.parse_version("22.1.2")
Expand All @@ -166,7 +166,7 @@ def test_empty_tool_section(self):
"[tool]", name="pyproject.toml", change_into=True
), self.assertRaisesRegex(
VersionError,
r"Version information not found in .*pyproject\.toml file\.",
r"\[tool.pontos.version\] section missing in .*pyproject\.toml\.",
):
cmd = PythonVersionCommand(PEP440VersioningScheme)
new_version = PEP440VersioningScheme.parse_version("22.1.2")
Expand Down

0 comments on commit bfa662d

Please sign in to comment.