tmpdir.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. """Support for providing temporary directories to test functions."""
  2. import os
  3. import re
  4. import sys
  5. import tempfile
  6. from pathlib import Path
  7. from typing import Optional
  8. import attr
  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,
  49. config: Config,
  50. *,
  51. _ispytest: bool = False,
  52. ) -> "TempPathFactory":
  53. """Create a factory according to pytest configuration.
  54. :meta private:
  55. """
  56. check_ispytest(_ispytest)
  57. return cls(
  58. given_basetemp=config.option.basetemp,
  59. trace=config.trace.get("tmpdir"),
  60. _ispytest=True,
  61. )
  62. def _ensure_relative_to_basetemp(self, basename: str) -> str:
  63. basename = os.path.normpath(basename)
  64. if (self.getbasetemp() / basename).resolve().parent != self.getbasetemp():
  65. raise ValueError(f"{basename} is not a normalized and relative path")
  66. return basename
  67. def mktemp(self, basename: str, numbered: bool = True) -> Path:
  68. """Create a new temporary directory managed by the factory.
  69. :param basename:
  70. Directory base name, must be a relative path.
  71. :param numbered:
  72. If ``True``, ensure the directory is unique by adding a numbered
  73. suffix greater than any existing one: ``basename="foo-"`` and ``numbered=True``
  74. means that this function will create directories named ``"foo-0"``,
  75. ``"foo-1"``, ``"foo-2"`` and so on.
  76. :returns:
  77. The path to the new directory.
  78. """
  79. basename = self._ensure_relative_to_basetemp(basename)
  80. if not numbered:
  81. p = self.getbasetemp().joinpath(basename)
  82. p.mkdir(mode=0o700)
  83. else:
  84. p = make_numbered_dir(root=self.getbasetemp(), prefix=basename, mode=0o700)
  85. self._trace("mktemp", p)
  86. return p
  87. def getbasetemp(self) -> Path:
  88. """Return the base temporary directory, creating it if needed."""
  89. if self._basetemp is not None:
  90. return self._basetemp
  91. if self._given_basetemp is not None:
  92. basetemp = self._given_basetemp
  93. if basetemp.exists():
  94. rm_rf(basetemp)
  95. basetemp.mkdir(mode=0o700)
  96. basetemp = basetemp.resolve()
  97. else:
  98. from_env = os.environ.get("PYTEST_DEBUG_TEMPROOT")
  99. temproot = Path(from_env or tempfile.gettempdir()).resolve()
  100. user = get_user() or "unknown"
  101. # use a sub-directory in the temproot to speed-up
  102. # make_numbered_dir() call
  103. rootdir = temproot.joinpath(f"pytest-of-{user}")
  104. try:
  105. rootdir.mkdir(mode=0o700, exist_ok=True)
  106. except OSError:
  107. # getuser() likely returned illegal characters for the platform, use unknown back off mechanism
  108. rootdir = temproot.joinpath("pytest-of-unknown")
  109. rootdir.mkdir(mode=0o700, exist_ok=True)
  110. # Because we use exist_ok=True with a predictable name, make sure
  111. # we are the owners, to prevent any funny business (on unix, where
  112. # temproot is usually shared).
  113. # Also, to keep things private, fixup any world-readable temp
  114. # rootdir's permissions. Historically 0o755 was used, so we can't
  115. # just error out on this, at least for a while.
  116. if sys.platform != "win32":
  117. uid = os.getuid()
  118. rootdir_stat = rootdir.stat()
  119. # getuid shouldn't fail, but cpython defines such a case.
  120. # Let's hope for the best.
  121. if uid != -1:
  122. if rootdir_stat.st_uid != uid:
  123. raise OSError(
  124. f"The temporary directory {rootdir} is not owned by the current user. "
  125. "Fix this and try again."
  126. )
  127. if (rootdir_stat.st_mode & 0o077) != 0:
  128. os.chmod(rootdir, rootdir_stat.st_mode & ~0o077)
  129. basetemp = make_numbered_dir_with_cleanup(
  130. prefix="pytest-",
  131. root=rootdir,
  132. keep=3,
  133. lock_timeout=LOCK_TIMEOUT,
  134. mode=0o700,
  135. )
  136. assert basetemp is not None, basetemp
  137. self._basetemp = basetemp
  138. self._trace("new basetemp", basetemp)
  139. return basetemp
  140. def get_user() -> Optional[str]:
  141. """Return the current user name, or None if getuser() does not work
  142. in the current environment (see #1010)."""
  143. import getpass
  144. try:
  145. return getpass.getuser()
  146. except (ImportError, KeyError):
  147. return None
  148. def pytest_configure(config: Config) -> None:
  149. """Create a TempPathFactory and attach it to the config object.
  150. This is to comply with existing plugins which expect the handler to be
  151. available at pytest_configure time, but ideally should be moved entirely
  152. to the tmp_path_factory session fixture.
  153. """
  154. mp = MonkeyPatch()
  155. config.add_cleanup(mp.undo)
  156. _tmp_path_factory = TempPathFactory.from_config(config, _ispytest=True)
  157. mp.setattr(config, "_tmp_path_factory", _tmp_path_factory, raising=False)
  158. @fixture(scope="session")
  159. def tmp_path_factory(request: FixtureRequest) -> TempPathFactory:
  160. """Return a :class:`pytest.TempPathFactory` instance for the test session."""
  161. # Set dynamically by pytest_configure() above.
  162. return request.config._tmp_path_factory # type: ignore
  163. def _mk_tmp(request: FixtureRequest, factory: TempPathFactory) -> Path:
  164. name = request.node.name
  165. name = re.sub(r"[\W]", "_", name)
  166. MAXVAL = 30
  167. name = name[:MAXVAL]
  168. return factory.mktemp(name, numbered=True)
  169. @fixture
  170. def tmp_path(request: FixtureRequest, tmp_path_factory: TempPathFactory) -> Path:
  171. """Return a temporary directory path object which is unique to each test
  172. function invocation, created as a sub directory of the base temporary
  173. directory.
  174. By default, a new base temporary directory is created each test session,
  175. and old bases are removed after 3 sessions, to aid in debugging. If
  176. ``--basetemp`` is used then it is cleared each session. See :ref:`base
  177. temporary directory`.
  178. The returned object is a :class:`pathlib.Path` object.
  179. """
  180. return _mk_tmp(request, tmp_path_factory)