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 <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2021-12-22 22:22:48 -08:00 committed by James Valleroy
parent 131793bfe3
commit 85149cb5d1
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 39 additions and 0 deletions

View File

@ -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:

View File

@ -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."""