Skip to content

Commit c9e9315

Browse files
bluetechpytestbot
authored andcommitted
[8.2.x] python: add workaround for permission error crashes from non-selected directories
1 parent 328001e commit c9e9315

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

changelog/12120.bugfix.rst

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

src/_pytest/python.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ def pytest_collect_directory(
176176
path: Path, parent: nodes.Collector
177177
) -> Optional[nodes.Collector]:
178178
pkginit = path / "__init__.py"
179-
if pkginit.is_file():
179+
try:
180+
has_pkginit = pkginit.is_file()
181+
except PermissionError:
182+
# See https://github.com/pytest-dev/pytest/issues/12120#issuecomment-2106349096.
183+
return None
184+
if has_pkginit:
180185
return Package.from_parent(parent, path=path)
181186
return None
182187

testing/test_collection.py

+17
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,23 @@ def test_testpaths_ini(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No
285285
items, reprec = pytester.inline_genitems()
286286
assert [x.name for x in items] == ["test_%s" % dirname]
287287

288+
def test_missing_permissions_on_unselected_directory_doesnt_crash(
289+
self, pytester: Pytester
290+
) -> None:
291+
"""Regression test for #12120."""
292+
test = pytester.makepyfile(test="def test(): pass")
293+
bad = pytester.mkdir("bad")
294+
try:
295+
bad.chmod(0)
296+
297+
result = pytester.runpytest(test)
298+
finally:
299+
bad.chmod(750)
300+
bad.rmdir()
301+
302+
assert result.ret == ExitCode.OK
303+
result.assert_outcomes(passed=1)
304+
288305

289306
class TestCollectPluginHookRelay:
290307
def test_pytest_collect_file(self, pytester: Pytester) -> None:

0 commit comments

Comments
 (0)