Skip to content
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

Change the default tmp_path_retention_policy to all #10609

Merged
merged 1 commit into from
Dec 25, 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
1 change: 0 additions & 1 deletion changelog/8141.feature.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Added :confval:`tmp_path_retention_count` and :confval:`tmp_path_retention_policy` configuration options to control how directories created by the :fixture:`tmp_path` fixture are kept.
The default behavior has changed to keep only directories for failed tests, equivalent to `tmp_path_retention_policy="failed"`.
3 changes: 1 addition & 2 deletions doc/en/how-to/tmp_path.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ The default base temporary directory
Temporary directories are by default created as sub-directories of
the system temporary directory. The base name will be ``pytest-NUM`` where
``NUM`` will be incremented with each test run.
By default, only the directories of failed tests will be kept.
Also only the last 3 directries will remain at most.
By default, entries older than 3 temporary directories will be removed.
This behavior can be configured with :confval:`tmp_path_retention_count` and
:confval:`tmp_path_retention_policy`.

Expand Down
2 changes: 1 addition & 1 deletion doc/en/reference/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ passed multiple times. The expected format is ``name=value``. For example::
[pytest]
tmp_path_retention_policy = "all"

Default: failed
Default: all


.. confval:: usefixtures
Expand Down
6 changes: 3 additions & 3 deletions src/_pytest/tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def pytest_addoption(parser: Parser) -> None:
"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="failed",
default="all",
)


Expand Down Expand Up @@ -267,8 +267,8 @@ def tmp_path(
directory.

By default, a new base temporary directory is created each test session,
and only the base of failed session is kept. Also it only keeps the last 3 bases
at most. This can be configured with :confval:`tmp_path_retention_count` and
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`.
Expand Down
27 changes: 23 additions & 4 deletions testing/test_tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def getini(self, name):
if name == "tmp_path_retention_count":
return 3
elif name == "tmp_path_retention_policy":
return "failed"
return "all"
else:
assert False

Expand Down Expand Up @@ -101,6 +101,12 @@ def test_2(tmp_path):
assert 0 == 1
"""
)
pytester.makepyprojecttoml(
"""
[tool.pytest.ini_options]
tmp_path_retention_policy = "failed"
"""
)

pytester.inline_run(p)
root = pytester._test_tmproot
Expand Down Expand Up @@ -128,6 +134,12 @@ def test_1(tmp_path):
assert 0 == 0
"""
)
pytester.makepyprojecttoml(
"""
[tool.pytest.ini_options]
tmp_path_retention_policy = "failed"
"""
)

pytester.inline_run(p)
root = pytester._test_tmproot
Expand Down Expand Up @@ -155,6 +167,13 @@ def test_fixt(fixt):
pass
"""
)
pytester.makepyprojecttoml(
"""
[tool.pytest.ini_options]
tmp_path_retention_policy = "failed"
"""
)

pytester.inline_run(p)

# Check if the whole directory is removed
Expand Down Expand Up @@ -570,7 +589,7 @@ def test_tmp_path_factory_create_directory_with_safe_permissions(
"""Verify that pytest creates directories under /tmp with private permissions."""
# Use the test's tmp_path as the system temproot (/tmp).
monkeypatch.setenv("PYTEST_DEBUG_TEMPROOT", str(tmp_path))
tmp_factory = TempPathFactory(None, 3, "failed", lambda *args: None, _ispytest=True)
tmp_factory = TempPathFactory(None, 3, "all", lambda *args: None, _ispytest=True)
basetemp = tmp_factory.getbasetemp()

# No world-readable permissions.
Expand All @@ -590,14 +609,14 @@ def test_tmp_path_factory_fixes_up_world_readable_permissions(
"""
# Use the test's tmp_path as the system temproot (/tmp).
monkeypatch.setenv("PYTEST_DEBUG_TEMPROOT", str(tmp_path))
tmp_factory = TempPathFactory(None, 3, "failed", lambda *args: None, _ispytest=True)
tmp_factory = TempPathFactory(None, 3, "all", lambda *args: None, _ispytest=True)
basetemp = tmp_factory.getbasetemp()

# Before - simulate bad perms.
os.chmod(basetemp.parent, 0o777)
assert (basetemp.parent.stat().st_mode & 0o077) != 0

tmp_factory = TempPathFactory(None, 3, "failed", lambda *args: None, _ispytest=True)
tmp_factory = TempPathFactory(None, 3, "all", lambda *args: None, _ispytest=True)
basetemp = tmp_factory.getbasetemp()

# After - fixed.
Expand Down