Improve rm_rf to handle only known functions
Warnings are emitted if we cannot safely remove paths. Fix #5626
This commit is contained in:
committed by
Bruno Oliveira
parent
dc6e7b9fcf
commit
9064eea216
@@ -312,7 +312,20 @@ class TestNumberedDir:
|
||||
p, consider_lock_dead_if_created_before=p.stat().st_mtime + 1
|
||||
)
|
||||
|
||||
def test_rmtree(self, tmp_path):
|
||||
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"))
|
||||
self._do_cleanup(tmp_path)
|
||||
|
||||
def test_removal_accepts_lock(self, tmp_path):
|
||||
folder = pathlib.make_numbered_dir(root=tmp_path, prefix=self.PREFIX)
|
||||
pathlib.create_cleanup_lock(folder)
|
||||
pathlib.maybe_delete_a_numbered_dir(folder)
|
||||
assert folder.is_dir()
|
||||
|
||||
|
||||
class TestRmRf:
|
||||
def test_rm_rf(self, tmp_path):
|
||||
from _pytest.pathlib import rm_rf
|
||||
|
||||
adir = tmp_path / "adir"
|
||||
@@ -328,7 +341,7 @@ class TestNumberedDir:
|
||||
rm_rf(adir)
|
||||
assert not adir.exists()
|
||||
|
||||
def test_rmtree_with_read_only_file(self, tmp_path):
|
||||
def test_rm_rf_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
|
||||
|
||||
@@ -337,14 +350,17 @@ class TestNumberedDir:
|
||||
|
||||
fn.touch()
|
||||
|
||||
mode = os.stat(str(fn)).st_mode
|
||||
os.chmod(str(fn), mode & ~stat.S_IWRITE)
|
||||
self.chmod_r(fn)
|
||||
|
||||
rm_rf(fn.parent)
|
||||
|
||||
assert not fn.parent.is_dir()
|
||||
|
||||
def test_rmtree_with_read_only_directory(self, tmp_path):
|
||||
def chmod_r(self, path):
|
||||
mode = os.stat(str(path)).st_mode
|
||||
os.chmod(str(path), mode & ~stat.S_IWRITE)
|
||||
|
||||
def test_rm_rf_with_read_only_directory(self, tmp_path):
|
||||
"""Ensure rm_rf can remove read-only directories (#5524)"""
|
||||
from _pytest.pathlib import rm_rf
|
||||
|
||||
@@ -352,23 +368,37 @@ class TestNumberedDir:
|
||||
adir.mkdir()
|
||||
|
||||
(adir / "foo.txt").touch()
|
||||
mode = os.stat(str(adir)).st_mode
|
||||
os.chmod(str(adir), mode & ~stat.S_IWRITE)
|
||||
self.chmod_r(adir)
|
||||
|
||||
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"))
|
||||
self._do_cleanup(tmp_path)
|
||||
def test_on_rm_rf_error(self, tmp_path):
|
||||
from _pytest.pathlib import on_rm_rf_error
|
||||
|
||||
def test_removal_accepts_lock(self, tmp_path):
|
||||
folder = pathlib.make_numbered_dir(root=tmp_path, prefix=self.PREFIX)
|
||||
pathlib.create_cleanup_lock(folder)
|
||||
pathlib.maybe_delete_a_numbered_dir(folder)
|
||||
assert folder.is_dir()
|
||||
adir = tmp_path / "dir"
|
||||
adir.mkdir()
|
||||
|
||||
fn = adir / "foo.txt"
|
||||
fn.touch()
|
||||
self.chmod_r(fn)
|
||||
|
||||
# unknown exception
|
||||
with pytest.warns(pytest.PytestWarning):
|
||||
exc_info = (None, RuntimeError(), None)
|
||||
on_rm_rf_error(os.unlink, str(fn), exc_info, start_path=tmp_path)
|
||||
assert fn.is_file()
|
||||
|
||||
# unknown function
|
||||
with pytest.warns(pytest.PytestWarning):
|
||||
exc_info = (None, PermissionError(), None)
|
||||
on_rm_rf_error(None, str(fn), exc_info, start_path=tmp_path)
|
||||
assert fn.is_file()
|
||||
|
||||
exc_info = (None, PermissionError(), None)
|
||||
on_rm_rf_error(os.unlink, str(fn), exc_info, start_path=tmp_path)
|
||||
assert not fn.is_file()
|
||||
|
||||
|
||||
def attempt_symlink_to(path, to_path):
|
||||
|
||||
Reference in New Issue
Block a user