1 |
9dda3aa9
|
Wojtek Kosior
|
# 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 |
61f0aa75
|
Wojtek Kosior
|
import pytest
|
11 |
bd588eb9
|
Wojtek Kosior
|
import pkgutil
|
12 |
2fc043b3
|
Wojtek Kosior
|
import importlib
|
13 |
496d90f7
|
Wojtek Kosior
|
from tempfile import TemporaryDirectory
|
14 |
|
|
from typing import Iterable
|
15 |
61f0aa75
|
Wojtek Kosior
|
|
16 |
9dda3aa9
|
Wojtek Kosior
|
here = Path(__file__).resolve().parent
|
17 |
|
|
sys.path.insert(0, str(here / 'src'))
|
18 |
61f0aa75
|
Wojtek Kosior
|
|
19 |
|
|
@pytest.fixture(autouse=True)
|
20 |
|
|
def no_requests(monkeypatch):
|
21 |
|
|
"""Remove requests.sessions.Session.request for all tests."""
|
22 |
2fc043b3
|
Wojtek Kosior
|
if importlib.util.find_spec("requests") is not None:
|
23 |
|
|
monkeypatch.delattr('requests.sessions.Session.request')
|
24 |
61f0aa75
|
Wojtek Kosior
|
|
25 |
|
|
@pytest.fixture
|
26 |
|
|
def mock_subprocess_run(monkeypatch, request):
|
27 |
|
|
"""
|
28 |
|
|
Temporarily replace subprocess.run() with a function supplied through pytest
|
29 |
|
|
marker 'subprocess_run'.
|
30 |
|
|
|
31 |
|
|
The marker excepts 2 arguments:
|
32 |
|
|
* the module inside which the subprocess attribute should be mocked and
|
33 |
|
|
* a run() function to use.
|
34 |
|
|
"""
|
35 |
|
|
where, mocked_run = request.node.get_closest_marker('subprocess_run').args
|
36 |
|
|
|
37 |
|
|
class MockedSubprocess:
|
38 |
|
|
"""Minimal mocked version of the subprocess module."""
|
39 |
|
|
run = mocked_run
|
40 |
|
|
|
41 |
|
|
monkeypatch.setattr(where, 'subprocess', MockedSubprocess)
|
42 |
bd588eb9
|
Wojtek Kosior
|
|
43 |
|
|
@pytest.fixture(autouse=True)
|
44 |
|
|
def no_gettext(monkeypatch, request):
|
45 |
|
|
"""
|
46 |
|
|
Make gettext return all strings untranslated unless we request otherwise.
|
47 |
|
|
"""
|
48 |
|
|
if request.node.get_closest_marker('enable_gettext'):
|
49 |
|
|
return
|
50 |
|
|
|
51 |
|
|
import hydrilla
|
52 |
|
|
modules_to_process = [hydrilla]
|
53 |
|
|
|
54 |
|
|
def add_child_modules(parent):
|
55 |
|
|
"""
|
56 |
|
|
Recursuvely collect all modules descending from 'parent' into an array.
|
57 |
|
|
"""
|
58 |
|
|
try:
|
59 |
|
|
load_paths = parent.__path__
|
60 |
|
|
except AttributeError:
|
61 |
|
|
return
|
62 |
|
|
|
63 |
|
|
for module_info in pkgutil.iter_modules(load_paths):
|
64 |
|
|
if module_info.name != '__main__':
|
65 |
|
|
__import__(f'{parent.__name__}.{module_info.name}')
|
66 |
|
|
modules_to_process.append(getattr(parent, module_info.name))
|
67 |
|
|
add_child_modules(getattr(parent, module_info.name))
|
68 |
|
|
|
69 |
|
|
add_child_modules(hydrilla)
|
70 |
|
|
|
71 |
|
|
for module in modules_to_process:
|
72 |
|
|
if hasattr(module, '_'):
|
73 |
|
|
monkeypatch.setattr(module, '_', lambda message: message)
|
74 |
496d90f7
|
Wojtek Kosior
|
|
75 |
|
|
@pytest.fixture
|
76 |
|
|
def tmpdir() -> Iterable[Path]:
|
77 |
|
|
"""
|
78 |
|
|
Provide test case with a temporary directory that will be automatically
|
79 |
|
|
deleted after the test.
|
80 |
|
|
"""
|
81 |
|
|
with TemporaryDirectory() as tmpdir:
|
82 |
|
|
yield Path(tmpdir)
|