use tempfile.TemporaryDirectory to cleanup garbage- dir

Python has a very nice rm_rf hidden in
tempfile.TemporaryDirectory._rmtree we can use it to cleanup our
"garbage-" dir
3d43f1dce3/Lib/tempfile.py (L784-L812)
This commit is contained in:
Thomas Grainger 2020-10-26 12:49:35 +00:00
parent 096d096539
commit ceea364afb
No known key found for this signature in database
GPG Key ID: E452A1247BAC1A88
3 changed files with 26 additions and 6 deletions

View File

@ -0,0 +1 @@
Fixed an issue where dirs without S_IXUSR prevented pytest from cleaning up the temp dir

View File

@ -6,7 +6,7 @@ import itertools
import os
import shutil
import sys
import uuid
import tempfile
import warnings
from enum import Enum
from functools import partial
@ -254,11 +254,10 @@ def maybe_delete_a_numbered_dir(path: Path) -> None:
lock_path = None
try:
lock_path = create_cleanup_lock(path)
parent = path.parent
garbage = parent.joinpath(f"garbage-{uuid.uuid4()}")
path.rename(garbage)
rm_rf(garbage)
with tempfile.TemporaryDirectory(
prefix="garbage-", dir=path.parent
) as garbage:
path.replace(garbage)
except OSError:
# known races:
# * other process did a cleanup at the same time

View File

@ -351,6 +351,26 @@ def test_long_path_during_cleanup(tmp_path):
assert not os.path.isdir(extended_path)
def test_permission_denied_during_cleanup(tmp_path):
"""
Ensure that deleting a numbered dir does not fail because of missing file
permission bits (#7940).
"""
path = tmp_path / "temp-1"
p = path / "ham" / "spam" / "eggs"
p.parent.mkdir(parents=True)
p.touch(mode=0)
for p in p.parents:
if p == path:
break
p.chmod(mode=0)
lock_path = get_lock_path(path)
maybe_delete_a_numbered_dir(path)
assert not path.exists()
assert not lock_path.is_file()
def test_get_extended_length_path_str():
assert get_extended_length_path_str(r"c:\foo") == r"\\?\c:\foo"
assert get_extended_length_path_str(r"\\share\foo") == r"\\?\UNC\share\foo"