Fix rmtree to remove directories with read-only files

Fix #5524
This commit is contained in:
Bruno Oliveira
2019-07-10 09:52:23 -03:00
committed by Bruno Oliveira
parent 2c402f4bd9
commit 37c37963c4
4 changed files with 97 additions and 14 deletions

View File

@@ -1,3 +1,5 @@
import os
import stat
import sys
import attr
@@ -311,11 +313,11 @@ class TestNumberedDir:
)
def test_rmtree(self, tmp_path):
from _pytest.pathlib import rmtree
from _pytest.pathlib import rm_rf
adir = tmp_path / "adir"
adir.mkdir()
rmtree(adir)
rm_rf(adir)
assert not adir.exists()
@@ -323,9 +325,40 @@ class TestNumberedDir:
afile = adir / "afile"
afile.write_bytes(b"aa")
rmtree(adir, force=True)
rm_rf(adir)
assert not adir.exists()
def test_rmtree_with_read_only_file(self, tmp_path):
"""Ensure rm_rf can remove directories with read-only files in them (#5524)"""
from _pytest.pathlib import rm_rf
fn = tmp_path / "dir/foo.txt"
fn.parent.mkdir()
fn.touch()
mode = os.stat(str(fn)).st_mode
os.chmod(str(fn), mode & ~stat.S_IWRITE)
rm_rf(fn.parent)
assert not fn.parent.is_dir()
def test_rmtree_with_read_only_directory(self, tmp_path):
"""Ensure rm_rf can remove read-only directories (#5524)"""
from _pytest.pathlib import rm_rf
adir = tmp_path / "dir"
adir.mkdir()
(adir / "foo.txt").touch()
mode = os.stat(str(adir)).st_mode
os.chmod(str(adir), mode & ~stat.S_IWRITE)
rm_rf(adir)
assert not adir.is_dir()
def test_cleanup_ignores_symlink(self, tmp_path):
the_symlink = tmp_path / (self.PREFIX + "current")
attempt_symlink_to(the_symlink, tmp_path / (self.PREFIX + "5"))
@@ -349,3 +382,24 @@ def attempt_symlink_to(path, to_path):
def test_tmpdir_equals_tmp_path(tmpdir, tmp_path):
assert Path(tmpdir) == tmp_path
def test_basetemp_with_read_only_files(testdir):
"""Integration test for #5524"""
testdir.makepyfile(
"""
import os
import stat
def test(tmp_path):
fn = tmp_path / 'foo.txt'
fn.write_text('hello')
mode = os.stat(str(fn)).st_mode
os.chmod(str(fn), mode & ~stat.S_IREAD)
"""
)
result = testdir.runpytest("--basetemp=tmp")
assert result.ret == 0
# running a second time and ensure we don't crash
result = testdir.runpytest("--basetemp=tmp")
assert result.ret == 0