feat: add util module with exists() function

This commit is contained in:
Lance Edgar 2024-09-12 12:55:28 -05:00
parent e3b593d628
commit 4879887cb3
4 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,6 @@
``wuttamess.util``
==================
.. automodule:: wuttamess.util
:members:

View file

@ -33,3 +33,4 @@ project.
api/wuttamess.apt api/wuttamess.apt
api/wuttamess.postfix api/wuttamess.postfix
api/wuttamess.sync api/wuttamess.sync
api/wuttamess.util

32
src/wuttamess/util.py Normal file
View file

@ -0,0 +1,32 @@
# -*- coding: utf-8; -*-
################################################################################
#
# WuttaMess -- Fabric Automation Helpers
# Copyright © 2024 Lance Edgar
#
# This file is part of Wutta Framework.
#
# Wutta Framework is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Wutta Framework is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Misc. Utilities
"""
def exists(c, path):
"""
Returns ``True`` if given path exists on the host, otherwise ``False``.
"""
return not c.run(f'test -e {path}', warn=True).failed

14
tests/test_util.py Normal file
View file

@ -0,0 +1,14 @@
# -*- coding: utf-8; -*-
from unittest import TestCase
from unittest.mock import MagicMock
from wuttamess import util as mod
class TestExists(TestCase):
def test_basic(self):
c = MagicMock()
mod.exists(c, '/foo')
c.run.assert_called_once_with('test -e /foo', warn=True)