diff --git a/openbb_terminal/terminal_helper.py b/openbb_terminal/terminal_helper.py index 63a0ab3801b..a7dee8dbbf4 100644 --- a/openbb_terminal/terminal_helper.py +++ b/openbb_terminal/terminal_helper.py @@ -9,7 +9,8 @@ import logging import os import subprocess # nosec import sys -from typing import List, Optional +from typing import List, Union +from packaging import version # IMPORTATION THIRDPARTY import requests @@ -220,19 +221,19 @@ def check_for_updates() -> None: r = None if r is not None and r.status_code == 200: - latest_version = r.json()["tag_name"] - latest_version_number = get_version_number(tag=latest_version) - current_version_number = get_version_number(tag=obbff.VERSION) + latest_tag_name = r.json()["tag_name"] + latest_version = version.parse(latest_tag_name) + current_version = version.parse(obbff.VERSION) - if latest_version_number and current_version_number: + if check_valid_versions(latest_version, current_version): - if current_version_number == latest_version_number: + if current_version == latest_version: console.print("[green]You are using the latest stable version[/green]") else: console.print( "[yellow]You are not using the latest stable version[/yellow]" ) - if current_version_number < latest_version_number: + if current_version < latest_version: console.print( "[yellow]Check for updates at https://openbb.co/products/terminal#get-started[/yellow]" ) @@ -252,21 +253,18 @@ def check_for_updates() -> None: console.print("\n") -def get_version_number(tag: str) -> Optional[str]: - """Get the release number from a tag.""" - - if not tag.startswith("v") and len(tag.split(".")) != 3: - return None - - if tag.startswith("v"): - tag = tag[1:] - - if "rc" in tag.split(".")[-1]: - tag_split = tag.split(".") - tag_split[-1] = tag_split[-1].split("rc")[0] - tag = ".".join(tag_split) - - return tag +def check_valid_versions( + latest_version: Union[version.LegacyVersion, version.Version], + current_version: Union[version.LegacyVersion, version.Version], +) -> bool: + if ( + not latest_version + or not current_version + or not isinstance(latest_version, version.Version) + or not isinstance(current_version, version.Version) + ): + return False + return True def welcome_message(): diff --git a/tests/openbb_terminal/test_terminal_helper.py b/tests/openbb_terminal/test_terminal_helper.py index 674d3331ed6..54d79916bb4 100644 --- a/tests/openbb_terminal/test_terminal_helper.py +++ b/tests/openbb_terminal/test_terminal_helper.py @@ -2,6 +2,7 @@ import sys import unittest from contextlib import contextmanager from unittest.mock import patch +import pytest from openbb_terminal import terminal_helper from tests.helpers.helpers import check_print @@ -39,3 +40,51 @@ class TestMainHelper(unittest.TestCase): def test_reset(self, mock): # pylint: disable=unused-argument terminal_helper.reset() + + +@pytest.mark.vcr(record_mode="none") +@pytest.mark.record_stdout +@pytest.mark.parametrize( + "last_release, current_release", + [ + ("v0.0.1", "v0.0.1"), + ("v0.0.2", "v0.0.1"), + ("v0.0.1", "v0.0.2"), + ("v1.9.0", "v1.10.0"), + ("v1.10.0", "v1.9.0"), + ("xyz", "v1.9.0"), + ("2.0.0", "2.0.0rc1"), + ("2.0.0rc1", "2.0.0"), + ("2.0.0rc1", "2.0.0rc2"), + ("2.0.0rc2", "2.0.0rc1"), + ("3.41.99", "88.123.456"), + ("88.123.456", "3.41.99"), + ("3.41.99", "3.41.99"), + ("3.41.99", "3.41.99rc1"), + ("8.10.01", "8.1.01"), + ], +) +def test_check_for_updates(mocker, last_release, current_release): + # MOCK GET + attrs = { + "status_code": 200, + "json.return_value": { + "url": "mock_url", + "assets_url": "mock_url", + "upload_url": "mock_url", + "html_url": "mock_url", + "node_id": "mock_node_id", + "tag_name": last_release, + "name": "mock_terminal_name", + }, + } + mock_response = mocker.Mock(**attrs) + mocker.patch(target="requests.get", new=mocker.Mock(return_value=mock_response)) + + # MOCK FF VERSION + mocker.patch( + target="openbb_terminal.feature_flags.VERSION", + new=current_release, + ) + + terminal_helper.check_for_updates() diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0-2.0.0rc1].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0-2.0.0rc1].txt new file mode 100644 index 00000000000..ebb1d30e693 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0-2.0.0rc1].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]Check for updates at https://openbb.co/products/terminal#get-started[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0rc1-2.0.0].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0rc1-2.0.0].txt new file mode 100644 index 00000000000..1829b0d4e81 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0rc1-2.0.0].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]You are using an unreleased version[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0rc1-2.0.0rc2].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0rc1-2.0.0rc2].txt new file mode 100644 index 00000000000..1829b0d4e81 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0rc1-2.0.0rc2].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]You are using an unreleased version[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0rc2-2.0.0rc1].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0rc2-2.0.0rc1].txt new file mode 100644 index 00000000000..ebb1d30e693 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[2.0.0rc2-2.0.0rc1].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]Check for updates at https://openbb.co/products/terminal#get-started[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[3.41.99-3.41.99].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[3.41.99-3.41.99].txt new file mode 100644 index 00000000000..9e44b60e915 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[3.41.99-3.41.99].txt @@ -0,0 +1,3 @@ +[green]You are using the latest stable version[/green] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[3.41.99-3.41.99rc1].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[3.41.99-3.41.99rc1].txt new file mode 100644 index 00000000000..ebb1d30e693 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[3.41.99-3.41.99rc1].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]Check for updates at https://openbb.co/products/terminal#get-started[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[3.41.99-88.123.456].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[3.41.99-88.123.456].txt new file mode 100644 index 00000000000..1829b0d4e81 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[3.41.99-88.123.456].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]You are using an unreleased version[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[8.10.01-8.1.01].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[8.10.01-8.1.01].txt new file mode 100644 index 00000000000..ebb1d30e693 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[8.10.01-8.1.01].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]Check for updates at https://openbb.co/products/terminal#get-started[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[88.123.456-3.41.99].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[88.123.456-3.41.99].txt new file mode 100644 index 00000000000..ebb1d30e693 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[88.123.456-3.41.99].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]Check for updates at https://openbb.co/products/terminal#get-started[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v0.0.1-v0.0.1].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v0.0.1-v0.0.1].txt new file mode 100644 index 00000000000..9e44b60e915 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v0.0.1-v0.0.1].txt @@ -0,0 +1,3 @@ +[green]You are using the latest stable version[/green] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v0.0.1-v0.0.2].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v0.0.1-v0.0.2].txt new file mode 100644 index 00000000000..1829b0d4e81 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v0.0.1-v0.0.2].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]You are using an unreleased version[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v0.0.2-v0.0.1].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v0.0.2-v0.0.1].txt new file mode 100644 index 00000000000..ebb1d30e693 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v0.0.2-v0.0.1].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]Check for updates at https://openbb.co/products/terminal#get-started[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v1.10.0-v1.9.0].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v1.10.0-v1.9.0].txt new file mode 100644 index 00000000000..ebb1d30e693 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v1.10.0-v1.9.0].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]Check for updates at https://openbb.co/products/terminal#get-started[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v1.9.0-v1.10.0].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v1.9.0-v1.10.0].txt new file mode 100644 index 00000000000..1829b0d4e81 --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[v1.9.0-v1.10.0].txt @@ -0,0 +1,4 @@ +[yellow]You are not using the latest stable version[/yellow] +[yellow]You are using an unreleased version[/yellow] + + diff --git a/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[xyz-v1.9.0].txt b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[xyz-v1.9.0].txt new file mode 100644 index 00000000000..11cd4741d9c --- /dev/null +++ b/tests/openbb_terminal/txt/test_terminal_helper/test_check_for_updates[xyz-v1.9.0].txt @@ -0,0 +1,3 @@ +[red]You are using an unrecognized version.[/red] + +