1
+ import os
2
+ import stat
1
3
import sys
2
4
3
5
import attr
@@ -311,21 +313,52 @@ def test_cleanup_locked(self, tmp_path):
311
313
)
312
314
313
315
def test_rmtree (self , tmp_path ):
314
- from _pytest .pathlib import rmtree
316
+ from _pytest .pathlib import rm_rf
315
317
316
318
adir = tmp_path / "adir"
317
319
adir .mkdir ()
318
- rmtree (adir )
320
+ rm_rf (adir )
319
321
320
322
assert not adir .exists ()
321
323
322
324
adir .mkdir ()
323
325
afile = adir / "afile"
324
326
afile .write_bytes (b"aa" )
325
327
326
- rmtree (adir , force = True )
328
+ rm_rf (adir )
327
329
assert not adir .exists ()
328
330
331
+ def test_rmtree_with_read_only_file (self , tmp_path ):
332
+ """Ensure rm_rf can remove directories with read-only files in them (#5524)"""
333
+ from _pytest .pathlib import rm_rf
334
+
335
+ fn = tmp_path / "dir/foo.txt"
336
+ fn .parent .mkdir ()
337
+
338
+ fn .touch ()
339
+
340
+ mode = os .stat (str (fn )).st_mode
341
+ os .chmod (str (fn ), mode & ~ stat .S_IWRITE )
342
+
343
+ rm_rf (fn .parent )
344
+
345
+ assert not fn .parent .is_dir ()
346
+
347
+ def test_rmtree_with_read_only_directory (self , tmp_path ):
348
+ """Ensure rm_rf can remove read-only directories (#5524)"""
349
+ from _pytest .pathlib import rm_rf
350
+
351
+ adir = tmp_path / "dir"
352
+ adir .mkdir ()
353
+
354
+ (adir / "foo.txt" ).touch ()
355
+ mode = os .stat (str (adir )).st_mode
356
+ os .chmod (str (adir ), mode & ~ stat .S_IWRITE )
357
+
358
+ rm_rf (adir )
359
+
360
+ assert not adir .is_dir ()
361
+
329
362
def test_cleanup_ignores_symlink (self , tmp_path ):
330
363
the_symlink = tmp_path / (self .PREFIX + "current" )
331
364
attempt_symlink_to (the_symlink , tmp_path / (self .PREFIX + "5" ))
@@ -349,3 +382,24 @@ def attempt_symlink_to(path, to_path):
349
382
350
383
def test_tmpdir_equals_tmp_path (tmpdir , tmp_path ):
351
384
assert Path (tmpdir ) == tmp_path
385
+
386
+
387
+ def test_basetemp_with_read_only_files (testdir ):
388
+ """Integration test for #5524"""
389
+ testdir .makepyfile (
390
+ """
391
+ import os
392
+ import stat
393
+
394
+ def test(tmp_path):
395
+ fn = tmp_path / 'foo.txt'
396
+ fn.write_text('hello')
397
+ mode = os.stat(str(fn)).st_mode
398
+ os.chmod(str(fn), mode & ~stat.S_IREAD)
399
+ """
400
+ )
401
+ result = testdir .runpytest ("--basetemp=tmp" )
402
+ assert result .ret == 0
403
+ # running a second time and ensure we don't crash
404
+ result = testdir .runpytest ("--basetemp=tmp" )
405
+ assert result .ret == 0
0 commit comments