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:
parent
096d096539
commit
ceea364afb
|
@ -0,0 +1 @@
|
||||||
|
Fixed an issue where dirs without S_IXUSR prevented pytest from cleaning up the temp dir
|
|
@ -6,7 +6,7 @@ import itertools
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import uuid
|
import tempfile
|
||||||
import warnings
|
import warnings
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
@ -254,11 +254,10 @@ def maybe_delete_a_numbered_dir(path: Path) -> None:
|
||||||
lock_path = None
|
lock_path = None
|
||||||
try:
|
try:
|
||||||
lock_path = create_cleanup_lock(path)
|
lock_path = create_cleanup_lock(path)
|
||||||
parent = path.parent
|
with tempfile.TemporaryDirectory(
|
||||||
|
prefix="garbage-", dir=path.parent
|
||||||
garbage = parent.joinpath(f"garbage-{uuid.uuid4()}")
|
) as garbage:
|
||||||
path.rename(garbage)
|
path.replace(garbage)
|
||||||
rm_rf(garbage)
|
|
||||||
except OSError:
|
except OSError:
|
||||||
# known races:
|
# known races:
|
||||||
# * other process did a cleanup at the same time
|
# * other process did a cleanup at the same time
|
||||||
|
|
|
@ -351,6 +351,26 @@ def test_long_path_during_cleanup(tmp_path):
|
||||||
assert not os.path.isdir(extended_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():
|
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"c:\foo") == r"\\?\c:\foo"
|
||||||
assert get_extended_length_path_str(r"\\share\foo") == r"\\?\UNC\share\foo"
|
assert get_extended_length_path_str(r"\\share\foo") == r"\\?\UNC\share\foo"
|
||||||
|
|
Loading…
Reference in New Issue