Skip to content

Commit 09b0e20

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 09b0e20

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

changelog/8940.bugfix.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed an issue where dirs without S_IXUSR prevented pytest from cleaning up the temp dir

src/_pytest/pathlib.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import shutil
88
import sys
9-
import uuid
9+
import tempfile
1010
import warnings
1111
from enum import Enum
1212
from functools import partial
@@ -254,11 +254,10 @@ def maybe_delete_a_numbered_dir(path: Path) -> None:
254254
lock_path = None
255255
try:
256256
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)
257+
with tempfile.TemporaryDirectory(
258+
prefix="garbage-", dir=os.fsdecode(path.parent)
259+
) as garbage:
260+
path.replace(garbage)
262261
except OSError:
263262
# known races:
264263
# * 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)