Skip to content

Commit

Permalink
Fix version compare (#2334)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Dec 11, 2021
1 parent 30eff09 commit 76a7e9b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
5 changes: 0 additions & 5 deletions custom_components/hacs/helpers/functions/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ def get_repository_name(repository) -> str:
return repository.data.full_name.split("/")[-1].replace("-", " ").replace("_", " ").title()


def version_left_higher_then_right(left: str, right: str) -> bool:
"""Return a bool if source is newer than target, will also be true if identical."""
return version.version_left_higher_then_right(left, right)


def extract_repository_from_url(url: str) -> str or None:
"""Extract the owner/repo part form a URL."""
match = re.match(RE_REPOSITORY, url)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/hacs/helpers/properties/can_be_installed.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# pylint: disable=missing-class-docstring,missing-module-docstring,missing-function-docstring,no-member
from abc import ABC

from custom_components.hacs.helpers.functions.misc import version_left_higher_then_right
from custom_components.hacs.utils.version import version_left_higher_or_equal_then_right


class RepositoryPropertyCanBeInstalled(ABC):
@property
def can_be_installed(self) -> bool:
if self.data.homeassistant is not None:
if self.data.releases:
if not version_left_higher_then_right(
if not version_left_higher_or_equal_then_right(
self.hacs.core.ha_version, self.data.homeassistant
):
return False
Expand Down
6 changes: 4 additions & 2 deletions custom_components/hacs/tasks/check_constrains.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..base import HacsBase
from ..const import MINIMUM_HA_VERSION
from ..enums import HacsDisabledReason, HacsStage
from ..utils.version import version_left_higher_then_right
from ..utils.version import version_left_higher_or_equal_then_right
from .base import HacsTask


Expand Down Expand Up @@ -37,7 +37,9 @@ def execute(self) -> None:

self.hacs.disable_hacs(HacsDisabledReason.CONSTRAINS)

if not version_left_higher_then_right(self.hacs.core.ha_version, MINIMUM_HA_VERSION):
if not version_left_higher_or_equal_then_right(
self.hacs.core.ha_version, MINIMUM_HA_VERSION
):
self.task_logger(
self.log.critical,
f"You need HA version {MINIMUM_HA_VERSION} or newer to use this integration.",
Expand Down
12 changes: 11 additions & 1 deletion custom_components/hacs/utils/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
def version_left_higher_then_right(left: str, right: str) -> bool:
"""Return a bool if source is newer than target, will also be true if identical."""
try:
return AwesomeVersion(left) >= AwesomeVersion(right)
return AwesomeVersion(left) > AwesomeVersion(right)
except (AwesomeVersionException, AttributeError):
return False


def version_left_higher_or_equal_then_right(left: str, right: str) -> bool:
"""Return a bool if source is newer than target, will also be true if identical."""
if left == right:
return True
try:
return version_left_higher_then_right(left, right)
except (AwesomeVersionException, AttributeError):
return False
36 changes: 18 additions & 18 deletions tests/helpers/misc/test_version_is_newer_than_version.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
"""Helpers: Misc: version_left_higher_then_right."""
"""Helpers: Misc: version_left_higher_or_equal_then_right."""
# pylint: disable=missing-docstring
from custom_components.hacs.helpers.functions.misc import version_left_higher_then_right
from custom_components.hacs.utils.version import version_left_higher_or_equal_then_right


def test_basic():
assert version_left_higher_then_right("1.0.0", "0.9.9")
assert version_left_higher_then_right("1", "0.9.9")
assert version_left_higher_then_right("1.1", "0.9.9")
assert version_left_higher_then_right("0.10.0", "0.9.9")
assert not version_left_higher_then_right("0.0.10", "0.9.9")
assert not version_left_higher_then_right("0.9.0", "0.9.9")
assert version_left_higher_then_right("1.0.0", "1.0.0")
assert version_left_higher_or_equal_then_right("1.0.0", "0.9.9")
assert version_left_higher_or_equal_then_right("1", "0.9.9")
assert version_left_higher_or_equal_then_right("1.1", "0.9.9")
assert version_left_higher_or_equal_then_right("0.10.0", "0.9.9")
assert not version_left_higher_or_equal_then_right("0.0.10", "0.9.9")
assert not version_left_higher_or_equal_then_right("0.9.0", "0.9.9")
assert version_left_higher_or_equal_then_right("1.0.0", "1.0.0")


def test_beta():
assert version_left_higher_then_right("1.0.0b1", "1.0.0b0")
assert not version_left_higher_then_right("1.0.0b1", "1.0.0")
assert version_left_higher_then_right("1.0.0", "1.0.0b1")
assert version_left_higher_or_equal_then_right("1.0.0b1", "1.0.0b0")
assert not version_left_higher_or_equal_then_right("1.0.0b1", "1.0.0")
assert version_left_higher_or_equal_then_right("1.0.0", "1.0.0b1")


def test_wierd_stuff():
assert version_left_higher_then_right("1.0.0rc1", "1.0.0b1")
assert not version_left_higher_then_right("1.0.0a1", "1.0.0b1")
assert version_left_higher_then_right("1.0.0", "1.0.0a0")
assert version_left_higher_then_right("1.0.0", "1.0.0b0")
assert version_left_higher_then_right("1.0.0", "1.0.0rc0")
assert not version_left_higher_then_right("0", "1.0.0rc0")
assert version_left_higher_or_equal_then_right("1.0.0rc1", "1.0.0b1")
assert not version_left_higher_or_equal_then_right("1.0.0a1", "1.0.0b1")
assert version_left_higher_or_equal_then_right("1.0.0", "1.0.0a0")
assert version_left_higher_or_equal_then_right("1.0.0", "1.0.0b0")
assert version_left_higher_or_equal_then_right("1.0.0", "1.0.0rc0")
assert not version_left_higher_or_equal_then_right("0", "1.0.0rc0")

0 comments on commit 76a7e9b

Please sign in to comment.