From e4cf09d8cb03775a1b8598bc3e15898ab2d18b89 Mon Sep 17 00:00:00 2001 From: Yusuke Kadowaki Date: Wed, 23 Nov 2022 17:57:05 +0900 Subject: [PATCH] Update tests to check if the directory is removed or kept depending on the policy --- testing/test_tmpdir.py | 53 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 779f58c66..2f6f7a873 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -12,7 +12,6 @@ import attr import pytest from _pytest import pathlib from _pytest.config import Config -from _pytest.config import ExitCode from _pytest.monkeypatch import MonkeyPatch from _pytest.pathlib import cleanup_numbered_dir from _pytest.pathlib import create_cleanup_lock @@ -141,7 +140,9 @@ class TestConfigTmpPath: assert len(list(base_dir)) == 0 # issue #10502 - def test_delete_dir_when_skipped_from_fixture(self, pytester: Pytester) -> None: + def test_policy_failed_removes_dir_when_skipped_from_fixture( + self, pytester: Pytester + ) -> None: p = pytester.makepyfile( """ import pytest @@ -154,9 +155,53 @@ class TestConfigTmpPath: pass """ ) + pytester.inline_run(p) - reprec = pytester.inline_run(p) - assert reprec.ret == ExitCode.OK + # 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_none_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 = "none" + """ + ) + 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 = [