Skip to content

Commit

Permalink
MAINT: Remove returning exceptions
Browse files Browse the repository at this point in the history
Return only bools instead of returning exceptions. Exceptions should be raised and bools can be returned then checked.
  • Loading branch information
khalford committed Jan 29, 2025
1 parent 00ee3eb commit 294f2f1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
8 changes: 3 additions & 5 deletions src/features/app_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def run(self, path1: Path, path2: Path) -> bool:
main_ver = self.get_version(main_content)
branch_ver = self.get_version(branch_content)
comparison = self.compare(main_ver, branch_ver)
if comparison == RuntimeError:
if not comparison:
raise RuntimeError(
f"The version in {('/'.join(str(path2).split('/')[4:]))[0:]} has not been updated correctly."
)
Expand Down Expand Up @@ -51,13 +51,11 @@ def get_version(content: str) -> Version:
return Version(content)

@staticmethod
def compare(main: Version, branch: Version) -> Union[bool, Type[RuntimeError]]:
def compare(main: Version, branch: Version) -> bool:
"""
Returns if the branch version is larger than the main version
:param main: Version on main
:param branch: Version on branch
:return: If the version update is correct return true, else return error
"""
if branch > main:
return True
return RuntimeError
return branch > main
10 changes: 4 additions & 6 deletions src/features/compose_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Compare Docker compose image version to the version.txt."""

from pathlib import Path
from typing import List, Union, Type
from typing import List

from packaging.version import Version

Expand All @@ -20,7 +20,7 @@ def run(self, app: Path, compose: Path) -> bool:
app_ver = Version(app_content)
compose_ver = self.get_version(compose_content)
comparison = self.compare(app_ver, compose_ver)
if comparison == RuntimeError:
if not comparison:
raise RuntimeError(
f"The version in {('/'.join(str(compose).split('/')[4:]))[0:]}"
f" does not match {('/'.join(str(app).split('/')[4:]))[0:]}."
Expand Down Expand Up @@ -57,13 +57,11 @@ def get_version(content: List[str]) -> Version:
return Version(version_str)

@staticmethod
def compare(app: Version, compose: Version) -> Union[bool, Type[RuntimeError]]:
def compare(app: Version, compose: Version) -> bool:
"""
Returns if the app version and docker compose version are equal.
:param app: App version
:param compose: Compose version
:return: If the version update is correct return true, else return error
"""
if app == compose:
return True
return RuntimeError
return app == compose
4 changes: 2 additions & 2 deletions tests/test_app_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def test_get_version(instance):
def test_compare_pass(instance):
"""Test that the compare returns true for a valid features.app_version"""
res = instance.compare(Version("1.0.0"), Version("1.0.1"))
assert res != RuntimeError
assert res


def test_compare_fails(instance):
"""Test that the compare returns an error for an invalid features.app_version"""
res = instance.compare(Version("1.0.1"), Version("1.0.0"))
assert res == RuntimeError
assert not res
4 changes: 2 additions & 2 deletions tests/test_compose_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def test_get_version(instance):
def test_compare_pass(instance):
"""Test that the compare returns true for a valid features.compose_version"""
res = instance.compare(Version("1.0.0"), Version("1.0.0"))
assert res != RuntimeError
assert res


def test_compare_fails(instance):
"""Test that the compare returns an error for an invalid features.compose_version"""
res = instance.compare(Version("1.0.1"), Version("1.0.0"))
assert res == RuntimeError
assert not res

0 comments on commit 294f2f1

Please sign in to comment.