diff options
author | Michał Górny <mgorny@gentoo.org> | 2020-09-05 13:13:02 +0200 |
---|---|---|
committer | Michał Górny <mgorny@gentoo.org> | 2020-09-05 13:13:02 +0200 |
commit | 49ff2ca317dcf62f9c085f12aedc820149495d71 (patch) | |
tree | d9279db56ef339a210d78c5c9f70d1458ae1f22a /tests/test_recursiveloader.py | |
parent | f576c0f594b016efd34e89d792257d56988ca7b7 (diff) | |
download | gemato-49ff2ca317dcf62f9c085f12aedc820149495d71.tar.gz |
test_recursiveloader: Cache tmp_paths for read-only layouts
Signed-off-by: Michał Górny <mgorny@gentoo.org>
Diffstat (limited to 'tests/test_recursiveloader.py')
-rw-r--r-- | tests/test_recursiveloader.py | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/tests/test_recursiveloader.py b/tests/test_recursiveloader.py index 42b2821..3065e08 100644 --- a/tests/test_recursiveloader.py +++ b/tests/test_recursiveloader.py @@ -8,6 +8,7 @@ import datetime import gzip import itertools import os +import re import pytest @@ -27,28 +28,51 @@ from tests.test_compression import COMPRESSION_ALGOS from tests.testutil import disallow_writes +@pytest.fixture(scope='module') +def layout_cache(): + cache = {} + yield cache + for layout, tmp_path in cache.items(): + layout.cleanup(tmp_path) + + class LayoutFactory: """Factory to install layouts in temporary directory with cleanup""" - def __init__(self, tmp_path): - self.tmp_path = tmp_path + def __init__(self, tmp_path_factory, name, layout_cache): + self.tmp_path_factory = tmp_path_factory + self.name = name + self.layout_cache = layout_cache self.layouts = [] def create(self, layout, readonly=False): - layout.create(self.tmp_path) - self.layouts.append(layout) if readonly: - disallow_writes(self.tmp_path) - return self.tmp_path + if layout not in self.layout_cache: + tmp_path = self.tmp_path_factory.mktemp(layout.__name__) + layout.create(tmp_path) + disallow_writes(tmp_path) + self.layout_cache[layout] = tmp_path + return self.layout_cache[layout] + + tmp_path = self.tmp_path_factory.mktemp(self.name) + layout.create(tmp_path) + self.layouts.append((layout, tmp_path)) + return tmp_path def cleanup(self): - for layout in self.layouts: - layout.cleanup(self.tmp_path) + for layout, tmp_path in self.layouts: + layout.cleanup(tmp_path) @pytest.fixture -def layout_factory(tmp_path): - factory = LayoutFactory(tmp_path) +def layout_factory(request, tmp_path_factory, layout_cache): + # stolen from pytest + name = request.node.name + name = re.sub(r"[\W]", "_", name) + MAXVAL = 30 + name = name[:MAXVAL] + + factory = LayoutFactory(tmp_path_factory, name, layout_cache) yield factory factory.cleanup() |