Skip to content

Commit 6cc022d

Browse files
committed
test: ignore some test if deps are not met.
1 parent c7ff2ef commit 6cc022d

5 files changed

Lines changed: 32 additions & 16 deletions

File tree

tests/__init__.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
- tests.captured() Context manager to capture stdout.
3333
- tests.mocked() Mock cloud password managers API response.
3434
- tests.skipIfNo() Skip a password manager test if it is disabled.
35+
- tests.skipIfNoInstalled() Skip a test if a program is not installed.
36+
- tests.skipIfNoModule() Skip a test if an optional module is not installed.
3537
- tests.mock_hibp() Mock HIBP API response.
3638
"""
3739

@@ -61,20 +63,32 @@ def _id(obj):
6163
return obj
6264

6365

64-
def skipIfNo(name, imported=True):
66+
def skipIfNo(name: str):
6567
"""Skip a password manager test if it is disabled."""
66-
if imported:
67-
if name not in {'bitwarden', 'lastpass', 'onepassword'}:
68-
return _id
69-
manager = name.upper()
70-
enabled = 'T_%s' % manager
71-
password = 'TESTS_%s_PASS' % manager
72-
if not (enabled in os.environ and password in os.environ):
73-
return unittest.skip(f"Skipping: {name} tests disabled.")
68+
if name not in {'bitwarden', 'lastpass', 'onepassword'}:
7469
return _id
75-
else:
76-
return unittest.skip(
77-
f"Skipping: {name} tests disabled. No dependencies")
70+
manager = name.upper()
71+
enabled = 'T_%s' % manager
72+
password = 'TESTS_%s_PASS' % manager
73+
if not (enabled in os.environ and password in os.environ):
74+
return unittest.skip(f"Skipping: {name} tests disabled.")
75+
return _id
76+
77+
78+
def skipIfNoInstalled(name: str):
79+
"""Skip a test if a program is not installed."""
80+
if shutil.which(name) is None:
81+
return unittest.skip(f"Skipping: {name} not installed disabled.")
82+
return _id
83+
84+
85+
def skipIfNoModule(name: str):
86+
"""Skip a test if an optional module is not installed."""
87+
try:
88+
__import__(name)
89+
except ImportError:
90+
return unittest.skip(f"Skipping: module {name} not installed.")
91+
return _id
7892

7993

8094
def mocked(manager, cmd):

tests/exports/test_keepass.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_keepass_isvalid(self):
3232
self.assertTrue(self.keepass.isvalid())
3333

3434

35+
@tests.skipIfNoModule('pykeepass')
3536
class TestExportKeepassInsert(tests.Test):
3637
"""Test keepass insert features."""
3738
keep = {

tests/exports/test_lastpass.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pass_import.managers.lastpass import LastpassCLI
99

1010

11+
@tests.skipIfNoInstalled('lpass')
1112
class TestExportLastpass(tests.Test):
1213
"""Test for Lastpass."""
1314

tests/exports/test_sphinx.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
try:
1313
from pwdsphinx import sphinx as pwdsphinx
1414
from pwdsphinx.sphinx import RULE_SIZE
15-
PWDSPHINX = True
1615
except ImportError:
1716
RULE_SIZE = 79
18-
PWDSPHINX = False
1917

2018

2119
class MockCreateSock:
@@ -69,7 +67,7 @@ def connect():
6967
return MockCreateSock()
7068

7169

72-
@tests.skipIfNo('pwdsphinx', PWDSPHINX)
70+
@tests.skipIfNoModule('pwdsphinx')
7371
class TestExportSphinx(tests.Test):
7472
"""Test sphinx general features."""
7573

@@ -91,7 +89,7 @@ def test_sphinx_isvalid(self):
9189
self.assertTrue(self.sphinx.isvalid())
9290

9391

94-
@tests.skipIfNo('pwdsphinx', PWDSPHINX)
92+
@tests.skipIfNoModule('pwdsphinx')
9593
class TestExportSphinxInsert(tests.Test):
9694
"""Test Sphinx insert features."""
9795

tests/imports/test_parse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def test_import_encryptr(self):
8989
importer.parse()
9090
self.assertImport(importer.data, REFERENCE_CARD, keep)
9191

92+
@tests.skipIfNoModule('secretstorage')
9293
def test_import_gnome_keyring(self):
9394
"""Testing: parse method for Gnome Keyring."""
9495
collection = 'pass-import'
@@ -132,6 +133,7 @@ def test_import_keepass_other(self):
132133
importer.parse()
133134
self.assertImport(importer.data, REFERENCE_OTHER)
134135

136+
@tests.skipIfNoInstalled('lpass')
135137
@patch('pass_import.managers.LastpassCLI._command')
136138
@patch('pass_import.managers.LastpassCLI._call')
137139
@patch("getpass.getpass")

0 commit comments

Comments
 (0)