Skip to content

Commit

Permalink
fix: fix version comparison when checking for new versions
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-makerx committed Feb 24, 2023
1 parent aef7e5e commit a66db6f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/algokit/core/version_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ def do_version_prompt() -> None:
logger.debug("Could not determine latest version")
return

if current_version != latest_version:
if _get_version_sequence(current_version) < _get_version_sequence(latest_version):
logger.info(f"You are using AlgoKit version {current_version}, however version {latest_version} is available.")
else:
logger.debug("Current version is up to date")


def _get_version_sequence(version: str) -> list[int | str]:
match = re.match(r"(\d+)\.(\d+)\.(\d+)(.*)", version)
if match:
return [int(x) for x in match.groups()[:3]] + [match.group(4)]
return [version]


def get_latest_version_or_cached() -> str | None:
version_check_path = get_app_state_dir() / "last-version-check"

Expand Down Expand Up @@ -61,9 +68,6 @@ def get_latest_version_or_cached() -> str | None:

def get_latest_github_version() -> str:
headers = {"ACCEPT": "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28"}
# TODO: remove GH_TOKEN auth once algokit repo is public
if gh_token := os.getenv("GH_TOKEN"):
headers["Authorization"] = f"Bearer {gh_token}"

response = httpx.get(LATEST_URL, headers=headers)
response.raise_for_status()
Expand Down
49 changes: 46 additions & 3 deletions tests/version_check/test_version_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from approvaltests.scrubbers.scrubbers import Scrubber, combine_scrubbers
from pytest_httpx import HTTPXMock
from pytest_mock import MockerFixture
from utils.app_dir_mock import AppDirs
from utils.approvals import normalize_path, verify
from utils.click_invoker import invoke

from tests.utils.app_dir_mock import AppDirs
from tests.utils.approvals import normalize_path, verify
from tests.utils.click_invoker import invoke

CURRENT_VERSION = metadata.version(PACKAGE_NAME)
NEW_VERSION = "999.99.99"
Expand Down Expand Up @@ -43,6 +44,48 @@ def test_version_check_queries_github_when_no_cache(app_dir_mock: AppDirs, httpx
verify(result.output, scrubber=make_scrubber(app_dir_mock))


@pytest.mark.parametrize(
"current_version,latest_version,warning_expected",
[
("0.2.0", "0.3.0", True),
("0.25.0", "0.30.0", True),
("0.3.0", "0.29.0", True),
("999.99.99", "1000.00.00", True),
("999.99.99-beta", "1000.00.00", True),
("999.99.99-alpha", "999.99.99-beta", True),
("0.25.0", "1.0.0", True),
("0.29.0", "1.0.0", True),
("0.3.0", "1.0.0", True),
("0.3.0", "0.2.0", False),
("0.3.0", "0.3.0", False),
("0.30.0", "0.25.0", False),
("0.29.0", "0.3.0", False),
("0.30.0", "0.30.0", False),
("1.0.0", "0.25.0", False),
("1.0.0", "0.29.0", False),
("1.0.0", "0.3.0", False),
("1.0.0", "1.0.0", False),
("999.99.99", "998.0.0", False),
("999.99.99", "999.99.0", False),
("999.99.99", "999.99.99", False),
("999.99.99-beta", "998.99.99", False),
("999.99.99-beta", "999.99.99-alpha", False),
],
)
def test_version_check_only_warns_if_newer_version_is_found(
app_dir_mock: AppDirs, mocker: MockerFixture, current_version: str, latest_version: str, *, warning_expected: bool
):
mocker.patch("algokit.core.version_prompt.get_current_package_version").return_value = current_version
version_cache = app_dir_mock.app_state_dir / "last-version-check"
version_cache.write_text(latest_version, encoding="utf-8")
result = invoke("bootstrap env", skip_version_check=False)

if warning_expected:
assert f"version {latest_version} is available" in result.output
else:
assert f"version {latest_version} is available" not in result.output


def test_version_check_uses_cache(app_dir_mock: AppDirs):
version_cache = app_dir_mock.app_state_dir / "last-version-check"
version_cache.write_text("1234.56.78", encoding="utf-8")
Expand Down

0 comments on commit a66db6f

Please sign in to comment.