From d1b040cdb633c966fffd8d904f0aaf042ebebf13 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 16 Nov 2021 08:21:08 -0800 Subject: [PATCH] package: Implement installing packages in the component Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/package.py | 9 +++++++++ plinth/tests/test_package.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/plinth/package.py b/plinth/package.py index 7257c5ca7..db9a07428 100644 --- a/plinth/package.py +++ b/plinth/package.py @@ -6,6 +6,7 @@ Framework for installing and updating distribution packages import json import logging import subprocess +import sys import threading from typing import Union @@ -50,6 +51,14 @@ class Packages(app.FollowerComponent): """Return the list of packages managed by this component.""" return self._packages + def setup(self, old_version): + """Install the packages.""" + # TODO: Drop the need for setup helper. + module_name = self.app.__module__ + module = sys.modules[module_name] + helper = module.setup_helper + helper.install(self.packages, skip_recommends=self.skip_recommends) + class PackageException(Exception): """A package operation has failed.""" diff --git a/plinth/tests/test_package.py b/plinth/tests/test_package.py index 8ae037f77..8f6db81eb 100644 --- a/plinth/tests/test_package.py +++ b/plinth/tests/test_package.py @@ -3,13 +3,16 @@ Test module for package module. """ -from unittest.mock import call, patch +from unittest.mock import Mock, call, patch import pytest +from plinth.app import App from plinth.errors import ActionError from plinth.package import Packages, packages_installed, remove +setup_helper = Mock() + def test_packages_init(): """Test initialization of packages component.""" @@ -26,6 +29,31 @@ def test_packages_init(): assert component.skip_recommends +def test_packages_setup(): + """Test setting up packages component.""" + + class TestApp(App): + """Test app""" + app_id = 'test-app' + + component = Packages('test-component', ['foo1', 'bar1']) + app = TestApp() + app.add(component) + setup_helper.reset_mock() + app.setup(old_version=3) + setup_helper.install.assert_has_calls( + [call(['foo1', 'bar1'], skip_recommends=False)]) + + component = Packages('test-component', ['foo2', 'bar2'], + skip_recommends=True) + app = TestApp() + app.add(component) + setup_helper.reset_mock() + app.setup(old_version=3) + setup_helper.install.assert_has_calls( + [call(['foo2', 'bar2'], skip_recommends=True)]) + + def test_packages_installed(): """Test packages_installed().""" # list as input