PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/conftest.py

https://bitbucket.org/pypy/pypy/
Python | 178 lines | 171 code | 3 blank | 4 comment | 0 complexity | 5f6bf9cf488b7e8776566abd49139cde MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import py, pytest, sys, textwrap
  2. from inspect import isclass
  3. # pytest settings
  4. rsyncdirs = ['.', '../lib-python', '../lib_pypy', '../demo']
  5. rsyncignore = ['_cache']
  6. # PyPy's command line extra options (these are added
  7. # to py.test's standard options)
  8. #
  9. option = None
  10. def braindead_deindent(self):
  11. """monkeypatch that wont end up doing stupid in the python tokenizer"""
  12. text = '\n'.join(self.lines)
  13. short = py.std.textwrap.dedent(text)
  14. newsource = py.code.Source()
  15. newsource.lines[:] = short.splitlines()
  16. return newsource
  17. py.code.Source.deindent = braindead_deindent
  18. def pytest_report_header():
  19. return "pytest-%s from %s" % (pytest.__version__, pytest.__file__)
  20. def pytest_addhooks(pluginmanager):
  21. from rpython.conftest import LeakFinder
  22. pluginmanager.register(LeakFinder())
  23. def pytest_configure(config):
  24. global option
  25. option = config.option
  26. def pytest_addoption(parser):
  27. from rpython.conftest import pytest_addoption
  28. pytest_addoption(parser)
  29. group = parser.getgroup("pypy options")
  30. group.addoption('-A', '--runappdirect', action="store_true",
  31. default=False, dest="runappdirect",
  32. help="run applevel tests directly on python interpreter (not through PyPy)")
  33. group.addoption('--direct', action="store_true",
  34. default=False, dest="rundirect",
  35. help="run pexpect tests directly")
  36. group.addoption('--raise-operr', action="store_true",
  37. default=False, dest="raise_operr",
  38. help="Show the interp-level OperationError in app-level tests")
  39. def pytest_funcarg__space(request):
  40. from pypy.tool.pytest.objspace import gettestobjspace
  41. spaceconfig = getattr(request.cls, 'spaceconfig', {})
  42. return gettestobjspace(**spaceconfig)
  43. #
  44. # Interfacing/Integrating with py.test's collection process
  45. #
  46. #
  47. def ensure_pytest_builtin_helpers(helpers='skip raises'.split()):
  48. """ hack (py.test.) raises and skip into builtins, needed
  49. for applevel tests to run directly on cpython but
  50. apparently earlier on "raises" was already added
  51. to module's globals.
  52. """
  53. import __builtin__
  54. for helper in helpers:
  55. if not hasattr(__builtin__, helper):
  56. setattr(__builtin__, helper, getattr(py.test, helper))
  57. def pytest_sessionstart(session):
  58. """ before session.main() is called. """
  59. # stick py.test raise in module globals -- carefully
  60. ensure_pytest_builtin_helpers()
  61. def pytest_pycollect_makemodule(path, parent):
  62. return PyPyModule(path, parent)
  63. def is_applevel(item):
  64. from pypy.tool.pytest.apptest import AppTestFunction
  65. return isinstance(item, AppTestFunction)
  66. def pytest_collection_modifyitems(config, items):
  67. if config.option.runappdirect:
  68. return
  69. for item in items:
  70. if isinstance(item, py.test.Function):
  71. if is_applevel(item):
  72. item.add_marker('applevel')
  73. else:
  74. item.add_marker('interplevel')
  75. class PyPyModule(py.test.collect.Module):
  76. """ we take care of collecting classes both at app level
  77. and at interp-level (because we need to stick a space
  78. at the class) ourselves.
  79. """
  80. def accept_regular_test(self):
  81. if self.config.option.runappdirect:
  82. # only collect regular tests if we are in an 'app_test' directory,
  83. # or in test_lib_pypy
  84. for name in self.listnames():
  85. if "app_test" in name or "test_lib_pypy" in name:
  86. return True
  87. return False
  88. return True
  89. def funcnamefilter(self, name):
  90. if name.startswith('test_'):
  91. return self.accept_regular_test()
  92. if name.startswith('app_test_'):
  93. return True
  94. return False
  95. def classnamefilter(self, name):
  96. if name.startswith('Test'):
  97. return self.accept_regular_test()
  98. if name.startswith('AppTest'):
  99. return True
  100. return False
  101. def makeitem(self, name, obj):
  102. if isclass(obj) and self.classnamefilter(name):
  103. if name.startswith('AppTest'):
  104. from pypy.tool.pytest.apptest import AppClassCollector
  105. return AppClassCollector(name, parent=self)
  106. elif hasattr(obj, 'func_code') and self.funcnamefilter(name):
  107. if name.startswith('app_test_'):
  108. assert not obj.func_code.co_flags & 32, \
  109. "generator app level functions? you must be joking"
  110. from pypy.tool.pytest.apptest import AppTestFunction
  111. return AppTestFunction(name, parent=self)
  112. return super(PyPyModule, self).makeitem(name, obj)
  113. def skip_on_missing_buildoption(**ropts):
  114. __tracebackhide__ = True
  115. import sys
  116. options = getattr(sys, 'pypy_translation_info', None)
  117. if options is None:
  118. py.test.skip("not running on translated pypy "
  119. "(btw, i would need options: %s)" %
  120. (ropts,))
  121. for opt in ropts:
  122. if not options.has_key(opt) or options[opt] != ropts[opt]:
  123. break
  124. else:
  125. return
  126. py.test.skip("need translated pypy with: %s, got %s"
  127. %(ropts,options))
  128. class LazyObjSpaceGetter(object):
  129. def __get__(self, obj, cls=None):
  130. from pypy.tool.pytest.objspace import gettestobjspace
  131. space = gettestobjspace()
  132. if cls:
  133. cls.space = space
  134. return space
  135. def pytest_runtest_setup(__multicall__, item):
  136. if isinstance(item, py.test.collect.Function):
  137. appclass = item.getparent(py.test.Class)
  138. if appclass is not None:
  139. # Make cls.space and cls.runappdirect available in tests.
  140. spaceconfig = getattr(appclass.obj, 'spaceconfig', None)
  141. if spaceconfig is not None:
  142. from pypy.tool.pytest.objspace import gettestobjspace
  143. appclass.obj.space = gettestobjspace(**spaceconfig)
  144. else:
  145. appclass.obj.space = LazyObjSpaceGetter()
  146. appclass.obj.runappdirect = option.runappdirect
  147. __multicall__.execute()
  148. def pytest_ignore_collect(path):
  149. return path.check(link=1)