This commit is contained in:
Iliyan Angelov
2025-12-06 03:27:35 +02:00
parent 7667eb5eda
commit 5a8ca3c475
2211 changed files with 28086 additions and 37066 deletions

View File

@@ -1,63 +1,68 @@
# mypy: allow-untyped-defs
"""Support for providing temporary directories to test functions."""
from __future__ import annotations
from collections.abc import Generator
import dataclasses
import os
from pathlib import Path
import re
from shutil import rmtree
import tempfile
from pathlib import Path
from shutil import rmtree
from typing import Any
from typing import final
from typing import Literal
from typing import Dict
from typing import Generator
from typing import Optional
from typing import TYPE_CHECKING
from typing import Union
from _pytest.nodes import Item
from _pytest.reports import CollectReport
from _pytest.stash import StashKey
if TYPE_CHECKING:
from typing_extensions import Literal
RetentionType = Literal["all", "failed", "none"]
from _pytest.config.argparsing import Parser
from .pathlib import cleanup_dead_symlinks
from .pathlib import LOCK_TIMEOUT
from .pathlib import make_numbered_dir
from .pathlib import make_numbered_dir_with_cleanup
from .pathlib import rm_rf
from _pytest.compat import get_user_id
from .pathlib import cleanup_dead_symlinks
from _pytest.compat import final, get_user_id
from _pytest.config import Config
from _pytest.config import ExitCode
from _pytest.config import hookimpl
from _pytest.config.argparsing import Parser
from _pytest.deprecated import check_ispytest
from _pytest.fixtures import fixture
from _pytest.fixtures import FixtureRequest
from _pytest.monkeypatch import MonkeyPatch
from _pytest.nodes import Item
from _pytest.reports import TestReport
from _pytest.stash import StashKey
tmppath_result_key = StashKey[dict[str, bool]]()
RetentionType = Literal["all", "failed", "none"]
tmppath_result_key = StashKey[Dict[str, bool]]()
@final
@dataclasses.dataclass
class TempPathFactory:
"""Factory for temporary directories under the common base temp directory,
as discussed at :ref:`temporary directory location and retention`.
"""Factory for temporary directories under the common base temp directory.
The base directory can be configured using the ``--basetemp`` option.
"""
_given_basetemp: Path | None
_given_basetemp: Optional[Path]
# pluggy TagTracerSub, not currently exposed, so Any.
_trace: Any
_basetemp: Path | None
_basetemp: Optional[Path]
_retention_count: int
_retention_policy: RetentionType
_retention_policy: "RetentionType"
def __init__(
self,
given_basetemp: Path | None,
given_basetemp: Optional[Path],
retention_count: int,
retention_policy: RetentionType,
retention_policy: "RetentionType",
trace,
basetemp: Path | None = None,
basetemp: Optional[Path] = None,
*,
_ispytest: bool = False,
) -> None:
@@ -80,7 +85,7 @@ class TempPathFactory:
config: Config,
*,
_ispytest: bool = False,
) -> TempPathFactory:
) -> "TempPathFactory":
"""Create a factory according to pytest configuration.
:meta private:
@@ -196,7 +201,7 @@ class TempPathFactory:
return basetemp
def get_user() -> str | None:
def get_user() -> Optional[str]:
"""Return the current user name, or None if getuser() does not work
in the current environment (see #1010)."""
try:
@@ -204,7 +209,7 @@ def get_user() -> str | None:
import getpass
return getpass.getuser()
except (ImportError, OSError, KeyError):
except (ImportError, KeyError):
return None
@@ -254,17 +259,26 @@ def _mk_tmp(request: FixtureRequest, factory: TempPathFactory) -> Path:
@fixture
def tmp_path(
request: FixtureRequest, tmp_path_factory: TempPathFactory
) -> Generator[Path]:
"""Return a temporary directory (as :class:`pathlib.Path` object)
which is unique to each test function invocation.
The temporary directory is created as a subdirectory
of the base temporary directory, with configurable retention,
as discussed in :ref:`temporary directory location and retention`.
) -> Generator[Path, None, None]:
"""Return a temporary directory path object which is unique to each test
function invocation, created as a sub directory of the base temporary
directory.
By default, a new base temporary directory is created each test session,
and old bases are removed after 3 sessions, to aid in debugging.
This behavior can be configured with :confval:`tmp_path_retention_count` and
:confval:`tmp_path_retention_policy`.
If ``--basetemp`` is used then it is cleared each session. See :ref:`base
temporary directory`.
The returned object is a :class:`pathlib.Path` object.
"""
path = _mk_tmp(request, tmp_path_factory)
yield path
# Remove the tmpdir if the policy is "failed" and the test passed.
tmp_path_factory: TempPathFactory = request.session.config._tmp_path_factory # type: ignore
policy = tmp_path_factory._retention_policy
result_dict = request.node.stash[tmppath_result_key]
@@ -276,7 +290,7 @@ def tmp_path(
del request.node.stash[tmppath_result_key]
def pytest_sessionfinish(session, exitstatus: int | ExitCode):
def pytest_sessionfinish(session, exitstatus: Union[int, ExitCode]):
"""After each session, remove base directory if all the tests passed,
the policy is "failed", and the basetemp is not specified by a user.
"""
@@ -301,12 +315,10 @@ def pytest_sessionfinish(session, exitstatus: int | ExitCode):
cleanup_dead_symlinks(basetemp)
@hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_makereport(
item: Item, call
) -> Generator[None, TestReport, TestReport]:
rep = yield
assert rep.when is not None
empty: dict[str, bool] = {}
item.stash.setdefault(tmppath_result_key, empty)[rep.when] = rep.passed
return rep
@hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item: Item, call):
outcome = yield
result: CollectReport = outcome.get_result()
empty: Dict[str, bool] = {}
item.stash.setdefault(tmppath_result_key, empty)[result.when] = result.passed