-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftest.py
103 lines (82 loc) · 2.78 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""configures pytest"""
import os
import sys
from pathlib import Path
from typing import Generator
from unittest.mock import patch
import appdirs
import pytest
from _pytest.config import Config
from pytest_cov.plugin import CovPlugin
from pytest_mock import MockerFixture
from testlibs.mockpi import MockPI
pytest_plugins = ["testlibs.markdown"]
@pytest.mark.tryfirst
def pytest_configure(config: Config) -> None:
"""Setup default pytest options."""
config.option.newfirst = True
config.option.failedfirst = True
config.option.tbstyle = "short"
config.option.durations = 0
config.option.durations_min = 1
config.option.pylint = True
config.option.black = True
config.option.isort = True
config.option.markdown = True
config.option.mypy = True
config.option.mypy_ignore_missing_imports = True
config.pluginmanager.getplugin("mypy").mypy_argv.extend(
["--strict", "--implicit-reexport"]
)
config.option.mccabe = True
config.addinivalue_line("mccabe-complexity", "3")
config.option.cov_source = ["scriptenv", "release"]
config.option.cov_fail_under = 100
config.option.cov_report = {
"term-missing": None,
"html": "cov_html",
}
config.option.cov_branch = True
config.pluginmanager.register(
CovPlugin(config.option, config.pluginmanager), "_cov"
)
@pytest.fixture(autouse=True)
def set_cache_path(tmp_path: Path) -> None:
"""Patches the default cache dir for scriptenv"""
os.environ["SCRIPTENV_CACHE_PATH"] = str(tmp_path / "temp_cache_path")
@pytest.fixture(autouse=True)
def patch_appdirs(tmp_path: Path) -> Generator[None, None, None]:
"""Patches appdirs to use a temporary directory"""
with patch.object(
appdirs, "user_cache_dir", return_value=tmp_path / "appdirs" / "user_cache_dir"
):
yield
@pytest.fixture(autouse=True)
def save_and_restore_sys_path(mocker: MockerFixture) -> None:
"""Saves and restores sys.path."""
mocker.patch("sys.path", list(sys.path))
@pytest.fixture(autouse=True)
def save_and_restore_os_environ() -> Generator[None, None, None]:
"""Saves and restores os.environ."""
backup = dict(os.environ)
try:
yield
finally:
os.environ.clear()
os.environ.update(backup)
@pytest.fixture(autouse=True)
def restore_sys_modules() -> Generator[None, None, None]:
"""Restores sys.modules."""
backup = set(sys.modules.keys())
try:
yield
finally:
for name in list(sys.modules.keys()):
if name not in backup:
del sys.modules[name]
@pytest.fixture
def mockpi(tmp_path: Path) -> Generator[MockPI, None, None]:
"""Fixture to setup a Python Package Index"""
mock_pi = MockPI(tmp_path / "mockpi")
with mock_pi.server():
yield mock_pi