diff --git a/src/lightning_app/CHANGELOG.md b/src/lightning_app/CHANGELOG.md index 2aa5c7cdd8..5a88748832 100644 --- a/src/lightning_app/CHANGELOG.md +++ b/src/lightning_app/CHANGELOG.md @@ -45,7 +45,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ### Fixed -- +- Resolved a bug where the install command was not installing the latest version of an app/component by default ([#14181](https://github.com/Lightning-AI/lightning/pull/14181)) ## [0.5.5] - 2022-08-9 diff --git a/src/lightning_app/cli/cmd_install.py b/src/lightning_app/cli/cmd_install.py index f15567bd84..8f0e45145e 100644 --- a/src/lightning_app/cli/cmd_install.py +++ b/src/lightning_app/cli/cmd_install.py @@ -1,4 +1,3 @@ -import json import logging import os import re @@ -7,6 +6,7 @@ import subprocess import sys import requests +from packaging.version import Version from lightning_app.core.constants import LIGHTNING_APPS_PUBLIC_REGISTRY, LIGHTNING_COMPONENT_PUBLIC_REGISTRY @@ -299,8 +299,8 @@ def _validate_name(name, resource_type, example): def _resolve_resource(registry_url, name, version_arg, resource_type): gallery_entries = [] try: - url = requests.get(registry_url) - data = json.loads(url.text) + response = requests.get(registry_url) + data = response.json() if resource_type == "app": gallery_entries = [a for a in data["apps"] if a["canDownloadSourceCode"]] @@ -328,7 +328,7 @@ def _resolve_resource(registry_url, name, version_arg, resource_type): entry = None if version_arg == "latest": - entry = entries[-1] + entry = max(entries, key=lambda app: Version(app["version"])) else: for e in entries: if e["version"] == version_arg: diff --git a/tests/tests_app/cli/test_cmd_install.py b/tests/tests_app/cli/test_cmd_install.py index 2d277ddb77..0139bbc9c5 100644 --- a/tests/tests_app/cli/test_cmd_install.py +++ b/tests/tests_app/cli/test_cmd_install.py @@ -212,6 +212,38 @@ def test_version_arg_app(tmpdir): assert result.exit_code == 0 +@mock.patch("lightning_app.cli.cmd_install.subprocess", mock.MagicMock()) +@mock.patch("lightning_app.cli.cmd_install.os.chdir", mock.MagicMock()) +@mock.patch("lightning_app.cli.cmd_install._show_install_app_prompt") +def test_install_resolve_latest_version(mock_show_install_app_prompt, tmpdir): + + app_name = "lightning/invideo" + runner = CliRunner() + with mock.patch("lightning_app.cli.cmd_install.requests.get") as get_api_mock: + get_api_mock.return_value.json.return_value = { + "apps": [ + { + "canDownloadSourceCode": True, + "version": "0.0.2", + "name": "lightning/invideo", + }, + { + "canDownloadSourceCode": True, + "version": "0.0.4", + "name": "lightning/invideo", + }, + { + "canDownloadSourceCode": True, + "version": "0.0.5", + "name": "another_app", + }, + ] + } + runner.invoke(lightning_cli.install_app, [app_name, "--yes"]) # no version specified so latest is installed + assert mock_show_install_app_prompt.called + assert mock_show_install_app_prompt.call_args[0][0]["version"] == "0.0.4" + + def test_proper_url_parsing(): name = "lightning/invideo"