From 4dede6072c4a8d20aed8583bfd98d83dd24ca588 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 20 Nov 2024 19:09:55 -0600 Subject: [PATCH] feat: add `apt.is_installed()` function --- src/wuttamess/apt.py | 13 +++++++++++++ tests/test_apt.py | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/wuttamess/apt.py b/src/wuttamess/apt.py index 32f526c..e11dca2 100644 --- a/src/wuttamess/apt.py +++ b/src/wuttamess/apt.py @@ -51,6 +51,19 @@ def install(c, *packages, **kwargs): return c.run(f'DEBIAN_FRONTEND={frontend} apt-get --assume-yes install {packages}') +def is_installed(c, package): + """ + Check if the given APT package is installed. + + :param c: Fabric connection. + + :param package: Name of package to be checked. + + :returns: ``True`` if package is installed, else ``False``. + """ + return c.run(f'dpkg-query -s {package}', warn=True).ok + + def update(c): """ Update the APT package lists. Essentially this runs: diff --git a/tests/test_apt.py b/tests/test_apt.py index 57f77d9..50c80a9 100644 --- a/tests/test_apt.py +++ b/tests/test_apt.py @@ -25,6 +25,21 @@ class TestInstall(TestCase): c.run.assert_called_once_with('DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install postfix') +class TestIsInstalled(TestCase): + + def test_already_installed(self): + c = MagicMock() + c.run.return_value.ok = True + self.assertTrue(mod.is_installed(c, 'postfix')) + c.run.assert_called_once_with('dpkg-query -s postfix', warn=True) + + def test_not_installed(self): + c = MagicMock() + c.run.return_value.ok = False + self.assertFalse(mod.is_installed(c, 'postfix')) + c.run.assert_called_once_with('dpkg-query -s postfix', warn=True) + + class TestUpdate(TestCase): def test_basic(self):