1
+ import os
2
+ import stat
1
3
import sys
2
4
3
5
import attr
@@ -326,6 +328,35 @@ def test_rmtree(self, tmp_path):
326
328
rmtree (adir , force = True )
327
329
assert not adir .exists ()
328
330
331
+ def test_rmtree_with_read_only_file (self , tmp_path ):
332
+ """Ensure rmtree can remove directories with read-only files in them (#5524)"""
333
+ from _pytest .pathlib import rmtree
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_IREAD )
342
+ rmtree (fn .parent , force = True )
343
+
344
+ assert not fn .parent .is_dir ()
345
+
346
+ def test_rmtree_with_read_only_directory (self , tmp_path ):
347
+ """Ensure rmtree can remove read-only directories (#5524)"""
348
+ from _pytest .pathlib import rmtree
349
+
350
+ adir = tmp_path / "dir"
351
+ adir .mkdir ()
352
+
353
+ (adir / "foo.txt" ).touch ()
354
+ mode = os .stat (str (adir )).st_mode
355
+ os .chmod (str (adir ), mode & ~ stat .S_IREAD )
356
+ rmtree (adir , force = True )
357
+
358
+ assert not adir .is_dir ()
359
+
329
360
def test_cleanup_ignores_symlink (self , tmp_path ):
330
361
the_symlink = tmp_path / (self .PREFIX + "current" )
331
362
attempt_symlink_to (the_symlink , tmp_path / (self .PREFIX + "5" ))
@@ -349,3 +380,24 @@ def attempt_symlink_to(path, to_path):
349
380
350
381
def test_tmpdir_equals_tmp_path (tmpdir , tmp_path ):
351
382
assert Path (tmpdir ) == tmp_path
383
+
384
+
385
+ def test_basetemp_with_read_only_files (testdir ):
386
+ """Integration test for #5524"""
387
+ testdir .makepyfile (
388
+ """
389
+ import os
390
+ import stat
391
+
392
+ def test(tmp_path):
393
+ fn = tmp_path / 'foo.txt'
394
+ fn.write_text('hello')
395
+ mode = os.stat(str(fn)).st_mode
396
+ os.chmod(str(fn), mode & ~stat.S_IREAD)
397
+ """
398
+ )
399
+ result = testdir .runpytest ("--basetemp=tmp" )
400
+ assert result .ret == 0
401
+ # running a second time and ensure we don't crash
402
+ result = testdir .runpytest ("--basetemp=tmp" )
403
+ assert result .ret == 0
0 commit comments