From a0b5dbc11bd79c9046085d52e6978094554574ce Mon Sep 17 00:00:00 2001 From: Yusuke Kadowaki Date: Tue, 29 Nov 2022 00:36:14 +0900 Subject: [PATCH] Add integration tests for tmpdir --- testing/test_tmpdir.py | 73 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 57f442b04..38048731e 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -92,6 +92,74 @@ class TestConfigTmpPath: assert mytemp.exists() assert not mytemp.joinpath("hello").exists() + def test_policy_none_delete_all(self, pytester: Pytester) -> None: + p = pytester.makepyfile( + """ + def test_1(tmp_path): + assert 0 == 0 + """ + ) + p_failed = pytester.makepyfile( + """ + def test_1(tmp_path): + assert 0 == 1 + """ + ) + pytester.makepyprojecttoml( + """ + [tool.pytest.ini_options] + tmp_path_retention_policy = "none" + """ + ) + + pytester.inline_run(p) + pytester.inline_run(p_failed) + + root = pytester._test_tmproot + for child in root.iterdir(): + base_dir = filter(lambda x: x.is_dir(), child.iterdir()) + # Check the base dir itself is gone without depending on test results + assert len(list(base_dir)) == 0 + + @pytest.mark.parametrize("policy", ["failed", "all"]) + @pytest.mark.parametrize("count", [0, 1, 3]) + def test_retention_count(self, pytester: Pytester, policy, count) -> None: + p = pytester.makepyfile( + """ + def test_1(tmp_path): + assert 0 == 0 + """ + ) + p_failed = pytester.makepyfile( + """ + def test_1(tmp_path): + assert 0 == 1 + """ + ) + + pytester.makepyprojecttoml( + f""" + [tool.pytest.ini_options] + tmp_path_retention_policy = {policy} + tmp_path_retention_count = {count} + """ + ) + + pytester.inline_run(p) + pytester.inline_run(p_failed) + pytester.inline_run(p) + pytester.inline_run(p_failed) + pytester.inline_run(p) + pytester.inline_run(p_failed) + pytester.inline_run(p) + pytester.inline_run(p_failed) + + root = pytester._test_tmproot + for child in root.iterdir(): + base_dir = filter(lambda x: not x.is_symlink(), child.iterdir()) + # Check the base dir itself is gone + assert len(list(base_dir)) == count + def test_policy_failed_removes_only_passed_dir(self, pytester: Pytester) -> None: p = pytester.makepyfile( """ @@ -132,10 +200,7 @@ class TestConfigTmpPath: pytester.inline_run(p) root = pytester._test_tmproot for child in root.iterdir(): - # This symlink will be deleted by cleanup_numbered_dir **after** - # the test finishes because it's triggered by atexit. - # So it has to be ignored here. - base_dir = filter(lambda x: not x.is_symlink(), child.iterdir()) + base_dir = filter(lambda x: x.is_dir(), child.iterdir()) # Check the base dir itself is gone assert len(list(base_dir)) == 0