Merge pull request #12311 from bluetech/pkg-collect-perm-error

python: add workaround for permission error crashes from non-selected directories
This commit is contained in:
Ran Benita 2024-05-13 20:32:23 +03:00 committed by GitHub
commit 704792ecbd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View File

@ -0,0 +1 @@
Fix `PermissionError` crashes arising from directories which are not selected on the command-line.

View File

@ -176,7 +176,12 @@ def pytest_collect_directory(
path: Path, parent: nodes.Collector
) -> Optional[nodes.Collector]:
pkginit = path / "__init__.py"
if pkginit.is_file():
try:
has_pkginit = pkginit.is_file()
except PermissionError:
# See https://github.com/pytest-dev/pytest/issues/12120#issuecomment-2106349096.
return None
if has_pkginit:
return Package.from_parent(parent, path=path)
return None

View File

@ -285,6 +285,23 @@ class TestCollectFS:
items, reprec = pytester.inline_genitems()
assert [x.name for x in items] == [f"test_{dirname}"]
def test_missing_permissions_on_unselected_directory_doesnt_crash(
self, pytester: Pytester
) -> None:
"""Regression test for #12120."""
test = pytester.makepyfile(test="def test(): pass")
bad = pytester.mkdir("bad")
try:
bad.chmod(0)
result = pytester.runpytest(test)
finally:
bad.chmod(750)
bad.rmdir()
assert result.ret == ExitCode.OK
result.assert_outcomes(passed=1)
class TestCollectPluginHookRelay:
def test_pytest_collect_file(self, pytester: Pytester) -> None: