Project

General

Profile

Download (2.37 KB) Statistics
| Branch: | Tag: | Revision:

hydrilla-builder / conftest.py @ 496d90f7

1
# SPDX-License-Identifier: CC0-1.0
2

    
3
# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
4
#
5
# Available under the terms of Creative Commons Zero v1.0 Universal.
6

    
7
import sys
8
from pathlib import Path
9

    
10
import pytest
11
import pkgutil
12
from tempfile import TemporaryDirectory
13
from typing import Iterable
14

    
15
here = Path(__file__).resolve().parent
16
sys.path.insert(0, str(here / 'src'))
17

    
18
@pytest.fixture(autouse=True)
19
def no_requests(monkeypatch):
20
    """Remove requests.sessions.Session.request for all tests."""
21
    monkeypatch.delattr('requests.sessions.Session.request')
22

    
23
@pytest.fixture
24
def mock_subprocess_run(monkeypatch, request):
25
    """
26
    Temporarily replace subprocess.run() with a function supplied through pytest
27
    marker 'subprocess_run'.
28

    
29
    The marker excepts 2 arguments:
30
    * the module inside which the subprocess attribute should be mocked and
31
    * a run() function to use.
32
    """
33
    where, mocked_run = request.node.get_closest_marker('subprocess_run').args
34

    
35
    class MockedSubprocess:
36
        """Minimal mocked version of the subprocess module."""
37
        run = mocked_run
38

    
39
    monkeypatch.setattr(where, 'subprocess', MockedSubprocess)
40

    
41
@pytest.fixture(autouse=True)
42
def no_gettext(monkeypatch, request):
43
    """
44
    Make gettext return all strings untranslated unless we request otherwise.
45
    """
46
    if request.node.get_closest_marker('enable_gettext'):
47
        return
48

    
49
    import hydrilla
50
    modules_to_process = [hydrilla]
51

    
52
    def add_child_modules(parent):
53
        """
54
        Recursuvely collect all modules descending from 'parent' into an array.
55
        """
56
        try:
57
            load_paths = parent.__path__
58
        except AttributeError:
59
            return
60

    
61
        for module_info in pkgutil.iter_modules(load_paths):
62
            if module_info.name != '__main__':
63
                __import__(f'{parent.__name__}.{module_info.name}')
64
                modules_to_process.append(getattr(parent, module_info.name))
65
                add_child_modules(getattr(parent, module_info.name))
66

    
67
    add_child_modules(hydrilla)
68

    
69
    for module in modules_to_process:
70
        if hasattr(module, '_'):
71
            monkeypatch.setattr(module, '_', lambda message: message)
72

    
73
@pytest.fixture
74
def tmpdir() -> Iterable[Path]:
75
    """
76
    Provide test case with a temporary directory that will be automatically
77
    deleted after the test.
78
    """
79
    with TemporaryDirectory() as tmpdir:
80
        yield Path(tmpdir)
(6-6/9)