Skip to content

Commit 4d5fb7d

Browse files
authored
Merge pull request #12319 from pytest-dev/backport-12311-to-8.2.x
[8.2.x] python: add workaround for permission error crashes from non-selected directories
2 parents 328001e + c9e9315 commit 4d5fb7d

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

changelog/12120.bugfix.rst

Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 17 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy