Skip to content

Commit

Permalink
test: ignore some test if deps are not met.
Browse files Browse the repository at this point in the history
  • Loading branch information
roddhjav committed Jan 24, 2024
1 parent e782613 commit 844270b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
38 changes: 26 additions & 12 deletions tests/__init__.py
Expand Up @@ -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.
"""

Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions tests/exports/test_keepass.py
Expand Up @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions tests/exports/test_lastpass.py
Expand Up @@ -8,6 +8,7 @@
from pass_import.managers.lastpass import LastpassCLI


@tests.skipIfNoInstalled('lpass')
class TestExportLastpass(tests.Test):
"""Test for Lastpass."""

Expand Down
6 changes: 2 additions & 4 deletions tests/exports/test_sphinx.py
Expand Up @@ -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:
Expand Down Expand Up @@ -69,7 +67,7 @@ def connect():
return MockCreateSock()


@tests.skipIfNo('pwdsphinx', PWDSPHINX)
@tests.skipIfNoModule('pwdsphinx')
class TestExportSphinx(tests.Test):
"""Test sphinx general features."""

Expand All @@ -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."""

Expand Down
2 changes: 2 additions & 0 deletions tests/imports/test_parse.py
Expand Up @@ -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'
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 844270b

Please sign in to comment.