Skip to content

Commit

Permalink
conftest: Don't manually clean up TemporaryDirectory's
Browse files Browse the repository at this point in the history
  • Loading branch information
maringuu committed Jan 24, 2023
1 parent 6b0a9b2 commit e7b6de6
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import grp
import logging
import os
import shutil
import tempfile
from configparser import ConfigParser
from pathlib import Path
from tempfile import TemporaryDirectory

import pytest

Expand All @@ -17,16 +15,27 @@
from test.conftest import merge_markers


def _create_docker_mount_base_dir() -> str:
dir = tempfile.mkdtemp(prefix='fact-docker-mount-base-dir')
@pytest.fixture
def _docker_mount_base_dir() -> str:
docker_gid = grp.getgrnam('docker').gr_gid
os.chown(dir, -1, docker_gid)
os.chmod(dir, 0o770)

return dir
with TemporaryDirectory(prefix='fact-docker-mount-base-dir') as tmp_dir:
os.chown(tmp_dir, -1, docker_gid)
os.chmod(tmp_dir, 0o770)
yield tmp_dir


@pytest.fixture
def _firmware_file_storage_directory() -> str:
with TemporaryDirectory(prefix='fact-firmware-file-storage-directory') as tmp_dir:
yield tmp_dir


def _get_test_config_tuple(defaults: dict | None = None) -> tuple[Config, ConfigParser]:
def _get_test_config_tuple(
firmware_file_storage_directory,
docker_mount_base_dir,
defaults: dict | None = None,
) -> tuple[Config, ConfigParser]:
"""Returns a tuple containing a `config.Config` instance and a `ConfigParser` instance.
Both instances are equivalent and the latter is legacy only.
The "docker-mount-base-dir" and "firmware-file-storage-directory" in the section "data-storage"
Expand All @@ -36,8 +45,10 @@ def _get_test_config_tuple(defaults: dict | None = None) -> tuple[Config, Config
"""
config.load()

docker_mount_base_dir = _create_docker_mount_base_dir()
firmware_file_storage_directory = Path(tempfile.mkdtemp())
if 'docker-mount-base-dir' in defaults:
raise ValueError('docker-mount-base-dir may not be changed with `@pytest.marker.cfg_defaults`')
if 'firmware-file-storage-directory' in defaults:
raise ValueError('firmware-file-storage-directory may not be changed with `@pytest.marker.cfg_defaults`')

# This dict must exactly match the one that a ConfigParser instance would
# read from the config file
Expand All @@ -59,7 +70,7 @@ def _get_test_config_tuple(defaults: dict | None = None) -> tuple[Config, Config
'redis-test-db': config.cfg.data_storage.redis_test_db, # Note: This is unused in production
'redis-host': config.cfg.data_storage.redis_host,
'redis-port': config.cfg.data_storage.redis_port,
'firmware-file-storage-directory': str(firmware_file_storage_directory),
'firmware-file-storage-directory': firmware_file_storage_directory,
'user-database': 'sqlite:////media/data/fact_auth_data/fact_users.db',
'password-salt': '1234',
'structural-threshold': '40', # TODO
Expand Down Expand Up @@ -113,22 +124,20 @@ def _get_test_config_tuple(defaults: dict | None = None) -> tuple[Config, Config

# FIXME When configparser is not used anymore this should not be named cfg_tuple but rather cfg
@pytest.fixture
def cfg_tuple(request):
def cfg_tuple(request, _firmware_file_storage_directory, _docker_mount_base_dir):
"""Returns a ``config.Config`` and a ``configparser.ConfigParser`` with testing defaults.
Defaults can be overwritten with the ``cfg_defaults`` pytest mark.
"""

cfg_defaults = merge_markers(request, 'cfg_defaults', dict)

cfg, configparser_cfg = _get_test_config_tuple(cfg_defaults)
cfg, configparser_cfg = _get_test_config_tuple(
_firmware_file_storage_directory,
_docker_mount_base_dir,
cfg_defaults,
)
yield cfg, configparser_cfg

# Don't clean up directorys we didn't create ourselves
if not cfg_defaults.get('data-storage', {}).get('docker-mount-base-dir', None):
shutil.rmtree(cfg.data_storage.docker_mount_base_dir)
if not cfg_defaults.get('data-storage', {}).get('firmware-file-storage-directory', None):
shutil.rmtree(cfg.data_storage.firmware_file_storage_directory)


@pytest.fixture(autouse=True)
def patch_cfg(cfg_tuple):
Expand Down

0 comments on commit e7b6de6

Please sign in to comment.