From 85149cb5d1729a40e86b2d3e11bfea9511d69f80 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 22 Dec 2021 22:22:48 -0800 Subject: [PATCH] package: Add diagnostic to check if a package is the latest version Closes: #2148. Tests: - For an app with older version of package installed, run diagnostics. A warning is shown. Latest version available is shown correctly in the message. - For an app with latest version of package installed, run diagnostics. Test shows as passed. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/package.py | 21 +++++++++++++++++++++ plinth/tests/test_package.py | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/plinth/package.py b/plinth/package.py index 80951c39e..c7f99d211 100644 --- a/plinth/package.py +++ b/plinth/package.py @@ -78,6 +78,27 @@ class Packages(app.FollowerComponent): helper = module.setup_helper helper.install(self.packages, skip_recommends=self.skip_recommends) + def diagnose(self): + """Run diagnostics and return results.""" + results = super().diagnose() + cache = apt.Cache() + for package_name in self.packages: + result = 'warning' + latest_version = '?' + if package_name in cache: + package = cache[package_name] + latest_version = package.candidate.version + if package.candidate.is_installed: + result = 'passed' + + message = _('Package {package_name} is the latest version ' + '({latest_version})').format( + package_name=package_name, + latest_version=latest_version) + results.append([message, result]) + + return results + def find_conflicts(self) -> Optional[list[str]]: """Return list of conflicting packages installed on the system.""" if not self.conflicts: diff --git a/plinth/tests/test_package.py b/plinth/tests/test_package.py index e60b3a0aa..c67d4c25f 100644 --- a/plinth/tests/test_package.py +++ b/plinth/tests/test_package.py @@ -60,6 +60,24 @@ def test_packages_setup(): [call(['foo2', 'bar2'], skip_recommends=True)]) +@patch('apt.Cache') +def test_diagnose(cache): + """Test checking for latest version of the package.""" + cache.return_value = { + 'package2': Mock(candidate=Mock(version='2.0', is_installed=True)), + 'package3': Mock(candidate=Mock(version='3.0', is_installed=False)), + } + component = Packages('test-component', + ['package1', 'package2', 'package3']) + results = component.diagnose() + assert '(?)' in results[0][0] + assert results[0][1] == 'warning' + assert '(2.0)' in results[1][0] + assert results[1][1] == 'passed' + assert '(3.0)' in results[2][0] + assert results[2][1] == 'warning' + + @patch('plinth.package.packages_installed') def test_packages_find_conflicts(packages_installed_): """Test finding conflicts."""