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

fix: version message, add test for version #191

Merged
merged 6 commits into from
Aug 25, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/scribe_data/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from scribe_data.cli.interactive import start_interactive_mode
from scribe_data.cli.list import list_wrapper
from scribe_data.cli.total import get_total_lexemes
from scribe_data.cli.upgrade import upgrade
from scribe_data.cli.upgrade import upgrade_cli
from scribe_data.cli.version import get_version_message

LIST_DESCRIPTION = "List languages, data types and combinations of each that Scribe-Data can be used for."
Expand Down Expand Up @@ -196,7 +196,7 @@ def main() -> None:
args = parser.parse_args()

if args.upgrade:
upgrade()
upgrade_cli()
return

if not args.command:
Expand Down
2 changes: 1 addition & 1 deletion src/scribe_data/cli/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from scribe_data.cli.version import get_latest_version, get_local_version


def upgrade():
def upgrade_cli():
local_version = get_local_version()
latest_version = get_latest_version()
latest_version = latest_version.split("v")[-1]
Expand Down
4 changes: 1 addition & 3 deletions src/scribe_data/cli/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ def get_version_message():
if local_version == latest_version:
return f"{local_version}"

update_message = (
f"Scribe-Data v{local_version} (Update available: v{latest_version})\n"
)
update_message = f"{local_version} (Upgrade available: {latest_version})\n"
update_message += "To update: pip scribe-data --upgrade"

return update_message
80 changes: 80 additions & 0 deletions tests/cli/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Tests for the CLI version functionality.

.. raw:: html
<!--
* Copyright (C) 2024 Scribe
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
"""

import unittest
from unittest.mock import patch

import pkg_resources
from scribe_data.cli.version import (
get_latest_version,
get_local_version,
get_version_message,
)


class TestVersionFunctions(unittest.TestCase):
@patch("pkg_resources.get_distribution")
def test_get_local_version_installed(self, mock_get_distribution):
mock_get_distribution.return_value.version = "1.0.0"
self.assertEqual(get_local_version(), "1.0.0")

@patch(
"pkg_resources.get_distribution", side_effect=pkg_resources.DistributionNotFound
)
def test_get_local_version_not_installed(self, mock_get_distribution):
self.assertEqual(get_local_version(), "Unknown (Not installed via pip)")

@patch("requests.get")
def test_get_latest_version(self, mock_get):
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {"name": "v1.0.1"}
self.assertEqual(get_latest_version(), "v1.0.1")

@patch("requests.get", side_effect=Exception("Unable to fetch version"))
def test_get_latest_version_failure(self, mock_get):
self.assertEqual(get_latest_version(), "Unknown (Unable to fetch version)")

@patch("scribe_data.cli.version.get_local_version", return_value="1.0.0")
@patch(
"scribe_data.cli.version.get_latest_version", return_value="Scribe-Data v1.0.0"
)
def test_get_version_message_up_to_date(
self, mock_latest_version, mock_local_version
):
"""
Tests the scenario where the local version is up to date with the latest version.
"""
expected_message = "Scribe-Data v1.0.0"
self.assertEqual(get_version_message(), expected_message)

@patch("scribe_data.cli.version.get_local_version", return_value="1.0.0")
@patch(
"scribe_data.cli.version.get_latest_version", return_value="Scribe-Data v1.0.1"
)
def test_get_version_message_update_available(
self, mock_latest_version, mock_local_version
):
"""
Tests the scenario where a newer version is available, suggesting an update.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor thing here that is more a personal thing for me, @axif0, I added the periods here as Python standards state that comments that are their own line should have a period at the end (and should also start with a capitalized work - they're full sentences). For inline comments it's best to not capitalize the first word and not end in a period :)

For next time! 😊

"""
expected_message = "Scribe-Data v1.0.0 (Upgrade available: Scribe-Data v1.0.1)\nTo update: pip scribe-data --upgrade"
self.assertEqual(get_version_message(), expected_message)
Loading