From 321eeae6d1192c722656d9f40fca9eb258c7a4da Mon Sep 17 00:00:00 2001 From: Yusuke Kadowaki Date: Thu, 27 Oct 2022 21:39:30 +0900 Subject: [PATCH] Refactor sessionfinish --- src/_pytest/tmpdir.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/_pytest/tmpdir.py b/src/_pytest/tmpdir.py index c11c92c84..c43dfa849 100644 --- a/src/_pytest/tmpdir.py +++ b/src/_pytest/tmpdir.py @@ -232,20 +232,22 @@ def tmp_path(request: FixtureRequest, tmp_path_factory: TempPathFactory) -> Path def pytest_sessionfinish(session, exitstatus: Union[int, ExitCode]): + """After each session, remove base directory if all the tests passed, + the policy is "failed", and the basetemp is not specified by a user. + """ tmp_path_factory: TempPathFactory = session.config._tmp_path_factory + if tmp_path_factory._basetemp is None: + return policy = tmp_path_factory._retention_policy - if ( exitstatus == 0 and policy == "failed" and tmp_path_factory._given_basetemp is None ): - # Register to remove the base directory before starting cleanup_numbered_dir - if tmp_path_factory._basetemp is None: - return def cleanup_passed_dir(passed_dir: Path): if passed_dir.exists(): rmtree(passed_dir) + # Register to remove the base directory before starting cleanup_numbered_dir atexit.register(cleanup_passed_dir, tmp_path_factory._basetemp)