Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if current version is the latest version #157

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
- id: black
language: python
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 7.1.0
hooks:
- id: flake8
language: python
Expand Down
3 changes: 3 additions & 0 deletions gcapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from gcapi.check_version import check_version
from gcapi.gcapi import AsyncClient, Client

check_version()

__author__ = """James Meakin"""
__email__ = "code@jmsmkn.com"
__all__ = ["AsyncClient", "Client"]
31 changes: 31 additions & 0 deletions gcapi/check_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import warnings
from importlib.metadata import version as get_version

import httpx
from packaging import version


def check_version():
package_name = "gcapi"
try:
current_version = get_version(package_name)
with httpx.Client() as client:
response = client.get(f"https://pypi.org/pypi/{package_name}/json")
latest_version = response.json()["info"]["version"]

if version.parse(current_version) < version.parse(latest_version):
warnings.warn(
f"You are using {package_name} version {current_version}. "
f"However, version {latest_version} is available. You should consider"
f" upgrading via `pip install --upgrade {package_name}`",
UserWarning,
stacklevel=0,
)
except Exception:
# If there's any error in checking the version, we'll silently pass
# This ensures the import process isn't disrupted
pass


# Call the function when the package is imported
check_version()
66 changes: 66 additions & 0 deletions tests/test_check_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import warnings
from unittest.mock import MagicMock, patch

import httpx
import pytest

from gcapi import check_version


@pytest.fixture
def mock_get_version():
with patch("gcapi.check_version.get_version") as mock:
yield mock


@pytest.fixture
def mock_httpx_client():
with patch("gcapi.check_version.httpx.Client") as mock:
yield mock


@pytest.mark.parametrize(
"current,latest,should_warn",
[
("1.0.0", "1.0.1", True),
("1.0.0", "1.1.0", True),
("1.0.0", "2.0.0", True),
("1.0.0", "1.0.0", False),
("1.0.1", "1.0.0", False),
("1.1.0", "1.0.0", False),
("2.0.0", "1.0.0", False),
],
)
def test_check_version_comparisons(
mock_get_version, mock_httpx_client, current, latest, should_warn
):
mock_get_version.return_value = current
mock_response = MagicMock()
mock_response.json.return_value = {"info": {"version": latest}}
mock_httpx_client.return_value.__enter__.return_value.get.return_value = (
mock_response
)

with warnings.catch_warnings(record=True) as w:
check_version()

if should_warn:
assert (
len(w) == 1
), f"A warning should be issued for version {current} < {latest}"
assert f"You are using gcapi version {current}" in str(w[0].message)
else:
assert (
len(w) == 0
), f"No warning should be issued for version {current} >= {latest}"


def test_check_version_network_error(mock_httpx_client):
mock_httpx_client.return_value.__enter__.return_value.get.side_effect = (
httpx.RequestError("Network error")
)

with warnings.catch_warnings(record=True) as w:
check_version()

assert len(w) == 0, "No warning should be issued for network errors"
Loading