mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 04:34:16 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -1,11 +1,13 @@
|
|||
# mypy: allow-untyped-defs
|
||||
"""Support for providing temporary directories to test functions."""
|
||||
from __future__ import annotations
|
||||
|
||||
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 Dict
|
||||
from typing import final
|
||||
|
|
@ -14,11 +16,6 @@ from typing import Literal
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
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 _pytest.config import Config
|
||||
from _pytest.config import ExitCode
|
||||
|
|
@ -32,9 +29,15 @@ from _pytest.nodes import Item
|
|||
from _pytest.reports import TestReport
|
||||
from _pytest.stash import StashKey
|
||||
|
||||
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
|
||||
|
||||
|
||||
tmppath_result_key = StashKey[Dict[str, bool]]()
|
||||
RetentionType = Literal["all", "failed", "none"]
|
||||
RetentionType = Literal['all', 'failed', 'none']
|
||||
|
||||
|
||||
@final
|
||||
|
|
@ -45,20 +48,20 @@ class TempPathFactory:
|
|||
The base directory can be configured using the ``--basetemp`` option.
|
||||
"""
|
||||
|
||||
_given_basetemp: Optional[Path]
|
||||
_given_basetemp: Path | None
|
||||
# pluggy TagTracerSub, not currently exposed, so Any.
|
||||
_trace: Any
|
||||
_basetemp: Optional[Path]
|
||||
_basetemp: Path | None
|
||||
_retention_count: int
|
||||
_retention_policy: RetentionType
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
given_basetemp: Optional[Path],
|
||||
given_basetemp: Path | None,
|
||||
retention_count: int,
|
||||
retention_policy: RetentionType,
|
||||
trace,
|
||||
basetemp: Optional[Path] = None,
|
||||
basetemp: Path | None = None,
|
||||
*,
|
||||
_ispytest: bool = False,
|
||||
) -> None:
|
||||
|
|
@ -81,27 +84,27 @@ class TempPathFactory:
|
|||
config: Config,
|
||||
*,
|
||||
_ispytest: bool = False,
|
||||
) -> "TempPathFactory":
|
||||
) -> TempPathFactory:
|
||||
"""Create a factory according to pytest configuration.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
check_ispytest(_ispytest)
|
||||
count = int(config.getini("tmp_path_retention_count"))
|
||||
count = int(config.getini('tmp_path_retention_count'))
|
||||
if count < 0:
|
||||
raise ValueError(
|
||||
f"tmp_path_retention_count must be >= 0. Current input: {count}."
|
||||
f'tmp_path_retention_count must be >= 0. Current input: {count}.',
|
||||
)
|
||||
|
||||
policy = config.getini("tmp_path_retention_policy")
|
||||
if policy not in ("all", "failed", "none"):
|
||||
policy = config.getini('tmp_path_retention_policy')
|
||||
if policy not in ('all', 'failed', 'none'):
|
||||
raise ValueError(
|
||||
f"tmp_path_retention_policy must be either all, failed, none. Current input: {policy}."
|
||||
f'tmp_path_retention_policy must be either all, failed, none. Current input: {policy}.',
|
||||
)
|
||||
|
||||
return cls(
|
||||
given_basetemp=config.option.basetemp,
|
||||
trace=config.trace.get("tmpdir"),
|
||||
trace=config.trace.get('tmpdir'),
|
||||
retention_count=count,
|
||||
retention_policy=policy,
|
||||
_ispytest=True,
|
||||
|
|
@ -110,7 +113,7 @@ class TempPathFactory:
|
|||
def _ensure_relative_to_basetemp(self, basename: str) -> str:
|
||||
basename = os.path.normpath(basename)
|
||||
if (self.getbasetemp() / basename).resolve().parent != self.getbasetemp():
|
||||
raise ValueError(f"{basename} is not a normalized and relative path")
|
||||
raise ValueError(f'{basename} is not a normalized and relative path')
|
||||
return basename
|
||||
|
||||
def mktemp(self, basename: str, numbered: bool = True) -> Path:
|
||||
|
|
@ -134,7 +137,7 @@ class TempPathFactory:
|
|||
p.mkdir(mode=0o700)
|
||||
else:
|
||||
p = make_numbered_dir(root=self.getbasetemp(), prefix=basename, mode=0o700)
|
||||
self._trace("mktemp", p)
|
||||
self._trace('mktemp', p)
|
||||
return p
|
||||
|
||||
def getbasetemp(self) -> Path:
|
||||
|
|
@ -153,17 +156,17 @@ class TempPathFactory:
|
|||
basetemp.mkdir(mode=0o700)
|
||||
basetemp = basetemp.resolve()
|
||||
else:
|
||||
from_env = os.environ.get("PYTEST_DEBUG_TEMPROOT")
|
||||
from_env = os.environ.get('PYTEST_DEBUG_TEMPROOT')
|
||||
temproot = Path(from_env or tempfile.gettempdir()).resolve()
|
||||
user = get_user() or "unknown"
|
||||
user = get_user() or 'unknown'
|
||||
# use a sub-directory in the temproot to speed-up
|
||||
# make_numbered_dir() call
|
||||
rootdir = temproot.joinpath(f"pytest-of-{user}")
|
||||
rootdir = temproot.joinpath(f'pytest-of-{user}')
|
||||
try:
|
||||
rootdir.mkdir(mode=0o700, exist_ok=True)
|
||||
except OSError:
|
||||
# getuser() likely returned illegal characters for the platform, use unknown back off mechanism
|
||||
rootdir = temproot.joinpath("pytest-of-unknown")
|
||||
rootdir = temproot.joinpath('pytest-of-unknown')
|
||||
rootdir.mkdir(mode=0o700, exist_ok=True)
|
||||
# Because we use exist_ok=True with a predictable name, make sure
|
||||
# we are the owners, to prevent any funny business (on unix, where
|
||||
|
|
@ -176,16 +179,16 @@ class TempPathFactory:
|
|||
rootdir_stat = rootdir.stat()
|
||||
if rootdir_stat.st_uid != uid:
|
||||
raise OSError(
|
||||
f"The temporary directory {rootdir} is not owned by the current user. "
|
||||
"Fix this and try again."
|
||||
f'The temporary directory {rootdir} is not owned by the current user. '
|
||||
'Fix this and try again.',
|
||||
)
|
||||
if (rootdir_stat.st_mode & 0o077) != 0:
|
||||
os.chmod(rootdir, rootdir_stat.st_mode & ~0o077)
|
||||
keep = self._retention_count
|
||||
if self._retention_policy == "none":
|
||||
if self._retention_policy == 'none':
|
||||
keep = 0
|
||||
basetemp = make_numbered_dir_with_cleanup(
|
||||
prefix="pytest-",
|
||||
prefix='pytest-',
|
||||
root=rootdir,
|
||||
keep=keep,
|
||||
lock_timeout=LOCK_TIMEOUT,
|
||||
|
|
@ -193,11 +196,11 @@ class TempPathFactory:
|
|||
)
|
||||
assert basetemp is not None, basetemp
|
||||
self._basetemp = basetemp
|
||||
self._trace("new basetemp", basetemp)
|
||||
self._trace('new basetemp', basetemp)
|
||||
return basetemp
|
||||
|
||||
|
||||
def get_user() -> Optional[str]:
|
||||
def get_user() -> str | None:
|
||||
"""Return the current user name, or None if getuser() does not work
|
||||
in the current environment (see #1010)."""
|
||||
try:
|
||||
|
|
@ -219,25 +222,25 @@ def pytest_configure(config: Config) -> None:
|
|||
mp = MonkeyPatch()
|
||||
config.add_cleanup(mp.undo)
|
||||
_tmp_path_factory = TempPathFactory.from_config(config, _ispytest=True)
|
||||
mp.setattr(config, "_tmp_path_factory", _tmp_path_factory, raising=False)
|
||||
mp.setattr(config, '_tmp_path_factory', _tmp_path_factory, raising=False)
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
parser.addini(
|
||||
"tmp_path_retention_count",
|
||||
help="How many sessions should we keep the `tmp_path` directories, according to `tmp_path_retention_policy`.",
|
||||
'tmp_path_retention_count',
|
||||
help='How many sessions should we keep the `tmp_path` directories, according to `tmp_path_retention_policy`.',
|
||||
default=3,
|
||||
)
|
||||
|
||||
parser.addini(
|
||||
"tmp_path_retention_policy",
|
||||
help="Controls which directories created by the `tmp_path` fixture are kept around, based on test outcome. "
|
||||
"(all/failed/none)",
|
||||
default="all",
|
||||
'tmp_path_retention_policy',
|
||||
help='Controls which directories created by the `tmp_path` fixture are kept around, based on test outcome. '
|
||||
'(all/failed/none)',
|
||||
default='all',
|
||||
)
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
@fixture(scope='session')
|
||||
def tmp_path_factory(request: FixtureRequest) -> TempPathFactory:
|
||||
"""Return a :class:`pytest.TempPathFactory` instance for the test session."""
|
||||
# Set dynamically by pytest_configure() above.
|
||||
|
|
@ -246,7 +249,7 @@ def tmp_path_factory(request: FixtureRequest) -> TempPathFactory:
|
|||
|
||||
def _mk_tmp(request: FixtureRequest, factory: TempPathFactory) -> Path:
|
||||
name = request.node.name
|
||||
name = re.sub(r"[\W]", "_", name)
|
||||
name = re.sub(r'[\W]', '_', name)
|
||||
MAXVAL = 30
|
||||
name = name[:MAXVAL]
|
||||
return factory.mktemp(name, numbered=True)
|
||||
|
|
@ -254,7 +257,7 @@ def _mk_tmp(request: FixtureRequest, factory: TempPathFactory) -> Path:
|
|||
|
||||
@fixture
|
||||
def tmp_path(
|
||||
request: FixtureRequest, tmp_path_factory: TempPathFactory
|
||||
request: FixtureRequest, tmp_path_factory: TempPathFactory,
|
||||
) -> 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
|
||||
|
|
@ -277,7 +280,7 @@ def tmp_path(
|
|||
policy = tmp_path_factory._retention_policy
|
||||
result_dict = request.node.stash[tmppath_result_key]
|
||||
|
||||
if policy == "failed" and result_dict.get("call", True):
|
||||
if policy == 'failed' and result_dict.get('call', True):
|
||||
# We do a "best effort" to remove files, but it might not be possible due to some leaked resource,
|
||||
# permissions, etc, in which case we ignore it.
|
||||
rmtree(path, ignore_errors=True)
|
||||
|
|
@ -285,7 +288,7 @@ def tmp_path(
|
|||
del request.node.stash[tmppath_result_key]
|
||||
|
||||
|
||||
def pytest_sessionfinish(session, exitstatus: Union[int, ExitCode]):
|
||||
def pytest_sessionfinish(session, exitstatus: 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.
|
||||
"""
|
||||
|
|
@ -296,9 +299,9 @@ def pytest_sessionfinish(session, exitstatus: Union[int, ExitCode]):
|
|||
|
||||
policy = tmp_path_factory._retention_policy
|
||||
if (
|
||||
exitstatus == 0
|
||||
and policy == "failed"
|
||||
and tmp_path_factory._given_basetemp is None
|
||||
exitstatus == 0 and
|
||||
policy == 'failed' and
|
||||
tmp_path_factory._given_basetemp is None
|
||||
):
|
||||
if basetemp.is_dir():
|
||||
# We do a "best effort" to remove files, but it might not be possible due to some leaked resource,
|
||||
|
|
@ -312,10 +315,10 @@ def pytest_sessionfinish(session, exitstatus: Union[int, ExitCode]):
|
|||
|
||||
@hookimpl(wrapper=True, tryfirst=True)
|
||||
def pytest_runtest_makereport(
|
||||
item: Item, call
|
||||
item: Item, call,
|
||||
) -> Generator[None, TestReport, TestReport]:
|
||||
rep = yield
|
||||
assert rep.when is not None
|
||||
empty: Dict[str, bool] = {}
|
||||
empty: dict[str, bool] = {}
|
||||
item.stash.setdefault(tmppath_result_key, empty)[rep.when] = rep.passed
|
||||
return rep
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue