Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: add workaround for permission error crashes from non-selected directories #12311

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/12120.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `PermissionError` crashes arising from directories which are not selected on the command-line.
7 changes: 6 additions & 1 deletion src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@
path: Path, parent: nodes.Collector
) -> Optional[nodes.Collector]:
pkginit = path / "__init__.py"
if pkginit.is_file():
try:
has_pkginit = pkginit.is_file()
except PermissionError:

Check warning on line 181 in src/_pytest/python.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/python.py#L181

Added line #L181 was not covered by tests
# See https://github.com/pytest-dev/pytest/issues/12120#issuecomment-2106349096.
return None

Check warning on line 183 in src/_pytest/python.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/python.py#L183

Added line #L183 was not covered by tests
if has_pkginit:
return Package.from_parent(parent, path=path)
return None

Expand Down
17 changes: 17 additions & 0 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,23 @@ def test_testpaths_ini(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No
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:
Expand Down
Loading