summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichał Górny <mgorny@gentoo.org>2020-08-30 16:24:10 +0200
committerMichał Górny <mgorny@gentoo.org>2020-08-30 16:24:10 +0200
commit21f919b14a9aeaa08aecbcdd41a530198315aad1 (patch)
treea23c4663c499324378d6f486c3c8e35ae5ba034c
parentf6dcfd5c6fe1338b74d502284cd524549b6edde9 (diff)
downloadgemato-21f919b14a9aeaa08aecbcdd41a530198315aad1.tar.gz
tests: Use module-scope fixtures whenever possible
Signed-off-by: Michał Górny <mgorny@gentoo.org>
-rw-r--r--tests/test_find_top_level.py10
-rw-r--r--tests/test_manifest.py4
-rw-r--r--tests/test_openpgp.py22
-rw-r--r--tests/test_verify.py5
-rw-r--r--tests/testutil.py58
5 files changed, 58 insertions, 41 deletions
diff --git a/tests/test_find_top_level.py b/tests/test_find_top_level.py
index b5e6a15..184fe72 100644
--- a/tests/test_find_top_level.py
+++ b/tests/test_find_top_level.py
@@ -12,8 +12,9 @@ import pytest
from gemato.find_top_level import find_top_level_manifest
-@pytest.fixture
-def plain_tree(tmp_path):
+@pytest.fixture(scope='module')
+def plain_tree(tmp_path_factory):
+ tmp_path = tmp_path_factory.mktemp('find-top-level-plain-')
for d in ('empty-subdir',
'manifest-subdir',
'deep/manifest-subdir',
@@ -84,8 +85,9 @@ def test_cross_device(tmp_path):
assert find_top_level_manifest(tmp_path / 'test') is None
-@pytest.fixture
-def compressed_manifest_tree(tmp_path):
+@pytest.fixture(scope='module')
+def compressed_manifest_tree(tmp_path_factory):
+ tmp_path = tmp_path_factory.mktemp('find-top-level-compressed-')
for d in ('empty-subdir',
'manifest-subdir',
'deep/manifest-subdir',
diff --git a/tests/test_manifest.py b/tests/test_manifest.py
index 1b154a3..7bed4d5 100644
--- a/tests/test_manifest.py
+++ b/tests/test_manifest.py
@@ -99,7 +99,7 @@ def test_find_timestamp(manifest_var, expected):
assert m.find_timestamp().ts == expected
-@pytest.fixture
+@pytest.fixture(scope='module')
def test_manifest():
m = ManifestFile()
with io.StringIO(TEST_MANIFEST) as f:
@@ -107,7 +107,7 @@ def test_manifest():
yield m
-@pytest.fixture
+@pytest.fixture(scope='module')
def deprecated_manifest():
m = ManifestFile()
with io.StringIO(TEST_DEPRECATED_MANIFEST) as f:
diff --git a/tests/test_openpgp.py b/tests/test_openpgp.py
index ce62add..995c9da 100644
--- a/tests/test_openpgp.py
+++ b/tests/test_openpgp.py
@@ -37,11 +37,7 @@ from tests.keydata import (
OTHER_PUBLIC_KEY, OTHER_PUBLIC_KEY_UID, OTHER_PUBLIC_KEY_SIG,
UNEXPIRE_SIG,
)
-from tests.testutil import hkp_server
-
-
-# workaround pyflakes
-hkp_server = hkp_server
+from tests.testutil import HKPServer
VALID_PUBLIC_KEY = PUBLIC_KEY + UID + PUBLIC_KEY_SIG
@@ -638,6 +634,22 @@ def test_recursive_manifest_loader_save_submanifest(tmp_path, privkey_env):
assert m2.openpgp_signature is None
+@pytest.fixture(scope='module')
+def global_hkp_server():
+ """A fixture that starts a single HKP server instance for tests"""
+ server = HKPServer()
+ server.start()
+ yield server
+ server.stop()
+
+
+@pytest.fixture
+def hkp_server(global_hkp_server):
+ """A fixture that resets the global HKP server with empty keys"""
+ global_hkp_server.keys.clear()
+ yield global_hkp_server
+
+
COMBINED_PUBLIC_KEYS = OTHER_VALID_PUBLIC_KEY + VALID_PUBLIC_KEY
diff --git a/tests/test_verify.py b/tests/test_verify.py
index c613706..79c5513 100644
--- a/tests/test_verify.py
+++ b/tests/test_verify.py
@@ -28,9 +28,10 @@ from gemato.verify import (
TEST_STRING = b'The quick brown fox jumps over the lazy dog'
-@pytest.fixture
-def test_tree(tmp_path):
+@pytest.fixture(scope='module')
+def test_tree(tmp_path_factory):
"""Test tree with different file types needed for tests"""
+ tmp_path = tmp_path_factory.mktemp('verify-')
with open(tmp_path / 'empty-file', 'w'):
pass
with open(tmp_path / 'regular-file', 'wb') as f:
diff --git a/tests/testutil.py b/tests/testutil.py
index c00ef75..092a4d2 100644
--- a/tests/testutil.py
+++ b/tests/testutil.py
@@ -3,7 +3,6 @@
# (c) 2017-2020 Michał Górny
# Licensed under the terms of 2-clause BSD license
-import collections
import errno
import functools
import io
@@ -88,31 +87,34 @@ class HKPServerRequestHandler(BaseHTTPRequestHandler):
self.wfile.flush()
-@pytest.fixture
-def hkp_server():
- keys = {}
- # try 10 randomly selected ports before giving up
- for port in random.sample(range(1024, 32768), 10):
- try:
- server = HTTPServer(
- ('127.0.0.1', port),
- functools.partial(HKPServerRequestHandler, keys))
- except OSError as e:
- if e.errno != errno.EADDRINUSE:
- raise unittest.SkipTest('Unable to bind the HKP server: {}'
- .format(e))
+class HKPServer:
+ def __init__(self):
+ self.keys = {}
+ self.addr = None
+
+ def start(self):
+ # try 10 randomly selected ports before giving up
+ for port in random.sample(range(1024, 32768), 10):
+ try:
+ self.server = HTTPServer(
+ ('127.0.0.1', port),
+ functools.partial(HKPServerRequestHandler, self.keys))
+ except OSError as e:
+ if e.errno != errno.EADDRINUSE:
+ pytest.skip(
+ f'Unable to bind the HKP server: {e}')
+ else:
+ break
else:
- break
- else:
- pytest.skip('Unable to find a free port for HKP server')
-
- server_addr = f'hkp://127.0.0.1:{port}'
- server_thread = threading.Thread(target=server.serve_forever)
- server_thread.start()
-
- yield collections.namedtuple('HKPServerTuple', ('addr', 'keys'))(
- server_addr, keys)
-
- server.shutdown()
- server.server_close()
- server_thread.join()
+ pytest.skip('Unable to find a free port for HKP server')
+
+ self.addr = f'hkp://127.0.0.1:{port}'
+ self.thread = threading.Thread(target=self.server.serve_forever)
+ self.thread.start()
+
+ def stop(self):
+ assert self.addr is not None
+ self.server.shutdown()
+ self.server.server_close()
+ self.thread.join()
+ self.addr = None