MCPcopy Create free account
hub / github.com/pytest-dev/pytest / TempPathFactory

Class TempPathFactory

src/_pytest/tmpdir.py:25–155  ·  view source on GitHub ↗

Factory for temporary directories under the common base temp directory. The base directory can be configured using the ``--basetemp`` option.

Source from the content-addressed store, hash-verified

23@final
24@attr.s(init=False)
25class TempPathFactory:
26 """Factory for temporary directories under the common base temp directory.
27
28 The base directory can be configured using the ``--basetemp`` option.
29 """
30
31 _given_basetemp = attr.ib(type=Optional[Path])
32 _trace = attr.ib()
33 _basetemp = attr.ib(type=Optional[Path])
34
35 def __init__(
36 self,
37 given_basetemp: Optional[Path],
38 trace,
39 basetemp: Optional[Path] = None,
40 *,
41 _ispytest: bool = False,
42 ) -> None:
43 check_ispytest(_ispytest)
44 if given_basetemp is None:
45 self._given_basetemp = None
46 else:
47 # Use os.path.abspath() to get absolute path instead of resolve() as it
48 # does not work the same in all platforms (see #4427).
49 # Path.absolute() exists, but it is not public (see https://bugs.python.org/issue25012).
50 self._given_basetemp = Path(os.path.abspath(str(given_basetemp)))
51 self._trace = trace
52 self._basetemp = basetemp
53
54 @classmethod
55 def from_config(
56 cls,
57 config: Config,
58 *,
59 _ispytest: bool = False,
60 ) -> "TempPathFactory":
61 """Create a factory according to pytest configuration.
62
63 :meta private:
64 """
65 check_ispytest(_ispytest)
66 return cls(
67 given_basetemp=config.option.basetemp,
68 trace=config.trace.get("tmpdir"),
69 _ispytest=True,
70 )
71
72 def _ensure_relative_to_basetemp(self, basename: str) -> str:
73 basename = os.path.normpath(basename)
74 if (self.getbasetemp() / basename).resolve().parent != self.getbasetemp():
75 raise ValueError(f"{basename} is not a normalized and relative path")
76 return basename
77
78 def mktemp(self, basename: str, numbered: bool = True) -> Path:
79 """Create a new temporary directory managed by the factory.
80
81 :param basename:
82 Directory base name, must be a relative path.

Calls

no outgoing calls