monkeypatch.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. """Monkeypatching and mocking functionality."""
  2. import os
  3. import re
  4. import sys
  5. import warnings
  6. from contextlib import contextmanager
  7. from typing import Any
  8. from typing import Generator
  9. from typing import List
  10. from typing import Mapping
  11. from typing import MutableMapping
  12. from typing import Optional
  13. from typing import overload
  14. from typing import Tuple
  15. from typing import TypeVar
  16. from typing import Union
  17. from _pytest.compat import final
  18. from _pytest.fixtures import fixture
  19. from _pytest.warning_types import PytestWarning
  20. RE_IMPORT_ERROR_NAME = re.compile(r"^No module named (.*)$")
  21. K = TypeVar("K")
  22. V = TypeVar("V")
  23. @fixture
  24. def monkeypatch() -> Generator["MonkeyPatch", None, None]:
  25. """A convenient fixture for monkey-patching.
  26. The fixture provides these methods to modify objects, dictionaries, or
  27. :data:`os.environ`:
  28. * :meth:`monkeypatch.setattr(obj, name, value, raising=True) <pytest.MonkeyPatch.setattr>`
  29. * :meth:`monkeypatch.delattr(obj, name, raising=True) <pytest.MonkeyPatch.delattr>`
  30. * :meth:`monkeypatch.setitem(mapping, name, value) <pytest.MonkeyPatch.setitem>`
  31. * :meth:`monkeypatch.delitem(obj, name, raising=True) <pytest.MonkeyPatch.delitem>`
  32. * :meth:`monkeypatch.setenv(name, value, prepend=None) <pytest.MonkeyPatch.setenv>`
  33. * :meth:`monkeypatch.delenv(name, raising=True) <pytest.MonkeyPatch.delenv>`
  34. * :meth:`monkeypatch.syspath_prepend(path) <pytest.MonkeyPatch.syspath_prepend>`
  35. * :meth:`monkeypatch.chdir(path) <pytest.MonkeyPatch.chdir>`
  36. * :meth:`monkeypatch.context() <pytest.MonkeyPatch.context>`
  37. All modifications will be undone after the requesting test function or
  38. fixture has finished. The ``raising`` parameter determines if a :class:`KeyError`
  39. or :class:`AttributeError` will be raised if the set/deletion operation does not have the
  40. specified target.
  41. To undo modifications done by the fixture in a contained scope,
  42. use :meth:`context() <pytest.MonkeyPatch.context>`.
  43. """
  44. mpatch = MonkeyPatch()
  45. yield mpatch
  46. mpatch.undo()
  47. def resolve(name: str) -> object:
  48. # Simplified from zope.dottedname.
  49. parts = name.split(".")
  50. used = parts.pop(0)
  51. found: object = __import__(used)
  52. for part in parts:
  53. used += "." + part
  54. try:
  55. found = getattr(found, part)
  56. except AttributeError:
  57. pass
  58. else:
  59. continue
  60. # We use explicit un-nesting of the handling block in order
  61. # to avoid nested exceptions.
  62. try:
  63. __import__(used)
  64. except ImportError as ex:
  65. expected = str(ex).split()[-1]
  66. if expected == used:
  67. raise
  68. else:
  69. raise ImportError(f"import error in {used}: {ex}") from ex
  70. found = annotated_getattr(found, part, used)
  71. return found
  72. def annotated_getattr(obj: object, name: str, ann: str) -> object:
  73. try:
  74. obj = getattr(obj, name)
  75. except AttributeError as e:
  76. raise AttributeError(
  77. "{!r} object at {} has no attribute {!r}".format(
  78. type(obj).__name__, ann, name
  79. )
  80. ) from e
  81. return obj
  82. def derive_importpath(import_path: str, raising: bool) -> Tuple[str, object]:
  83. if not isinstance(import_path, str) or "." not in import_path:
  84. raise TypeError(f"must be absolute import path string, not {import_path!r}")
  85. module, attr = import_path.rsplit(".", 1)
  86. target = resolve(module)
  87. if raising:
  88. annotated_getattr(target, attr, ann=module)
  89. return attr, target
  90. class Notset:
  91. def __repr__(self) -> str:
  92. return "<notset>"
  93. notset = Notset()
  94. @final
  95. class MonkeyPatch:
  96. """Helper to conveniently monkeypatch attributes/items/environment
  97. variables/syspath.
  98. Returned by the :fixture:`monkeypatch` fixture.
  99. .. versionchanged:: 6.2
  100. Can now also be used directly as `pytest.MonkeyPatch()`, for when
  101. the fixture is not available. In this case, use
  102. :meth:`with MonkeyPatch.context() as mp: <context>` or remember to call
  103. :meth:`undo` explicitly.
  104. """
  105. def __init__(self) -> None:
  106. self._setattr: List[Tuple[object, str, object]] = []
  107. self._setitem: List[Tuple[Mapping[Any, Any], object, object]] = []
  108. self._cwd: Optional[str] = None
  109. self._savesyspath: Optional[List[str]] = None
  110. @classmethod
  111. @contextmanager
  112. def context(cls) -> Generator["MonkeyPatch", None, None]:
  113. """Context manager that returns a new :class:`MonkeyPatch` object
  114. which undoes any patching done inside the ``with`` block upon exit.
  115. Example:
  116. .. code-block:: python
  117. import functools
  118. def test_partial(monkeypatch):
  119. with monkeypatch.context() as m:
  120. m.setattr(functools, "partial", 3)
  121. Useful in situations where it is desired to undo some patches before the test ends,
  122. such as mocking ``stdlib`` functions that might break pytest itself if mocked (for examples
  123. of this see :issue:`3290`).
  124. """
  125. m = cls()
  126. try:
  127. yield m
  128. finally:
  129. m.undo()
  130. @overload
  131. def setattr(
  132. self,
  133. target: str,
  134. name: object,
  135. value: Notset = ...,
  136. raising: bool = ...,
  137. ) -> None:
  138. ...
  139. @overload
  140. def setattr(
  141. self,
  142. target: object,
  143. name: str,
  144. value: object,
  145. raising: bool = ...,
  146. ) -> None:
  147. ...
  148. def setattr(
  149. self,
  150. target: Union[str, object],
  151. name: Union[object, str],
  152. value: object = notset,
  153. raising: bool = True,
  154. ) -> None:
  155. """
  156. Set attribute value on target, memorizing the old value.
  157. For example:
  158. .. code-block:: python
  159. import os
  160. monkeypatch.setattr(os, "getcwd", lambda: "/")
  161. The code above replaces the :func:`os.getcwd` function by a ``lambda`` which
  162. always returns ``"/"``.
  163. For convenience, you can specify a string as ``target`` which
  164. will be interpreted as a dotted import path, with the last part
  165. being the attribute name:
  166. .. code-block:: python
  167. monkeypatch.setattr("os.getcwd", lambda: "/")
  168. Raises :class:`AttributeError` if the attribute does not exist, unless
  169. ``raising`` is set to False.
  170. **Where to patch**
  171. ``monkeypatch.setattr`` works by (temporarily) changing the object that a name points to with another one.
  172. There can be many names pointing to any individual object, so for patching to work you must ensure
  173. that you patch the name used by the system under test.
  174. See the section :ref:`Where to patch <python:where-to-patch>` in the :mod:`unittest.mock`
  175. docs for a complete explanation, which is meant for :func:`unittest.mock.patch` but
  176. applies to ``monkeypatch.setattr`` as well.
  177. """
  178. __tracebackhide__ = True
  179. import inspect
  180. if isinstance(value, Notset):
  181. if not isinstance(target, str):
  182. raise TypeError(
  183. "use setattr(target, name, value) or "
  184. "setattr(target, value) with target being a dotted "
  185. "import string"
  186. )
  187. value = name
  188. name, target = derive_importpath(target, raising)
  189. else:
  190. if not isinstance(name, str):
  191. raise TypeError(
  192. "use setattr(target, name, value) with name being a string or "
  193. "setattr(target, value) with target being a dotted "
  194. "import string"
  195. )
  196. oldval = getattr(target, name, notset)
  197. if raising and oldval is notset:
  198. raise AttributeError(f"{target!r} has no attribute {name!r}")
  199. # avoid class descriptors like staticmethod/classmethod
  200. if inspect.isclass(target):
  201. oldval = target.__dict__.get(name, notset)
  202. self._setattr.append((target, name, oldval))
  203. setattr(target, name, value)
  204. def delattr(
  205. self,
  206. target: Union[object, str],
  207. name: Union[str, Notset] = notset,
  208. raising: bool = True,
  209. ) -> None:
  210. """Delete attribute ``name`` from ``target``.
  211. If no ``name`` is specified and ``target`` is a string
  212. it will be interpreted as a dotted import path with the
  213. last part being the attribute name.
  214. Raises AttributeError it the attribute does not exist, unless
  215. ``raising`` is set to False.
  216. """
  217. __tracebackhide__ = True
  218. import inspect
  219. if isinstance(name, Notset):
  220. if not isinstance(target, str):
  221. raise TypeError(
  222. "use delattr(target, name) or "
  223. "delattr(target) with target being a dotted "
  224. "import string"
  225. )
  226. name, target = derive_importpath(target, raising)
  227. if not hasattr(target, name):
  228. if raising:
  229. raise AttributeError(name)
  230. else:
  231. oldval = getattr(target, name, notset)
  232. # Avoid class descriptors like staticmethod/classmethod.
  233. if inspect.isclass(target):
  234. oldval = target.__dict__.get(name, notset)
  235. self._setattr.append((target, name, oldval))
  236. delattr(target, name)
  237. def setitem(self, dic: Mapping[K, V], name: K, value: V) -> None:
  238. """Set dictionary entry ``name`` to value."""
  239. self._setitem.append((dic, name, dic.get(name, notset)))
  240. # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
  241. dic[name] = value # type: ignore[index]
  242. def delitem(self, dic: Mapping[K, V], name: K, raising: bool = True) -> None:
  243. """Delete ``name`` from dict.
  244. Raises ``KeyError`` if it doesn't exist, unless ``raising`` is set to
  245. False.
  246. """
  247. if name not in dic:
  248. if raising:
  249. raise KeyError(name)
  250. else:
  251. self._setitem.append((dic, name, dic.get(name, notset)))
  252. # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
  253. del dic[name] # type: ignore[attr-defined]
  254. def setenv(self, name: str, value: str, prepend: Optional[str] = None) -> None:
  255. """Set environment variable ``name`` to ``value``.
  256. If ``prepend`` is a character, read the current environment variable
  257. value and prepend the ``value`` adjoined with the ``prepend``
  258. character.
  259. """
  260. if not isinstance(value, str):
  261. warnings.warn( # type: ignore[unreachable]
  262. PytestWarning(
  263. "Value of environment variable {name} type should be str, but got "
  264. "{value!r} (type: {type}); converted to str implicitly".format(
  265. name=name, value=value, type=type(value).__name__
  266. )
  267. ),
  268. stacklevel=2,
  269. )
  270. value = str(value)
  271. if prepend and name in os.environ:
  272. value = value + prepend + os.environ[name]
  273. self.setitem(os.environ, name, value)
  274. def delenv(self, name: str, raising: bool = True) -> None:
  275. """Delete ``name`` from the environment.
  276. Raises ``KeyError`` if it does not exist, unless ``raising`` is set to
  277. False.
  278. """
  279. environ: MutableMapping[str, str] = os.environ
  280. self.delitem(environ, name, raising=raising)
  281. def syspath_prepend(self, path) -> None:
  282. """Prepend ``path`` to ``sys.path`` list of import locations."""
  283. if self._savesyspath is None:
  284. self._savesyspath = sys.path[:]
  285. sys.path.insert(0, str(path))
  286. # https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171
  287. # this is only needed when pkg_resources was already loaded by the namespace package
  288. if "pkg_resources" in sys.modules:
  289. from pkg_resources import fixup_namespace_packages
  290. fixup_namespace_packages(str(path))
  291. # A call to syspathinsert() usually means that the caller wants to
  292. # import some dynamically created files, thus with python3 we
  293. # invalidate its import caches.
  294. # This is especially important when any namespace package is in use,
  295. # since then the mtime based FileFinder cache (that gets created in
  296. # this case already) gets not invalidated when writing the new files
  297. # quickly afterwards.
  298. from importlib import invalidate_caches
  299. invalidate_caches()
  300. def chdir(self, path: Union[str, "os.PathLike[str]"]) -> None:
  301. """Change the current working directory to the specified path.
  302. :param path:
  303. The path to change into.
  304. """
  305. if self._cwd is None:
  306. self._cwd = os.getcwd()
  307. os.chdir(path)
  308. def undo(self) -> None:
  309. """Undo previous changes.
  310. This call consumes the undo stack. Calling it a second time has no
  311. effect unless you do more monkeypatching after the undo call.
  312. There is generally no need to call `undo()`, since it is
  313. called automatically during tear-down.
  314. .. note::
  315. The same `monkeypatch` fixture is used across a
  316. single test function invocation. If `monkeypatch` is used both by
  317. the test function itself and one of the test fixtures,
  318. calling `undo()` will undo all of the changes made in
  319. both functions.
  320. Prefer to use :meth:`context() <pytest.MonkeyPatch.context>` instead.
  321. """
  322. for obj, name, value in reversed(self._setattr):
  323. if value is not notset:
  324. setattr(obj, name, value)
  325. else:
  326. delattr(obj, name)
  327. self._setattr[:] = []
  328. for dictionary, key, value in reversed(self._setitem):
  329. if value is notset:
  330. try:
  331. # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
  332. del dictionary[key] # type: ignore[attr-defined]
  333. except KeyError:
  334. pass # Was already deleted, so we have the desired state.
  335. else:
  336. # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
  337. dictionary[key] = value # type: ignore[index]
  338. self._setitem[:] = []
  339. if self._savesyspath is not None:
  340. sys.path[:] = self._savesyspath
  341. self._savesyspath = None
  342. if self._cwd is not None:
  343. os.chdir(self._cwd)
  344. self._cwd = None