Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
62 lines (48 loc) · 1.94 KB
/
Copy pathconftest.py
File metadata and controls
62 lines (48 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES.
# All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""Release successful owned module fixtures after their complete test outcome."""
from __future__ import annotations
import shutil
from collections.abc import Callable, Iterator
from dataclasses import dataclass, field
from pathlib import Path
import pytest
@dataclass
class _ModuleDirs:
paths: list[Path] = field(default_factory=list)
finished: bool = False
failed: bool = False
_OWNED = pytest.StashKey[dict[Path, _ModuleDirs]]()
@pytest.fixture(scope='module')
def owned_module_dir(
request: pytest.FixtureRequest, tmp_path_factory: pytest.TempPathFactory
) -> Iterator[Callable[[str], Path]]:
"""Own only the allocated roots; retain their Git objects after any failure."""
modules = request.config.stash.setdefault(_OWNED, {})
owned = modules.setdefault(request.node.path, _ModuleDirs())
def allocate(prefix: str) -> Path:
path = tmp_path_factory.mktemp(prefix)
owned.paths.append(path)
return path
try:
yield allocate
finally:
# The last consumer's teardown report does not exist yet. In
# particular, a later finalizer can still report a teardown failure.
owned.finished = True
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item: pytest.Item) -> Iterator[None]:
outcome = yield
report = outcome.get_result()
modules = item.config.stash.get(_OWNED, {})
owned = modules.get(item.path)
if owned is None:
return
owned.failed |= report.failed
if report.when == 'teardown' and owned.finished and not owned.failed:
# Every dependent setup, call and teardown has now reported. Do not
# follow symlinks or Git alternates outside these exact owned roots.
for path in owned.paths:
shutil.rmtree(path)
del modules[item.path]