-
Notifications
You must be signed in to change notification settings - Fork 73
/
conftest.py
147 lines (106 loc) · 3.06 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import logging
import os
import pathlib
import platform
import sys
import path
import pytest
@pytest.fixture
def save_env():
orig = os.environ.copy()
try:
yield
finally:
for key in set(os.environ) - set(orig):
del os.environ[key]
for key, value in orig.items():
if os.environ.get(key) != value:
os.environ[key] = value
@pytest.fixture
def needs_zlib():
pytest.importorskip('zlib')
@pytest.fixture(autouse=True)
def log_everything():
"""
For tests, set the level on the logger to log everything.
"""
logging.getLogger('distutils').setLevel(0)
@pytest.fixture(autouse=True)
def capture_log_at_info(caplog):
"""
By default, capture logs at INFO and greater.
"""
caplog.set_level(logging.INFO)
def _save_cwd():
return path.Path('.')
@pytest.fixture
def distutils_managed_tempdir(request):
from distutils.tests.compat import py38 as os_helper
self = request.instance
self.tempdirs = []
try:
with _save_cwd():
yield
finally:
while self.tempdirs:
tmpdir = self.tempdirs.pop()
os_helper.rmtree(tmpdir)
@pytest.fixture
def save_argv():
orig = sys.argv[:]
try:
yield
finally:
sys.argv[:] = orig
@pytest.fixture
def save_cwd():
with _save_cwd():
yield
@pytest.fixture
def temp_cwd(tmp_path):
with path.Path(tmp_path):
yield
# from pytest-dev/pytest#363
@pytest.fixture(scope="session")
def monkeysession(request):
from _pytest.monkeypatch import MonkeyPatch
mpatch = MonkeyPatch()
yield mpatch
mpatch.undo()
@pytest.fixture(autouse=True, scope="session")
def suppress_path_mangle(monkeysession):
"""
Disable the path mangling in CCompiler. Workaround for #169.
"""
from distutils import ccompiler
monkeysession.setattr(
ccompiler.CCompiler, '_make_relative', staticmethod(lambda x: x)
)
def _set_home(monkeypatch, path):
var = 'USERPROFILE' if platform.system() == 'Windows' else 'HOME'
monkeypatch.setenv(var, str(path))
return path
@pytest.fixture
def temp_home(tmp_path, monkeypatch):
return _set_home(monkeypatch, tmp_path)
@pytest.fixture
def fake_home(fs, monkeypatch):
home = fs.create_dir('/fakehome')
return _set_home(monkeypatch, pathlib.Path(home.path))
@pytest.fixture
def disable_macos_customization(monkeypatch):
from distutils import sysconfig
monkeypatch.setattr(sysconfig, '_customize_macos', lambda: None)
@pytest.fixture(autouse=True, scope="session")
def monkey_patch_get_default_compiler(monkeysession):
"""
Monkey patch distutils get_default_compiler to allow overriding the
default compiler. Mainly to test mingw32 with a MSVC Python.
"""
from distutils import ccompiler
default_compiler = os.environ.get("DISTUTILS_TEST_DEFAULT_COMPILER")
if default_compiler is None:
return
def patched_getter(*args, **kwargs):
return default_compiler
monkeysession.setattr(ccompiler, 'get_default_compiler', patched_getter)