Skip to content

Commit

Permalink
fixup! WIP: fix on_rm_rf_error
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Jul 16, 2020
1 parent 9898587 commit 44efb38
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
11 changes: 5 additions & 6 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ def on_rm_rf_error(func, path: str, exc, *, start_path: Path, last_evalue=None)
exctype, excvalue = exc[:2]

if excvalue is last_evalue:
# TODO: test
return False

# another process removed the file in the middle of the "rm_rf" (xdist for example)
# more context: https://github.com/pytest-dev/pytest/issues/5974#issuecomment-543799018
if isinstance(excvalue, FileNotFoundError):
return False

if not isinstance(excvalue, PermissionError):
if not start_path.exists():
return False
elif not isinstance(excvalue, PermissionError):
warnings.warn(
PytestWarning(
"(rm_rf) error removing {}\n{}: {}".format(path, exctype, excvalue)
Expand Down Expand Up @@ -104,9 +105,7 @@ def chmod_rw(p: str) -> None:


def rm_rf(path: Path) -> None:
"""Remove the path contents recursively, even if some elements
are read-only.
"""
"""Remove the path recursively (handling/fixing any permission errors)."""
onerror = partial(on_rm_rf_error, start_path=path)
shutil.rmtree(str(path), onerror=onerror)

Expand Down
14 changes: 5 additions & 9 deletions testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
import py

import pytest
from _pytest.compat import TYPE_CHECKING
from _pytest.pathlib import _shorten_path
from _pytest.pathlib import fnmatch_ex
from _pytest.pathlib import get_lock_path
from _pytest.pathlib import maybe_delete_a_numbered_dir
from _pytest.pathlib import Path
from _pytest.pathlib import rm_rf

if TYPE_CHECKING:
import py.path


class TestPort:
"""Test that our port of py.common.FNMatcher (fnmatch_ex) produces the same results as the
Expand Down Expand Up @@ -109,8 +105,8 @@ def test_shorten_path(testdir) -> None:
assert _shorten_path(os.path.join(abs_foo, "..")) == "~"


def test_rm_rf_chmod0(tmpdir: py.path.local) -> None:
noperm = tmpdir.ensure('noperm', dir=True)
noperm.chmod(0)
rm_rf(tmpdir)
assert not os.path.exists(tmpdir)
def test_rm_rf_chmod0(tmp_path: "Path") -> None:
noperm = tmp_path / "noperm"
noperm.touch(mode=0)
rm_rf(tmp_path)
assert not os.path.exists(tmp_path)

0 comments on commit 44efb38

Please sign in to comment.