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
|
|
13
|
here = Path(__file__).resolve().parent
|
14
|
sys.path.insert(0, str(here / 'src'))
|
15
|
|
16
|
@pytest.fixture(autouse=True)
|
17
|
def no_requests(monkeypatch):
|
18
|
"""Remove requests.sessions.Session.request for all tests."""
|
19
|
monkeypatch.delattr('requests.sessions.Session.request')
|
20
|
|
21
|
@pytest.fixture
|
22
|
def mock_subprocess_run(monkeypatch, request):
|
23
|
"""
|
24
|
Temporarily replace subprocess.run() with a function supplied through pytest
|
25
|
marker 'subprocess_run'.
|
26
|
|
27
|
The marker excepts 2 arguments:
|
28
|
* the module inside which the subprocess attribute should be mocked and
|
29
|
* a run() function to use.
|
30
|
"""
|
31
|
where, mocked_run = request.node.get_closest_marker('subprocess_run').args
|
32
|
|
33
|
class MockedSubprocess:
|
34
|
"""Minimal mocked version of the subprocess module."""
|
35
|
run = mocked_run
|
36
|
|
37
|
monkeypatch.setattr(where, 'subprocess', MockedSubprocess)
|
38
|
|
39
|
@pytest.fixture(autouse=True)
|
40
|
def no_gettext(monkeypatch, request):
|
41
|
"""
|
42
|
Make gettext return all strings untranslated unless we request otherwise.
|
43
|
"""
|
44
|
if request.node.get_closest_marker('enable_gettext'):
|
45
|
return
|
46
|
|
47
|
import hydrilla
|
48
|
modules_to_process = [hydrilla]
|
49
|
|
50
|
def add_child_modules(parent):
|
51
|
"""
|
52
|
Recursuvely collect all modules descending from 'parent' into an array.
|
53
|
"""
|
54
|
try:
|
55
|
load_paths = parent.__path__
|
56
|
except AttributeError:
|
57
|
return
|
58
|
|
59
|
for module_info in pkgutil.iter_modules(load_paths):
|
60
|
if module_info.name != '__main__':
|
61
|
__import__(f'{parent.__name__}.{module_info.name}')
|
62
|
modules_to_process.append(getattr(parent, module_info.name))
|
63
|
add_child_modules(getattr(parent, module_info.name))
|
64
|
|
65
|
add_child_modules(hydrilla)
|
66
|
|
67
|
for module in modules_to_process:
|
68
|
if hasattr(module, '_'):
|
69
|
monkeypatch.setattr(module, '_', lambda message: message)
|