Skip to content

Commit

Permalink
Review rm_rf handling of FileNotFoundErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Oct 22, 2019
1 parent 978c7ae commit 7752b9b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,34 @@ def ensure_reset_dir(path):
path.mkdir()


def on_rm_rf_error(func, path: str, exc, *, start_path):
"""Handles known read-only errors during rmtree."""
excvalue = exc[1]
def on_rm_rf_error(func, path: str, exc, *, start_path) -> bool:
"""Handles known read-only errors during rmtree.
The returned value is used only by our own tests.
"""
exctype, excvalue = exc[:2]

# another process removed the file in the middle of the "rm_rf" (xdist for example)
if isinstance(excvalue, FileNotFoundError):
return False

if not isinstance(excvalue, PermissionError):
warnings.warn(
PytestWarning("(rm_rf) error removing {}: {}".format(path, excvalue))
PytestWarning(
"(rm_rf) error removing {}\n{}: {}".format(path, exctype, excvalue)
)
)
return
return False

if func not in (os.rmdir, os.remove, os.unlink):
warnings.warn(
PytestWarning("(rm_rf) error removing {}: {}".format(path, excvalue))
PytestWarning(
"(rm_rf) unknown function {} when removing {}:\n{}: {}".format(
path, func, exctype, excvalue
)
)
)
return
return False

# Chmod + retry.
import stat
Expand All @@ -73,6 +86,7 @@ def chmod_rw(p: str):
chmod_rw(str(path))

func(path)
return True


def rm_rf(path: Path):
Expand Down
4 changes: 4 additions & 0 deletions testing/test_tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ def test_on_rm_rf_error(self, tmp_path):
on_rm_rf_error(os.unlink, str(fn), exc_info, start_path=tmp_path)
assert fn.is_file()

# we ignore FileNotFoundError
exc_info = (None, FileNotFoundError(), None)
assert not on_rm_rf_error(None, str(fn), exc_info, start_path=tmp_path)

# unknown function
with pytest.warns(pytest.PytestWarning):
exc_info = (None, PermissionError(), None)
Expand Down

0 comments on commit 7752b9b

Please sign in to comment.