feat: add apt.is_installed() function

This commit is contained in:
Lance Edgar 2024-11-20 19:09:55 -06:00
parent 12daf6a1e3
commit 4dede6072c
2 changed files with 28 additions and 0 deletions

View file

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

View file

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