mirror of
https://github.com/rendercv/rendercv.git
synced 2025-12-23 21:47:55 -05:00
Improve packaging version comparison (#299)
* proper packaging version comparison * fix tests * test_rendercv_no_version_when_there_is_no_new_version * Augment test * small changes --------- Co-authored-by: Sina Atalay <dev@atalay.biz>
This commit is contained in:
committed by
GitHub
parent
efe308ae1c
commit
0f37939f7d
@@ -85,6 +85,7 @@ full = [
|
||||
"watchdog==6.0.0", # to poll files for updates
|
||||
"typst==0.12.2", # to render PDF from Typst source files
|
||||
"rendercv-fonts==0.2.0", # some font files for RenderCV
|
||||
"packaging==24.2", # to validate the version number
|
||||
]
|
||||
|
||||
[project.urls]
|
||||
|
||||
@@ -22,6 +22,8 @@ except ImportError as e:
|
||||
from .. import _parial_install_error_message
|
||||
|
||||
raise ImportError(_parial_install_error_message) from e
|
||||
|
||||
import packaging.version
|
||||
from rich import print
|
||||
|
||||
from .. import __version__, data
|
||||
@@ -126,7 +128,8 @@ def warn_if_new_version_is_available() -> bool:
|
||||
True if there is a new version, and False otherwise.
|
||||
"""
|
||||
latest_version = utilities.get_latest_version_number_from_pypi()
|
||||
if latest_version is not None and __version__ != latest_version:
|
||||
version = packaging.version.Version(__version__)
|
||||
if latest_version is not None and version < latest_version:
|
||||
warning(
|
||||
f"A new version of RenderCV is available! You are using v{__version__},"
|
||||
f" and the latest version is v{latest_version}."
|
||||
|
||||
@@ -20,6 +20,8 @@ except ImportError as e:
|
||||
|
||||
raise ImportError(_parial_install_error_message) from e
|
||||
|
||||
import packaging.version
|
||||
|
||||
from .. import data, renderer
|
||||
from . import printer
|
||||
|
||||
@@ -122,7 +124,7 @@ def copy_files(paths: list[pathlib.Path] | pathlib.Path, new_path: pathlib.Path)
|
||||
shutil.copy2(file_path, png_path_with_page_number)
|
||||
|
||||
|
||||
def get_latest_version_number_from_pypi() -> Optional[str]:
|
||||
def get_latest_version_number_from_pypi() -> Optional[packaging.version.Version]:
|
||||
"""Get the latest version number of RenderCV from PyPI.
|
||||
|
||||
Example:
|
||||
@@ -143,7 +145,8 @@ def get_latest_version_number_from_pypi() -> Optional[str]:
|
||||
data = response.read()
|
||||
encoding = response.info().get_content_charset("utf-8")
|
||||
json_data = json.loads(data.decode(encoding))
|
||||
version = json_data["info"]["version"]
|
||||
version_string = json_data["info"]["version"]
|
||||
version = packaging.version.Version(version_string)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import sys
|
||||
import time
|
||||
from datetime import date as Date
|
||||
|
||||
import packaging.version
|
||||
import pydantic
|
||||
import pytest
|
||||
import ruamel.yaml
|
||||
@@ -670,12 +671,14 @@ def test_main_file():
|
||||
|
||||
def test_get_latest_version_number_from_pypi():
|
||||
version = utilities.get_latest_version_number_from_pypi()
|
||||
assert isinstance(version, str)
|
||||
assert isinstance(version, packaging.version.Version)
|
||||
|
||||
|
||||
def test_if_welcome_prints_new_version_available(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
utilities, "get_latest_version_number_from_pypi", lambda: "99999"
|
||||
utilities,
|
||||
"get_latest_version_number_from_pypi",
|
||||
lambda: packaging.version.Version("99.99.99"),
|
||||
)
|
||||
import contextlib
|
||||
import io
|
||||
@@ -689,7 +692,9 @@ def test_if_welcome_prints_new_version_available(monkeypatch):
|
||||
|
||||
def test_rendercv_version_when_there_is_a_new_version(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
utilities, "get_latest_version_number_from_pypi", lambda: "99999"
|
||||
utilities,
|
||||
"get_latest_version_number_from_pypi",
|
||||
lambda: packaging.version.Version("99.99.99"),
|
||||
)
|
||||
|
||||
result = runner.invoke(cli.app, ["--version"])
|
||||
@@ -697,24 +702,46 @@ def test_rendercv_version_when_there_is_a_new_version(monkeypatch):
|
||||
assert "A new version of RenderCV is available!" in result.stdout
|
||||
|
||||
|
||||
def test_rendercv_version_when_there_is_not_a_new_version(monkeypatch):
|
||||
def test_rendercv_no_version_when_there_is_no_new_version(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
utilities, "get_latest_version_number_from_pypi", lambda: __version__
|
||||
utilities,
|
||||
"get_latest_version_number_from_pypi",
|
||||
lambda: packaging.version.Version("00.00.00"),
|
||||
)
|
||||
|
||||
result = runner.invoke(cli.app, ["--version"])
|
||||
|
||||
assert "A new version of RenderCV is available!" not in result.stdout
|
||||
assert __version__ in result.stdout
|
||||
|
||||
|
||||
def test_rendercv_version_when_there_is_not_a_new_version(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
utilities,
|
||||
"get_latest_version_number_from_pypi",
|
||||
lambda: packaging.version.Version(__version__),
|
||||
)
|
||||
|
||||
result = runner.invoke(cli.app, ["--version"])
|
||||
|
||||
assert "A new version of RenderCV is available!" not in result.stdout
|
||||
assert __version__ in result.stdout
|
||||
|
||||
|
||||
def test_warn_if_new_version_is_available(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
utilities, "get_latest_version_number_from_pypi", lambda: __version__
|
||||
utilities,
|
||||
"get_latest_version_number_from_pypi",
|
||||
lambda: packaging.version.Version(__version__),
|
||||
)
|
||||
|
||||
assert not printer.warn_if_new_version_is_available()
|
||||
|
||||
monkeypatch.setattr(utilities, "get_latest_version_number_from_pypi", lambda: "999")
|
||||
monkeypatch.setattr(
|
||||
utilities,
|
||||
"get_latest_version_number_from_pypi",
|
||||
lambda: packaging.version.Version("99.99.99"),
|
||||
)
|
||||
|
||||
assert printer.warn_if_new_version_is_available()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user