-
Notifications
You must be signed in to change notification settings - Fork 59
/
conftest.py
158 lines (119 loc) · 3.26 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
148
149
150
151
152
153
154
155
156
157
158
import os
import sys
import platform
import shutil
import pytest
collect_ignore = []
if platform.system() != 'Windows':
collect_ignore.extend(
[
'distutils/msvc9compiler.py',
]
)
@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
def distutils_logging_silencer(request):
from distutils import log
self = request.instance
self.threshold = log.set_threshold(log.FATAL)
# catching warnings
# when log will be replaced by logging
# we won't need such monkey-patch anymore
self._old_log = log.Log._log
log.Log._log = self._log
self.logs = []
try:
yield
finally:
log.set_threshold(self.threshold)
log.Log._log = self._old_log
@pytest.fixture
def distutils_managed_tempdir(request):
from distutils.tests import py38compat as os_helper
self = request.instance
self.old_cwd = os.getcwd()
self.tempdirs = []
try:
yield
finally:
# Restore working dir, for Solaris and derivatives, where rmdir()
# on the current directory fails.
os.chdir(self.old_cwd)
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():
orig = os.getcwd()
try:
yield
finally:
os.chdir(orig)
@pytest.fixture
def threshold_warn():
from distutils.log import set_threshold, WARN
orig = set_threshold(WARN)
yield
set_threshold(orig)
@pytest.fixture
def pypirc(request, save_env, distutils_managed_tempdir):
from distutils.core import PyPIRCCommand
from distutils.core import Distribution
self = request.instance
self.tmp_dir = self.mkdtemp()
os.environ['HOME'] = self.tmp_dir
os.environ['USERPROFILE'] = self.tmp_dir
self.rc = os.path.join(self.tmp_dir, '.pypirc')
self.dist = Distribution()
class command(PyPIRCCommand):
def __init__(self, dist):
super().__init__(dist)
def initialize_options(self):
pass
finalize_options = initialize_options
self._cmd = command
@pytest.fixture
def cleanup_testfn():
from distutils.tests import py38compat as os_helper
yield
path = os_helper.TESTFN
if os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path)
# 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)
)