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