Skip to content

Fix tmp_path_retention_policy crash when skipping from fixture #10517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/_pytest/tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import tempfile
from pathlib import Path
from shutil import rmtree
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.stash import StashKey

if TYPE_CHECKING:
from typing_extensions import Literal

Expand All @@ -33,6 +37,8 @@
from _pytest.fixtures import FixtureRequest
from _pytest.monkeypatch import MonkeyPatch

tmppath_result_key = StashKey[Dict[str, bool]]()


@final
@attr.s(init=False)
Expand Down Expand Up @@ -273,11 +279,15 @@ def tmp_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
if policy == "failed" and request.node._tmp_path_result_call.passed:
result_dict = request.node.stash[tmppath_result_key]

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)

del request.node.stash[tmppath_result_key]

# remove dead symlink
basetemp = tmp_path_factory._basetemp
if basetemp is None:
Expand Down Expand Up @@ -306,7 +316,11 @@ def pytest_sessionfinish(session, exitstatus: Union[int, ExitCode]):


@hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
def pytest_runtest_makereport(item: Item, call):
outcome = yield
result = outcome.get_result()
setattr(item, "_tmp_path_result_" + result.when, result)

if tmppath_result_key not in item.stash:
item.stash[tmppath_result_key] = {result.when: result.passed}
else:
item.stash[tmppath_result_key][result.when] = result.passed
64 changes: 64 additions & 0 deletions testing/test_tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,70 @@ def test_1(tmp_path):
# Check the base dir itself is gone
assert len(list(base_dir)) == 0

# issue #10502
def test_policy_failed_removes_dir_when_skipped_from_fixture(
self, pytester: Pytester
) -> None:
p = pytester.makepyfile(
"""
import pytest

@pytest.fixture
def fixt(tmp_path):
pytest.skip()

def test_fixt(fixt):
pass
"""
)
pytester.inline_run(p)

# Check if the whole directory is removed
root = pytester._test_tmproot
for child in root.iterdir():
base_dir = list(
filter(lambda x: x.is_dir() and not x.is_symlink(), child.iterdir())
)
assert len(base_dir) == 0

# issue #10502
def test_policy_all_keeps_dir_when_skipped_from_fixture(
self, pytester: Pytester
) -> None:
p = pytester.makepyfile(
"""
import pytest

@pytest.fixture
def fixt(tmp_path):
pytest.skip()

def test_fixt(fixt):
pass
"""
)
pytester.makepyprojecttoml(
"""
[tool.pytest.ini_options]
tmp_path_retention_policy = "all"
"""
)
pytester.inline_run(p)

# Check if the whole directory is kept
root = pytester._test_tmproot
for child in root.iterdir():
base_dir = list(
filter(lambda x: x.is_dir() and not x.is_symlink(), child.iterdir())
)
assert len(base_dir) == 1
test_dir = list(
filter(
lambda x: x.is_dir() and not x.is_symlink(), base_dir[0].iterdir()
)
)
assert len(test_dir) == 1


testdata = [
("mypath", True),
Expand Down