You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
9.2KB

  1. """Support for providing temporary directories to test functions."""
  2. import os
  3. import re
  4. import tempfile
  5. from pathlib import Path
  6. from typing import Optional
  7. import attr
  8. import py
  9. from .pathlib import LOCK_TIMEOUT
  10. from .pathlib import make_numbered_dir
  11. from .pathlib import make_numbered_dir_with_cleanup
  12. from .pathlib import rm_rf
  13. from _pytest.compat import final
  14. from _pytest.config import Config
  15. from _pytest.deprecated import check_ispytest
  16. from _pytest.fixtures import fixture
  17. from _pytest.fixtures import FixtureRequest
  18. from _pytest.monkeypatch import MonkeyPatch
  19. @final
  20. @attr.s(init=False)
  21. class TempPathFactory:
  22. """Factory for temporary directories under the common base temp directory.
  23. The base directory can be configured using the ``--basetemp`` option.
  24. """
  25. _given_basetemp = attr.ib(type=Optional[Path])
  26. _trace = attr.ib()
  27. _basetemp = attr.ib(type=Optional[Path])
  28. def __init__(
  29. self,
  30. given_basetemp: Optional[Path],
  31. trace,
  32. basetemp: Optional[Path] = None,
  33. *,
  34. _ispytest: bool = False,
  35. ) -> None:
  36. check_ispytest(_ispytest)
  37. if given_basetemp is None:
  38. self._given_basetemp = None
  39. else:
  40. # Use os.path.abspath() to get absolute path instead of resolve() as it
  41. # does not work the same in all platforms (see #4427).
  42. # Path.absolute() exists, but it is not public (see https://bugs.python.org/issue25012).
  43. self._given_basetemp = Path(os.path.abspath(str(given_basetemp)))
  44. self._trace = trace
  45. self._basetemp = basetemp
  46. @classmethod
  47. def from_config(
  48. cls, config: Config, *, _ispytest: bool = False,
  49. ) -> "TempPathFactory":
  50. """Create a factory according to pytest configuration.
  51. :meta private:
  52. """
  53. check_ispytest(_ispytest)
  54. return cls(
  55. given_basetemp=config.option.basetemp,
  56. trace=config.trace.get("tmpdir"),
  57. _ispytest=True,
  58. )
  59. def _ensure_relative_to_basetemp(self, basename: str) -> str:
  60. basename = os.path.normpath(basename)
  61. if (self.getbasetemp() / basename).resolve().parent != self.getbasetemp():
  62. raise ValueError(f"{basename} is not a normalized and relative path")
  63. return basename
  64. def mktemp(self, basename: str, numbered: bool = True) -> Path:
  65. """Create a new temporary directory managed by the factory.
  66. :param basename:
  67. Directory base name, must be a relative path.
  68. :param numbered:
  69. If ``True``, ensure the directory is unique by adding a numbered
  70. suffix greater than any existing one: ``basename="foo-"`` and ``numbered=True``
  71. means that this function will create directories named ``"foo-0"``,
  72. ``"foo-1"``, ``"foo-2"`` and so on.
  73. :returns:
  74. The path to the new directory.
  75. """
  76. basename = self._ensure_relative_to_basetemp(basename)
  77. if not numbered:
  78. p = self.getbasetemp().joinpath(basename)
  79. p.mkdir(mode=0o700)
  80. else:
  81. p = make_numbered_dir(root=self.getbasetemp(), prefix=basename, mode=0o700)
  82. self._trace("mktemp", p)
  83. return p
  84. def getbasetemp(self) -> Path:
  85. """Return the base temporary directory, creating it if needed."""
  86. if self._basetemp is not None:
  87. return self._basetemp
  88. if self._given_basetemp is not None:
  89. basetemp = self._given_basetemp
  90. if basetemp.exists():
  91. rm_rf(basetemp)
  92. basetemp.mkdir(mode=0o700)
  93. basetemp = basetemp.resolve()
  94. else:
  95. from_env = os.environ.get("PYTEST_DEBUG_TEMPROOT")
  96. temproot = Path(from_env or tempfile.gettempdir()).resolve()
  97. user = get_user() or "unknown"
  98. # use a sub-directory in the temproot to speed-up
  99. # make_numbered_dir() call
  100. rootdir = temproot.joinpath(f"pytest-of-{user}")
  101. rootdir.mkdir(mode=0o700, exist_ok=True)
  102. # Because we use exist_ok=True with a predictable name, make sure
  103. # we are the owners, to prevent any funny business (on unix, where
  104. # temproot is usually shared).
  105. # Also, to keep things private, fixup any world-readable temp
  106. # rootdir's permissions. Historically 0o755 was used, so we can't
  107. # just error out on this, at least for a while.
  108. if hasattr(os, "getuid"):
  109. rootdir_stat = rootdir.stat()
  110. uid = os.getuid()
  111. # getuid shouldn't fail, but cpython defines such a case.
  112. # Let's hope for the best.
  113. if uid != -1:
  114. if rootdir_stat.st_uid != uid:
  115. raise OSError(
  116. f"The temporary directory {rootdir} is not owned by the current user. "
  117. "Fix this and try again."
  118. )
  119. if (rootdir_stat.st_mode & 0o077) != 0:
  120. os.chmod(rootdir, rootdir_stat.st_mode & ~0o077)
  121. basetemp = make_numbered_dir_with_cleanup(
  122. prefix="pytest-",
  123. root=rootdir,
  124. keep=3,
  125. lock_timeout=LOCK_TIMEOUT,
  126. mode=0o700,
  127. )
  128. assert basetemp is not None, basetemp
  129. self._basetemp = basetemp
  130. self._trace("new basetemp", basetemp)
  131. return basetemp
  132. @final
  133. @attr.s(init=False)
  134. class TempdirFactory:
  135. """Backward comptibility wrapper that implements :class:``py.path.local``
  136. for :class:``TempPathFactory``."""
  137. _tmppath_factory = attr.ib(type=TempPathFactory)
  138. def __init__(
  139. self, tmppath_factory: TempPathFactory, *, _ispytest: bool = False
  140. ) -> None:
  141. check_ispytest(_ispytest)
  142. self._tmppath_factory = tmppath_factory
  143. def mktemp(self, basename: str, numbered: bool = True) -> py.path.local:
  144. """Same as :meth:`TempPathFactory.mktemp`, but returns a ``py.path.local`` object."""
  145. return py.path.local(self._tmppath_factory.mktemp(basename, numbered).resolve())
  146. def getbasetemp(self) -> py.path.local:
  147. """Backward compat wrapper for ``_tmppath_factory.getbasetemp``."""
  148. return py.path.local(self._tmppath_factory.getbasetemp().resolve())
  149. def get_user() -> Optional[str]:
  150. """Return the current user name, or None if getuser() does not work
  151. in the current environment (see #1010)."""
  152. import getpass
  153. try:
  154. return getpass.getuser()
  155. except (ImportError, KeyError):
  156. return None
  157. def pytest_configure(config: Config) -> None:
  158. """Create a TempdirFactory and attach it to the config object.
  159. This is to comply with existing plugins which expect the handler to be
  160. available at pytest_configure time, but ideally should be moved entirely
  161. to the tmpdir_factory session fixture.
  162. """
  163. mp = MonkeyPatch()
  164. tmppath_handler = TempPathFactory.from_config(config, _ispytest=True)
  165. t = TempdirFactory(tmppath_handler, _ispytest=True)
  166. config._cleanup.append(mp.undo)
  167. mp.setattr(config, "_tmp_path_factory", tmppath_handler, raising=False)
  168. mp.setattr(config, "_tmpdirhandler", t, raising=False)
  169. @fixture(scope="session")
  170. def tmpdir_factory(request: FixtureRequest) -> TempdirFactory:
  171. """Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session."""
  172. # Set dynamically by pytest_configure() above.
  173. return request.config._tmpdirhandler # type: ignore
  174. @fixture(scope="session")
  175. def tmp_path_factory(request: FixtureRequest) -> TempPathFactory:
  176. """Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session."""
  177. # Set dynamically by pytest_configure() above.
  178. return request.config._tmp_path_factory # type: ignore
  179. def _mk_tmp(request: FixtureRequest, factory: TempPathFactory) -> Path:
  180. name = request.node.name
  181. name = re.sub(r"[\W]", "_", name)
  182. MAXVAL = 30
  183. name = name[:MAXVAL]
  184. return factory.mktemp(name, numbered=True)
  185. @fixture
  186. def tmpdir(tmp_path: Path) -> py.path.local:
  187. """Return a temporary directory path object which is unique to each test
  188. function invocation, created as a sub directory of the base temporary
  189. directory.
  190. By default, a new base temporary directory is created each test session,
  191. and old bases are removed after 3 sessions, to aid in debugging. If
  192. ``--basetemp`` is used then it is cleared each session. See :ref:`base
  193. temporary directory`.
  194. The returned object is a `py.path.local`_ path object.
  195. .. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html
  196. """
  197. return py.path.local(tmp_path)
  198. @fixture
  199. def tmp_path(request: FixtureRequest, tmp_path_factory: TempPathFactory) -> Path:
  200. """Return a temporary directory path object which is unique to each test
  201. function invocation, created as a sub directory of the base temporary
  202. directory.
  203. By default, a new base temporary directory is created each test session,
  204. and old bases are removed after 3 sessions, to aid in debugging. If
  205. ``--basetemp`` is used then it is cleared each session. See :ref:`base
  206. temporary directory`.
  207. The returned object is a :class:`pathlib.Path` object.
  208. """
  209. return _mk_tmp(request, tmp_path_factory)