diff --git a/changelog/6074.bugfix.rst b/changelog/6074.bugfix.rst new file mode 100644 index 00000000000..624cf5d1c8f --- /dev/null +++ b/changelog/6074.bugfix.rst @@ -0,0 +1 @@ +pytester: fix order of arguments in ``rm_rf`` warning when cleaning up temporary directories, and do not emit warnings for errors with ``os.open``. diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 543103fb5b0..8d25b21dd7d 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -68,13 +68,14 @@ def on_rm_rf_error(func, path: str, exc, *, start_path: Path) -> bool: return False if func not in (os.rmdir, os.remove, os.unlink): - warnings.warn( - PytestWarning( - "(rm_rf) unknown function {} when removing {}:\n{}: {}".format( - path, func, exctype, excvalue + if func not in (os.open,): + warnings.warn( + PytestWarning( + "(rm_rf) unknown function {} when removing {}:\n{}: {}".format( + func, path, exctype, excvalue + ) ) ) - ) return False # Chmod + retry. diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 0ebed22ac45..29b6db947bc 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -388,11 +388,21 @@ def test_on_rm_rf_error(self, tmp_path): assert not on_rm_rf_error(None, str(fn), exc_info, start_path=tmp_path) # unknown function - with pytest.warns(pytest.PytestWarning): + with pytest.warns( + pytest.PytestWarning, + match=r"^\(rm_rf\) unknown function None when removing .*foo.txt:\nNone: ", + ): exc_info = (None, PermissionError(), None) on_rm_rf_error(None, str(fn), exc_info, start_path=tmp_path) assert fn.is_file() + # ignored function + with pytest.warns(None) as warninfo: + exc_info = (None, PermissionError(), None) + on_rm_rf_error(os.open, str(fn), exc_info, start_path=tmp_path) + assert fn.is_file() + assert not [x.message for x in warninfo] + exc_info = (None, PermissionError(), None) on_rm_rf_error(os.unlink, str(fn), exc_info, start_path=tmp_path) assert not fn.is_file()