structures.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. import inspect
  2. import warnings
  3. from collections import namedtuple
  4. from functools import reduce
  5. from operator import attrgetter
  6. import attr
  7. from ..deprecated import MARK_PARAMETERSET_UNPACKING, MARK_INFO_ATTRIBUTE
  8. from ..compat import NOTSET, getfslineno, MappingMixin
  9. from six.moves import map
  10. EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark"
  11. def alias(name, warning=None):
  12. getter = attrgetter(name)
  13. def warned(self):
  14. warnings.warn(warning, stacklevel=2)
  15. return getter(self)
  16. return property(getter if warning is None else warned, doc="alias for " + name)
  17. def istestfunc(func):
  18. return (
  19. hasattr(func, "__call__")
  20. and getattr(func, "__name__", "<lambda>") != "<lambda>"
  21. )
  22. def get_empty_parameterset_mark(config, argnames, func):
  23. requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION)
  24. if requested_mark in ("", None, "skip"):
  25. mark = MARK_GEN.skip
  26. elif requested_mark == "xfail":
  27. mark = MARK_GEN.xfail(run=False)
  28. else:
  29. raise LookupError(requested_mark)
  30. fs, lineno = getfslineno(func)
  31. reason = "got empty parameter set %r, function %s at %s:%d" % (
  32. argnames,
  33. func.__name__,
  34. fs,
  35. lineno,
  36. )
  37. return mark(reason=reason)
  38. class ParameterSet(namedtuple("ParameterSet", "values, marks, id")):
  39. @classmethod
  40. def param(cls, *values, **kw):
  41. marks = kw.pop("marks", ())
  42. if isinstance(marks, MarkDecorator):
  43. marks = (marks,)
  44. else:
  45. assert isinstance(marks, (tuple, list, set))
  46. def param_extract_id(id=None):
  47. return id
  48. id_ = param_extract_id(**kw)
  49. return cls(values, marks, id_)
  50. @classmethod
  51. def extract_from(cls, parameterset, legacy_force_tuple=False):
  52. """
  53. :param parameterset:
  54. a legacy style parameterset that may or may not be a tuple,
  55. and may or may not be wrapped into a mess of mark objects
  56. :param legacy_force_tuple:
  57. enforce tuple wrapping so single argument tuple values
  58. don't get decomposed and break tests
  59. """
  60. if isinstance(parameterset, cls):
  61. return parameterset
  62. if not isinstance(parameterset, MarkDecorator) and legacy_force_tuple:
  63. return cls.param(parameterset)
  64. newmarks = []
  65. argval = parameterset
  66. while isinstance(argval, MarkDecorator):
  67. newmarks.append(
  68. MarkDecorator(Mark(argval.markname, argval.args[:-1], argval.kwargs))
  69. )
  70. argval = argval.args[-1]
  71. assert not isinstance(argval, ParameterSet)
  72. if legacy_force_tuple:
  73. argval = (argval,)
  74. if newmarks:
  75. warnings.warn(MARK_PARAMETERSET_UNPACKING)
  76. return cls(argval, marks=newmarks, id=None)
  77. @classmethod
  78. def _for_parametrize(cls, argnames, argvalues, func, config):
  79. if not isinstance(argnames, (tuple, list)):
  80. argnames = [x.strip() for x in argnames.split(",") if x.strip()]
  81. force_tuple = len(argnames) == 1
  82. else:
  83. force_tuple = False
  84. parameters = [
  85. ParameterSet.extract_from(x, legacy_force_tuple=force_tuple)
  86. for x in argvalues
  87. ]
  88. del argvalues
  89. if parameters:
  90. # check all parameter sets have the correct number of values
  91. for param in parameters:
  92. if len(param.values) != len(argnames):
  93. raise ValueError(
  94. 'In "parametrize" the number of values ({}) must be '
  95. "equal to the number of names ({})".format(
  96. param.values, argnames
  97. )
  98. )
  99. else:
  100. # empty parameter set (likely computed at runtime): create a single
  101. # parameter set with NOSET values, with the "empty parameter set" mark applied to it
  102. mark = get_empty_parameterset_mark(config, argnames, func)
  103. parameters.append(
  104. ParameterSet(values=(NOTSET,) * len(argnames), marks=[mark], id=None)
  105. )
  106. return argnames, parameters
  107. @attr.s(frozen=True)
  108. class Mark(object):
  109. #: name of the mark
  110. name = attr.ib(type=str)
  111. #: positional arguments of the mark decorator
  112. args = attr.ib() # type: List[object]
  113. #: keyword arguments of the mark decorator
  114. kwargs = attr.ib() # type: Dict[str, object]
  115. def combined_with(self, other):
  116. """
  117. :param other: the mark to combine with
  118. :type other: Mark
  119. :rtype: Mark
  120. combines by appending aargs and merging the mappings
  121. """
  122. assert self.name == other.name
  123. return Mark(
  124. self.name, self.args + other.args, dict(self.kwargs, **other.kwargs)
  125. )
  126. @attr.s
  127. class MarkDecorator(object):
  128. """ A decorator for test functions and test classes. When applied
  129. it will create :class:`MarkInfo` objects which may be
  130. :ref:`retrieved by hooks as item keywords <excontrolskip>`.
  131. MarkDecorator instances are often created like this::
  132. mark1 = pytest.mark.NAME # simple MarkDecorator
  133. mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator
  134. and can then be applied as decorators to test functions::
  135. @mark2
  136. def test_function():
  137. pass
  138. When a MarkDecorator instance is called it does the following:
  139. 1. If called with a single class as its only positional argument and no
  140. additional keyword arguments, it attaches itself to the class so it
  141. gets applied automatically to all test cases found in that class.
  142. 2. If called with a single function as its only positional argument and
  143. no additional keyword arguments, it attaches a MarkInfo object to the
  144. function, containing all the arguments already stored internally in
  145. the MarkDecorator.
  146. 3. When called in any other case, it performs a 'fake construction' call,
  147. i.e. it returns a new MarkDecorator instance with the original
  148. MarkDecorator's content updated with the arguments passed to this
  149. call.
  150. Note: The rules above prevent MarkDecorator objects from storing only a
  151. single function or class reference as their positional argument with no
  152. additional keyword or positional arguments.
  153. """
  154. mark = attr.ib(validator=attr.validators.instance_of(Mark))
  155. name = alias("mark.name")
  156. args = alias("mark.args")
  157. kwargs = alias("mark.kwargs")
  158. @property
  159. def markname(self):
  160. return self.name # for backward-compat (2.4.1 had this attr)
  161. def __eq__(self, other):
  162. return self.mark == other.mark if isinstance(other, MarkDecorator) else False
  163. def __repr__(self):
  164. return "<MarkDecorator %r>" % (self.mark,)
  165. def with_args(self, *args, **kwargs):
  166. """ return a MarkDecorator with extra arguments added
  167. unlike call this can be used even if the sole argument is a callable/class
  168. :return: MarkDecorator
  169. """
  170. mark = Mark(self.name, args, kwargs)
  171. return self.__class__(self.mark.combined_with(mark))
  172. def __call__(self, *args, **kwargs):
  173. """ if passed a single callable argument: decorate it with mark info.
  174. otherwise add *args/**kwargs in-place to mark information. """
  175. if args and not kwargs:
  176. func = args[0]
  177. is_class = inspect.isclass(func)
  178. if len(args) == 1 and (istestfunc(func) or is_class):
  179. if is_class:
  180. store_mark(func, self.mark)
  181. else:
  182. store_legacy_markinfo(func, self.mark)
  183. store_mark(func, self.mark)
  184. return func
  185. return self.with_args(*args, **kwargs)
  186. def get_unpacked_marks(obj):
  187. """
  188. obtain the unpacked marks that are stored on an object
  189. """
  190. mark_list = getattr(obj, "pytestmark", [])
  191. if not isinstance(mark_list, list):
  192. mark_list = [mark_list]
  193. return normalize_mark_list(mark_list)
  194. def normalize_mark_list(mark_list):
  195. """
  196. normalizes marker decorating helpers to mark objects
  197. :type mark_list: List[Union[Mark, Markdecorator]]
  198. :rtype: List[Mark]
  199. """
  200. return [getattr(mark, "mark", mark) for mark in mark_list] # unpack MarkDecorator
  201. def store_mark(obj, mark):
  202. """store a Mark on an object
  203. this is used to implement the Mark declarations/decorators correctly
  204. """
  205. assert isinstance(mark, Mark), mark
  206. # always reassign name to avoid updating pytestmark
  207. # in a reference that was only borrowed
  208. obj.pytestmark = get_unpacked_marks(obj) + [mark]
  209. def store_legacy_markinfo(func, mark):
  210. """create the legacy MarkInfo objects and put them onto the function
  211. """
  212. if not isinstance(mark, Mark):
  213. raise TypeError("got {mark!r} instead of a Mark".format(mark=mark))
  214. holder = getattr(func, mark.name, None)
  215. if holder is None:
  216. holder = MarkInfo.for_mark(mark)
  217. setattr(func, mark.name, holder)
  218. elif isinstance(holder, MarkInfo):
  219. holder.add_mark(mark)
  220. def transfer_markers(funcobj, cls, mod):
  221. """
  222. this function transfers class level markers and module level markers
  223. into function level markinfo objects
  224. this is the main reason why marks are so broken
  225. the resolution will involve phasing out function level MarkInfo objects
  226. """
  227. for obj in (cls, mod):
  228. for mark in get_unpacked_marks(obj):
  229. if not _marked(funcobj, mark):
  230. store_legacy_markinfo(funcobj, mark)
  231. def _marked(func, mark):
  232. """ Returns True if :func: is already marked with :mark:, False otherwise.
  233. This can happen if marker is applied to class and the test file is
  234. invoked more than once.
  235. """
  236. try:
  237. func_mark = getattr(func, getattr(mark, "combined", mark).name)
  238. except AttributeError:
  239. return False
  240. return any(mark == info.combined for info in func_mark)
  241. @attr.s
  242. class MarkInfo(object):
  243. """ Marking object created by :class:`MarkDecorator` instances. """
  244. _marks = attr.ib(converter=list)
  245. @_marks.validator
  246. def validate_marks(self, attribute, value):
  247. for item in value:
  248. if not isinstance(item, Mark):
  249. raise ValueError(
  250. "MarkInfo expects Mark instances, got {!r} ({!r})".format(
  251. item, type(item)
  252. )
  253. )
  254. combined = attr.ib(
  255. repr=False,
  256. default=attr.Factory(
  257. lambda self: reduce(Mark.combined_with, self._marks), takes_self=True
  258. ),
  259. )
  260. name = alias("combined.name", warning=MARK_INFO_ATTRIBUTE)
  261. args = alias("combined.args", warning=MARK_INFO_ATTRIBUTE)
  262. kwargs = alias("combined.kwargs", warning=MARK_INFO_ATTRIBUTE)
  263. @classmethod
  264. def for_mark(cls, mark):
  265. return cls([mark])
  266. def __repr__(self):
  267. return "<MarkInfo {!r}>".format(self.combined)
  268. def add_mark(self, mark):
  269. """ add a MarkInfo with the given args and kwargs. """
  270. self._marks.append(mark)
  271. self.combined = self.combined.combined_with(mark)
  272. def __iter__(self):
  273. """ yield MarkInfo objects each relating to a marking-call. """
  274. return map(MarkInfo.for_mark, self._marks)
  275. class MarkGenerator(object):
  276. """ Factory for :class:`MarkDecorator` objects - exposed as
  277. a ``pytest.mark`` singleton instance. Example::
  278. import pytest
  279. @pytest.mark.slowtest
  280. def test_function():
  281. pass
  282. will set a 'slowtest' :class:`MarkInfo` object
  283. on the ``test_function`` object. """
  284. _config = None
  285. def __getattr__(self, name):
  286. if name[0] == "_":
  287. raise AttributeError("Marker name must NOT start with underscore")
  288. if self._config is not None:
  289. self._check(name)
  290. return MarkDecorator(Mark(name, (), {}))
  291. def _check(self, name):
  292. try:
  293. if name in self._markers:
  294. return
  295. except AttributeError:
  296. pass
  297. self._markers = values = set()
  298. for line in self._config.getini("markers"):
  299. marker = line.split(":", 1)[0]
  300. marker = marker.rstrip()
  301. x = marker.split("(", 1)[0]
  302. values.add(x)
  303. if name not in self._markers:
  304. raise AttributeError("%r not a registered marker" % (name,))
  305. MARK_GEN = MarkGenerator()
  306. class NodeKeywords(MappingMixin):
  307. def __init__(self, node):
  308. self.node = node
  309. self.parent = node.parent
  310. self._markers = {node.name: True}
  311. def __getitem__(self, key):
  312. try:
  313. return self._markers[key]
  314. except KeyError:
  315. if self.parent is None:
  316. raise
  317. return self.parent.keywords[key]
  318. def __setitem__(self, key, value):
  319. self._markers[key] = value
  320. def __delitem__(self, key):
  321. raise ValueError("cannot delete key in keywords dict")
  322. def __iter__(self):
  323. seen = self._seen()
  324. return iter(seen)
  325. def _seen(self):
  326. seen = set(self._markers)
  327. if self.parent is not None:
  328. seen.update(self.parent.keywords)
  329. return seen
  330. def __len__(self):
  331. return len(self._seen())
  332. def __repr__(self):
  333. return "<NodeKeywords for node %s>" % (self.node,)
  334. @attr.s(cmp=False, hash=False)
  335. class NodeMarkers(object):
  336. """
  337. internal strucutre for storing marks belongong to a node
  338. ..warning::
  339. unstable api
  340. """
  341. own_markers = attr.ib(default=attr.Factory(list))
  342. def update(self, add_markers):
  343. """update the own markers
  344. """
  345. self.own_markers.extend(add_markers)
  346. def find(self, name):
  347. """
  348. find markers in own nodes or parent nodes
  349. needs a better place
  350. """
  351. for mark in self.own_markers:
  352. if mark.name == name:
  353. yield mark
  354. def __iter__(self):
  355. return iter(self.own_markers)