Skip to content

Commit 0f327ba

Browse files
committed
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 https://github.com/python/cpython/blob/3d43f1dce3e9aaded38f9a2c73e3c66acf85505c/Lib/tempfile.py#L784-L812
1 parent 65e6e39 commit 0f327ba

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/_pytest/pathlib.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import shutil
88
import sys
9+
import tempfile
910
import uuid
1011
import warnings
1112
from enum import Enum
@@ -254,11 +255,10 @@ def maybe_delete_a_numbered_dir(path: Path) -> None:
254255
lock_path = None
255256
try:
256257
lock_path = create_cleanup_lock(path)
257-
parent = path.parent
258-
259-
garbage = parent.joinpath(f"garbage-{uuid.uuid4()}")
260-
path.rename(garbage)
261-
rm_rf(garbage)
258+
with tempfile.TemporaryDirectory(
259+
prefix="garbage-", dir=os.fsdecode(path.parent)
260+
) as garbage:
261+
path.replace(garbage)
262262
except OSError:
263263
# known races:
264264
# * other process did a cleanup at the same time

testing/test_tmpdir.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,29 @@ def test(tmp_path):
435435
fn.write_text('hello')
436436
mode = os.stat(str(fn)).st_mode
437437
os.chmod(str(fn), mode & ~stat.S_IREAD)
438-
"""
438+
"""
439439
)
440440
result = testdir.runpytest("--basetemp=tmp")
441441
assert result.ret == 0
442442
# running a second time and ensure we don't crash
443443
result = testdir.runpytest("--basetemp=tmp")
444444
assert result.ret == 0
445+
446+
447+
def test_basetemp_with_not_executable_dirs(testdir):
448+
"""Integration test for #7940"""
449+
testdir.makepyfile(
450+
"""
451+
def test(tmp_path):
452+
p = tmp_path / "ham" / "spam" / "eggs"
453+
p.parent.mkdir(parents=True)
454+
p.touch(mode=0)
455+
for p in p.parents:
456+
if p == tmp_path:
457+
break
458+
p.chmod(mode=0)
459+
"""
460+
)
461+
assert testdir.runpytest("--basetemp=tmp").ret == 0
462+
# running a second time and ensure we don't crash
463+
assert testdir.runpytest("--basetemp=tmp").ret == 0

0 commit comments

Comments
 (0)