monkeypatch.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. # -*- coding: utf-8 -*-
  2. """ monkeypatching and mocking functionality. """
  3. from __future__ import absolute_import
  4. from __future__ import division
  5. from __future__ import print_function
  6. import os
  7. import re
  8. import sys
  9. import warnings
  10. from contextlib import contextmanager
  11. import six
  12. import pytest
  13. from _pytest.fixtures import fixture
  14. from _pytest.pathlib import Path
  15. RE_IMPORT_ERROR_NAME = re.compile(r"^No module named (.*)$")
  16. @fixture
  17. def monkeypatch():
  18. """The returned ``monkeypatch`` fixture provides these
  19. helper methods to modify objects, dictionaries or os.environ::
  20. monkeypatch.setattr(obj, name, value, raising=True)
  21. monkeypatch.delattr(obj, name, raising=True)
  22. monkeypatch.setitem(mapping, name, value)
  23. monkeypatch.delitem(obj, name, raising=True)
  24. monkeypatch.setenv(name, value, prepend=False)
  25. monkeypatch.delenv(name, raising=True)
  26. monkeypatch.syspath_prepend(path)
  27. monkeypatch.chdir(path)
  28. All modifications will be undone after the requesting
  29. test function or fixture has finished. The ``raising``
  30. parameter determines if a KeyError or AttributeError
  31. will be raised if the set/deletion operation has no target.
  32. """
  33. mpatch = MonkeyPatch()
  34. yield mpatch
  35. mpatch.undo()
  36. def resolve(name):
  37. # simplified from zope.dottedname
  38. parts = name.split(".")
  39. used = parts.pop(0)
  40. found = __import__(used)
  41. for part in parts:
  42. used += "." + part
  43. try:
  44. found = getattr(found, part)
  45. except AttributeError:
  46. pass
  47. else:
  48. continue
  49. # we use explicit un-nesting of the handling block in order
  50. # to avoid nested exceptions on python 3
  51. try:
  52. __import__(used)
  53. except ImportError as ex:
  54. # str is used for py2 vs py3
  55. expected = str(ex).split()[-1]
  56. if expected == used:
  57. raise
  58. else:
  59. raise ImportError("import error in %s: %s" % (used, ex))
  60. found = annotated_getattr(found, part, used)
  61. return found
  62. def annotated_getattr(obj, name, ann):
  63. try:
  64. obj = getattr(obj, name)
  65. except AttributeError:
  66. raise AttributeError(
  67. "%r object at %s has no attribute %r" % (type(obj).__name__, ann, name)
  68. )
  69. return obj
  70. def derive_importpath(import_path, raising):
  71. if not isinstance(import_path, six.string_types) or "." not in import_path:
  72. raise TypeError("must be absolute import path string, not %r" % (import_path,))
  73. module, attr = import_path.rsplit(".", 1)
  74. target = resolve(module)
  75. if raising:
  76. annotated_getattr(target, attr, ann=module)
  77. return attr, target
  78. class Notset(object):
  79. def __repr__(self):
  80. return "<notset>"
  81. notset = Notset()
  82. class MonkeyPatch(object):
  83. """ Object returned by the ``monkeypatch`` fixture keeping a record of setattr/item/env/syspath changes.
  84. """
  85. def __init__(self):
  86. self._setattr = []
  87. self._setitem = []
  88. self._cwd = None
  89. self._savesyspath = None
  90. @contextmanager
  91. def context(self):
  92. """
  93. Context manager that returns a new :class:`MonkeyPatch` object which
  94. undoes any patching done inside the ``with`` block upon exit:
  95. .. code-block:: python
  96. import functools
  97. def test_partial(monkeypatch):
  98. with monkeypatch.context() as m:
  99. m.setattr(functools, "partial", 3)
  100. Useful in situations where it is desired to undo some patches before the test ends,
  101. such as mocking ``stdlib`` functions that might break pytest itself if mocked (for examples
  102. of this see `#3290 <https://github.com/pytest-dev/pytest/issues/3290>`_.
  103. """
  104. m = MonkeyPatch()
  105. try:
  106. yield m
  107. finally:
  108. m.undo()
  109. def setattr(self, target, name, value=notset, raising=True):
  110. """ Set attribute value on target, memorizing the old value.
  111. By default raise AttributeError if the attribute did not exist.
  112. For convenience you can specify a string as ``target`` which
  113. will be interpreted as a dotted import path, with the last part
  114. being the attribute name. Example:
  115. ``monkeypatch.setattr("os.getcwd", lambda: "/")``
  116. would set the ``getcwd`` function of the ``os`` module.
  117. The ``raising`` value determines if the setattr should fail
  118. if the attribute is not already present (defaults to True
  119. which means it will raise).
  120. """
  121. __tracebackhide__ = True
  122. import inspect
  123. if value is notset:
  124. if not isinstance(target, six.string_types):
  125. raise TypeError(
  126. "use setattr(target, name, value) or "
  127. "setattr(target, value) with target being a dotted "
  128. "import string"
  129. )
  130. value = name
  131. name, target = derive_importpath(target, raising)
  132. oldval = getattr(target, name, notset)
  133. if raising and oldval is notset:
  134. raise AttributeError("%r has no attribute %r" % (target, name))
  135. # avoid class descriptors like staticmethod/classmethod
  136. if inspect.isclass(target):
  137. oldval = target.__dict__.get(name, notset)
  138. self._setattr.append((target, name, oldval))
  139. setattr(target, name, value)
  140. def delattr(self, target, name=notset, raising=True):
  141. """ Delete attribute ``name`` from ``target``, by default raise
  142. AttributeError it the attribute did not previously exist.
  143. If no ``name`` is specified and ``target`` is a string
  144. it will be interpreted as a dotted import path with the
  145. last part being the attribute name.
  146. If ``raising`` is set to False, no exception will be raised if the
  147. attribute is missing.
  148. """
  149. __tracebackhide__ = True
  150. import inspect
  151. if name is notset:
  152. if not isinstance(target, six.string_types):
  153. raise TypeError(
  154. "use delattr(target, name) or "
  155. "delattr(target) with target being a dotted "
  156. "import string"
  157. )
  158. name, target = derive_importpath(target, raising)
  159. if not hasattr(target, name):
  160. if raising:
  161. raise AttributeError(name)
  162. else:
  163. oldval = getattr(target, name, notset)
  164. # Avoid class descriptors like staticmethod/classmethod.
  165. if inspect.isclass(target):
  166. oldval = target.__dict__.get(name, notset)
  167. self._setattr.append((target, name, oldval))
  168. delattr(target, name)
  169. def setitem(self, dic, name, value):
  170. """ Set dictionary entry ``name`` to value. """
  171. self._setitem.append((dic, name, dic.get(name, notset)))
  172. dic[name] = value
  173. def delitem(self, dic, name, raising=True):
  174. """ Delete ``name`` from dict. Raise KeyError if it doesn't exist.
  175. If ``raising`` is set to False, no exception will be raised if the
  176. key is missing.
  177. """
  178. if name not in dic:
  179. if raising:
  180. raise KeyError(name)
  181. else:
  182. self._setitem.append((dic, name, dic.get(name, notset)))
  183. del dic[name]
  184. def _warn_if_env_name_is_not_str(self, name):
  185. """On Python 2, warn if the given environment variable name is not a native str (#4056)"""
  186. if six.PY2 and not isinstance(name, str):
  187. warnings.warn(
  188. pytest.PytestWarning(
  189. "Environment variable name {!r} should be str".format(name)
  190. )
  191. )
  192. def setenv(self, name, value, prepend=None):
  193. """ Set environment variable ``name`` to ``value``. If ``prepend``
  194. is a character, read the current environment variable value
  195. and prepend the ``value`` adjoined with the ``prepend`` character."""
  196. if not isinstance(value, str):
  197. warnings.warn(
  198. pytest.PytestWarning(
  199. "Value of environment variable {name} type should be str, but got "
  200. "{value!r} (type: {type}); converted to str implicitly".format(
  201. name=name, value=value, type=type(value).__name__
  202. )
  203. ),
  204. stacklevel=2,
  205. )
  206. value = str(value)
  207. if prepend and name in os.environ:
  208. value = value + prepend + os.environ[name]
  209. self._warn_if_env_name_is_not_str(name)
  210. self.setitem(os.environ, name, value)
  211. def delenv(self, name, raising=True):
  212. """ Delete ``name`` from the environment. Raise KeyError if it does
  213. not exist.
  214. If ``raising`` is set to False, no exception will be raised if the
  215. environment variable is missing.
  216. """
  217. self._warn_if_env_name_is_not_str(name)
  218. self.delitem(os.environ, name, raising=raising)
  219. def syspath_prepend(self, path):
  220. """ Prepend ``path`` to ``sys.path`` list of import locations. """
  221. from pkg_resources import fixup_namespace_packages
  222. if self._savesyspath is None:
  223. self._savesyspath = sys.path[:]
  224. sys.path.insert(0, str(path))
  225. # https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171
  226. fixup_namespace_packages(str(path))
  227. # A call to syspathinsert() usually means that the caller wants to
  228. # import some dynamically created files, thus with python3 we
  229. # invalidate its import caches.
  230. # This is especially important when any namespace package is in used,
  231. # since then the mtime based FileFinder cache (that gets created in
  232. # this case already) gets not invalidated when writing the new files
  233. # quickly afterwards.
  234. if sys.version_info >= (3, 3):
  235. from importlib import invalidate_caches
  236. invalidate_caches()
  237. def chdir(self, path):
  238. """ Change the current working directory to the specified path.
  239. Path can be a string or a py.path.local object.
  240. """
  241. if self._cwd is None:
  242. self._cwd = os.getcwd()
  243. if hasattr(path, "chdir"):
  244. path.chdir()
  245. elif isinstance(path, Path):
  246. # modern python uses the fspath protocol here LEGACY
  247. os.chdir(str(path))
  248. else:
  249. os.chdir(path)
  250. def undo(self):
  251. """ Undo previous changes. This call consumes the
  252. undo stack. Calling it a second time has no effect unless
  253. you do more monkeypatching after the undo call.
  254. There is generally no need to call `undo()`, since it is
  255. called automatically during tear-down.
  256. Note that the same `monkeypatch` fixture is used across a
  257. single test function invocation. If `monkeypatch` is used both by
  258. the test function itself and one of the test fixtures,
  259. calling `undo()` will undo all of the changes made in
  260. both functions.
  261. """
  262. for obj, name, value in reversed(self._setattr):
  263. if value is not notset:
  264. setattr(obj, name, value)
  265. else:
  266. delattr(obj, name)
  267. self._setattr[:] = []
  268. for dictionary, name, value in reversed(self._setitem):
  269. if value is notset:
  270. try:
  271. del dictionary[name]
  272. except KeyError:
  273. pass # was already deleted, so we have the desired state
  274. else:
  275. dictionary[name] = value
  276. self._setitem[:] = []
  277. if self._savesyspath is not None:
  278. sys.path[:] = self._savesyspath
  279. self._savesyspath = None
  280. if self._cwd is not None:
  281. os.chdir(self._cwd)
  282. self._cwd = None