diff --git a/tests/__init__.py b/tests/__init__.py index 818edd7..6e2a805 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -32,6 +32,8 @@ - tests.captured() Context manager to capture stdout. - tests.mocked() Mock cloud password managers API response. - tests.skipIfNo() Skip a password manager test if it is disabled. + - tests.skipIfNoInstalled() Skip a test if a program is not installed. + - tests.skipIfNoModule() Skip a test if an optional module is not installed. - tests.mock_hibp() Mock HIBP API response. """ @@ -61,20 +63,32 @@ def _id(obj): return obj -def skipIfNo(name, imported=True): +def skipIfNo(name: str): """Skip a password manager test if it is disabled.""" - if imported: - if name not in {'bitwarden', 'lastpass', 'onepassword'}: - return _id - manager = name.upper() - enabled = 'T_%s' % manager - password = 'TESTS_%s_PASS' % manager - if not (enabled in os.environ and password in os.environ): - return unittest.skip(f"Skipping: {name} tests disabled.") + if name not in {'bitwarden', 'lastpass', 'onepassword'}: return _id - else: - return unittest.skip( - f"Skipping: {name} tests disabled. No dependencies") + manager = name.upper() + enabled = 'T_%s' % manager + password = 'TESTS_%s_PASS' % manager + if not (enabled in os.environ and password in os.environ): + return unittest.skip(f"Skipping: {name} tests disabled.") + return _id + + +def skipIfNoInstalled(name: str): + """Skip a test if a program is not installed.""" + if shutil.which(name) is None: + return unittest.skip(f"Skipping: {name} not installed disabled.") + return _id + + +def skipIfNoModule(name: str): + """Skip a test if an optional module is not installed.""" + try: + __import__(name) + except ImportError: + return unittest.skip(f"Skipping: module {name} not installed.") + return _id def mocked(manager, cmd): diff --git a/tests/exports/test_keepass.py b/tests/exports/test_keepass.py index 72112ae..45103ff 100644 --- a/tests/exports/test_keepass.py +++ b/tests/exports/test_keepass.py @@ -32,6 +32,7 @@ def test_keepass_isvalid(self): self.assertTrue(self.keepass.isvalid()) +@tests.skipIfNoModule('pykeepass') class TestExportKeepassInsert(tests.Test): """Test keepass insert features.""" keep = { diff --git a/tests/exports/test_lastpass.py b/tests/exports/test_lastpass.py index 749dbc6..7009d06 100644 --- a/tests/exports/test_lastpass.py +++ b/tests/exports/test_lastpass.py @@ -8,6 +8,7 @@ from pass_import.managers.lastpass import LastpassCLI +@tests.skipIfNoInstalled('lpass') class TestExportLastpass(tests.Test): """Test for Lastpass.""" diff --git a/tests/exports/test_sphinx.py b/tests/exports/test_sphinx.py index a22c7ed..d4bc277 100644 --- a/tests/exports/test_sphinx.py +++ b/tests/exports/test_sphinx.py @@ -12,10 +12,8 @@ try: from pwdsphinx import sphinx as pwdsphinx from pwdsphinx.sphinx import RULE_SIZE - PWDSPHINX = True except ImportError: RULE_SIZE = 79 - PWDSPHINX = False class MockCreateSock: @@ -69,7 +67,7 @@ def connect(): return MockCreateSock() -@tests.skipIfNo('pwdsphinx', PWDSPHINX) +@tests.skipIfNoModule('pwdsphinx') class TestExportSphinx(tests.Test): """Test sphinx general features.""" @@ -91,7 +89,7 @@ def test_sphinx_isvalid(self): self.assertTrue(self.sphinx.isvalid()) -@tests.skipIfNo('pwdsphinx', PWDSPHINX) +@tests.skipIfNoModule('pwdsphinx') class TestExportSphinxInsert(tests.Test): """Test Sphinx insert features.""" diff --git a/tests/imports/test_parse.py b/tests/imports/test_parse.py index 60ac09c..7802ce9 100644 --- a/tests/imports/test_parse.py +++ b/tests/imports/test_parse.py @@ -89,6 +89,7 @@ def test_import_encryptr(self): importer.parse() self.assertImport(importer.data, REFERENCE_CARD, keep) + @tests.skipIfNoModule('secretstorage') def test_import_gnome_keyring(self): """Testing: parse method for Gnome Keyring.""" collection = 'pass-import' @@ -132,6 +133,7 @@ def test_import_keepass_other(self): importer.parse() self.assertImport(importer.data, REFERENCE_OTHER) + @tests.skipIfNoInstalled('lpass') @patch('pass_import.managers.LastpassCLI._command') @patch('pass_import.managers.LastpassCLI._call') @patch("getpass.getpass")