| 1 |
import py, pytest, sys, os, textwrap |
| 2 |
from inspect import isclass |
| 3 |
|
| 4 |
LOOK_FOR_PYTHON3 = 'python3.11' |
| 5 |
if sys.platform == 'win32': |
| 6 |
PYTHON3 = None |
| 7 |
else: |
| 8 |
PYTHON3 = os.getenv('PYTHON3') or py.path.local.sysfind(LOOK_FOR_PYTHON3) |
| 9 |
if PYTHON3 is not None: |
| 10 |
PYTHON3 = str(PYTHON3) |
| 11 |
HOST_IS_PY3 = sys.version_info[0] > 2 |
| 12 |
APPLEVEL_FN = 'apptest_*.py' |
| 13 |
|
| 14 |
|
| 15 |
rsyncdirs = ['.', '../lib-python', '../lib_pypy', '../demo'] |
| 16 |
rsyncignore = ['_cache'] |
| 17 |
|
| 18 |
try: |
| 19 |
from hypothesis import settings, __version__ |
| 20 |
except ImportError: |
| 21 |
pass |
| 22 |
else: |
| 23 |
try: |
| 24 |
settings.register_profile('default', deadline=None) |
| 25 |
except Exception: |
| 26 |
import warnings |
| 27 |
warnings.warn("Version of hypothesis too old, " |
| 28 |
"cannot set the deadline to None") |
| 29 |
settings.load_profile('default') |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
option = None |
| 35 |
|
| 36 |
def braindead_deindent(self): |
| 37 |
"""monkeypatch that wont end up doing stupid in the python tokenizer""" |
| 38 |
text = '\n'.join(self.lines) |
| 39 |
short = py.std.textwrap.dedent(text) |
| 40 |
newsource = py.code.Source() |
| 41 |
newsource.lines[:] = short.splitlines() |
| 42 |
return newsource |
| 43 |
|
| 44 |
try: |
| 45 |
py.code.Source.deindent = braindead_deindent |
| 46 |
except AttributeError: |
| 47 |
|
| 48 |
pass |
| 49 |
|
| 50 |
def get_marker(item, name): |
| 51 |
try: |
| 52 |
return item.get_closest_marker(name=name) |
| 53 |
except AttributeError: |
| 54 |
|
| 55 |
return item.get_marker(name=name) |
| 56 |
|
| 57 |
def pytest_report_header(): |
| 58 |
return "pytest-%s from %s" % (pytest.__version__, pytest.__file__) |
| 59 |
|
| 60 |
if pytest.__version__[0] > '6': |
| 61 |
@pytest.hookimpl(tryfirst=True) |
| 62 |
def pytest_load_initial_conftests(args): |
| 63 |
if not (set(args) & {'-D', '--direct-apptest'}): |
| 64 |
args.append('--assert=reinterp') |
| 65 |
else: |
| 66 |
@pytest.hookimpl(tryfirst=True) |
| 67 |
def pytest_cmdline_preparse(config, args): |
| 68 |
if not (set(args) & {'-D', '--direct-apptest'}): |
| 69 |
args.append('--assert=reinterp') |
| 70 |
|
| 71 |
def pytest_configure(config): |
| 72 |
if HOST_IS_PY3 and not config.getoption('direct_apptest'): |
| 73 |
raise ValueError( |
| 74 |
"On top of a Python 3 interpreter, the -D flag is mandatory") |
| 75 |
global option |
| 76 |
option = config.option |
| 77 |
mode_A = config.getoption('runappdirect') |
| 78 |
mode_D = config.getoption('direct_apptest') |
| 79 |
def py3k_skip(message): |
| 80 |
pytest.skip('[py3k] %s' % message) |
| 81 |
pytest.py3k_skip = py3k_skip |
| 82 |
if mode_D or not mode_A: |
| 83 |
config.addinivalue_line('python_files', APPLEVEL_FN) |
| 84 |
if not mode_A and not mode_D: |
| 85 |
from rpython.conftest import LeakFinder |
| 86 |
config.pluginmanager.register(LeakFinder()) |
| 87 |
|
| 88 |
|
| 89 |
sys.setrecursionlimit(2000) |
| 90 |
if mode_A: |
| 91 |
from pypy.tool.pytest.apptest import PythonInterpreter |
| 92 |
config.applevel = PythonInterpreter(config.option.python) |
| 93 |
elif mode_D: |
| 94 |
class Applevel(): |
| 95 |
is_pypy = '__pypy__' in sys.builtin_module_names |
| 96 |
config.applevel = Applevel() |
| 97 |
else: |
| 98 |
config.applevel = None |
| 99 |
config.addinivalue_line( |
| 100 |
"markers", "pypy_only: only run test on PyPy" |
| 101 |
) |
| 102 |
config.addinivalue_line("markers", "flaky: test that sometimes fails") |
| 103 |
|
| 104 |
|
| 105 |
def pytest_addoption(parser): |
| 106 |
group = parser.getgroup("pypy options") |
| 107 |
group.addoption('-A', '--runappdirect', action="store_true", |
| 108 |
default=False, dest="runappdirect", |
| 109 |
help="run legacy applevel tests directly on the python interpreter " + |
| 110 |
"specified by --python") |
| 111 |
group.addoption('--python', type=str, default=PYTHON3, |
| 112 |
help="python interpreter to run appdirect tests with") |
| 113 |
group.addoption('-D', '--direct-apptest', action="store_true", |
| 114 |
default=False, dest="direct_apptest", |
| 115 |
help="run '%s' tests directly on host interpreter" % APPLEVEL_FN) |
| 116 |
group.addoption('--direct', action="store_true", |
| 117 |
default=False, dest="rundirect", |
| 118 |
help="run pexpect tests directly") |
| 119 |
group.addoption('--raise-operr', action="store_true", |
| 120 |
default=False, dest="raise_operr", |
| 121 |
help="Show the interp-level OperationError in app-level tests") |
| 122 |
group.addoption('--no-applevel-rewrite', action="store_false", |
| 123 |
default=True, dest="applevel_rewrite", |
| 124 |
help="Don't use assert rewriting in app-level test files") |
| 125 |
|
| 126 |
@pytest.fixture(scope='class') |
| 127 |
def spaceconfig(request): |
| 128 |
return getattr(request.cls, 'spaceconfig', {}) |
| 129 |
|
| 130 |
@pytest.fixture(scope='function') |
| 131 |
def space(spaceconfig): |
| 132 |
from pypy.tool.pytest.objspace import gettestobjspace |
| 133 |
return gettestobjspace(**spaceconfig) |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
def ensure_pytest_builtin_helpers(helpers='skip raises'.split()): |
| 142 |
""" hack (pytest.) raises and skip into builtins, needed |
| 143 |
for applevel tests to run directly on cpython but |
| 144 |
apparently earlier on "raises" was already added |
| 145 |
to module's globals. |
| 146 |
""" |
| 147 |
try: |
| 148 |
import builtins |
| 149 |
except ImportError: |
| 150 |
import __builtin__ as builtins |
| 151 |
for helper in helpers: |
| 152 |
if not hasattr(builtins, helper): |
| 153 |
try: |
| 154 |
setattr(builtins, helper, getattr(pytest, helper)) |
| 155 |
except AttributeError: |
| 156 |
|
| 157 |
pass |
| 158 |
|
| 159 |
def pytest_sessionstart(session): |
| 160 |
""" before session.main() is called. """ |
| 161 |
|
| 162 |
ensure_pytest_builtin_helpers() |
| 163 |
|
| 164 |
def pytest_pycollect_makemodule(path, parent): |
| 165 |
if path.fnmatch(APPLEVEL_FN): |
| 166 |
if parent.config.getoption('direct_apptest'): |
| 167 |
return |
| 168 |
from pypy.tool.pytest.apptest2 import AppTestModule |
| 169 |
rewrite = parent.config.getoption('applevel_rewrite') |
| 170 |
return AppTestModule(path, parent, rewrite_asserts=rewrite) |
| 171 |
else: |
| 172 |
if hasattr(PyPyModule, "from_parent"): |
| 173 |
|
| 174 |
return PyPyModule.from_parent(parent, fspath=path) |
| 175 |
else: |
| 176 |
return PyPyModule(path, parent) |
| 177 |
|
| 178 |
def is_applevel(item): |
| 179 |
from pypy.tool.pytest.apptest import AppTestMethod |
| 180 |
return isinstance(item, AppTestMethod) |
| 181 |
|
| 182 |
def pytest_collection_modifyitems(config, items): |
| 183 |
if config.getoption('runappdirect') or config.getoption('direct_apptest'): |
| 184 |
return |
| 185 |
for item in items: |
| 186 |
if isinstance(item, pytest.Function): |
| 187 |
if is_applevel(item): |
| 188 |
item.add_marker('applevel') |
| 189 |
else: |
| 190 |
item.add_marker('interplevel') |
| 191 |
|
| 192 |
|
| 193 |
class PyPyModule(pytest.Module): |
| 194 |
""" we take care of collecting classes both at app level |
| 195 |
and at interp-level (because we need to stick a space |
| 196 |
at the class) ourselves. |
| 197 |
""" |
| 198 |
def accept_regular_test(self): |
| 199 |
if self.config.option.runappdirect: |
| 200 |
|
| 201 |
for name in self.listnames(): |
| 202 |
if "test_lib_pypy" in name: |
| 203 |
return True |
| 204 |
return False |
| 205 |
return True |
| 206 |
|
| 207 |
def funcnamefilter(self, name): |
| 208 |
if name.startswith('test_'): |
| 209 |
return self.accept_regular_test() |
| 210 |
return False |
| 211 |
|
| 212 |
def classnamefilter(self, name): |
| 213 |
if name.startswith('Test'): |
| 214 |
return self.accept_regular_test() |
| 215 |
if name.startswith('AppTest'): |
| 216 |
return True |
| 217 |
return False |
| 218 |
|
| 219 |
def makeitem(self, name, obj): |
| 220 |
if isclass(obj) and self.classnamefilter(name): |
| 221 |
if name.startswith('AppTest'): |
| 222 |
from pypy.tool.pytest.apptest import AppClassCollector |
| 223 |
return AppClassCollector(name, parent=self) |
| 224 |
return super(PyPyModule, self).makeitem(name, obj) |
| 225 |
|
| 226 |
def skip_on_missing_buildoption(**ropts): |
| 227 |
__tracebackhide__ = True |
| 228 |
import sys |
| 229 |
options = getattr(sys, 'pypy_translation_info', None) |
| 230 |
if options is None: |
| 231 |
pytest.skip("not running on translated pypy3 " |
| 232 |
"(btw, i would need options: %s)" % |
| 233 |
(ropts,)) |
| 234 |
for opt in ropts: |
| 235 |
if not options.has_key(opt) or options[opt] != ropts[opt]: |
| 236 |
break |
| 237 |
else: |
| 238 |
return |
| 239 |
pytest.skip("need translated pypy3 with: %s, got %s" |
| 240 |
%(ropts,options)) |
| 241 |
|
| 242 |
@pytest.hookimpl(tryfirst=True) |
| 243 |
def pytest_runtest_setup(item): |
| 244 |
if isinstance(item, pytest.Function): |
| 245 |
config = item.config |
| 246 |
if get_marker(item, name='pypy_only'): |
| 247 |
if config.applevel is not None and not config.applevel.is_pypy: |
| 248 |
pytest.skip('PyPy-specific test') |
| 249 |
appclass = item.getparent(pytest.Class) |
| 250 |
if appclass is not None: |
| 251 |
from pypy.tool.pytest.objspace import gettestobjspace |
| 252 |
|
| 253 |
spaceconfig = getattr(appclass.obj, 'spaceconfig', {}) |
| 254 |
appclass.obj.space = gettestobjspace(**spaceconfig) |
| 255 |
appclass.obj.runappdirect = config.option.runappdirect |
| 256 |
|
| 257 |
def pytest_ignore_collect(path, config): |
| 258 |
if (config.getoption('direct_apptest') and not path.isdir() |
| 259 |
and not path.fnmatch(APPLEVEL_FN)): |
| 260 |
return True |
| 261 |
return path.check(link=1) |