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

fix #13083: Fix scandir() crash by returning [] when directory is not found (#13083) #13085

Closed
wants to merge 1 commit into from
Closed
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ Ran Benita
Raphael Castaneda
Raphael Pierzina
Rafal Semik
Reza Mousavi
Raquel Alegre
Ravi Chandra
Reagan Lee
Expand Down
6 changes: 6 additions & 0 deletions changelog/13083.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
13083.bugfix.rst:

Fix issue where the `scandir` function in `pathlib.py` caused symlink loops under certain conditions.

- Issue: https://github.com/pytest-dev/pytest/issues/13083
- Authors: Reza Mousavi
32 changes: 18 additions & 14 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,21 +955,25 @@

The returned entries are sorted according to the given key.
The default is to sort by name.
If the directory does not exist, return an empty list.
"""
entries = []
with os.scandir(path) as s:
# Skip entries with symlink loops and other brokenness, so the caller
# doesn't have to deal with it.
for entry in s:
try:
entry.is_file()
except OSError as err:
if _ignore_error(err):
continue
raise
entries.append(entry)
entries.sort(key=sort_key) # type: ignore[arg-type]
return entries
try:
entries = []
with os.scandir(path) as s:
# Skip entries with symlink loops and other brokenness, so the caller
# doesn't have to deal with it.
for entry in s:
try:
entry.is_file()
except OSError as err:

Check warning on line 968 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L968

Added line #L968 was not covered by tests
if _ignore_error(err):
continue
raise

Check warning on line 971 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L970-L971

Added lines #L970 - L971 were not covered by tests
entries.append(entry)
entries.sort(key=sort_key) # type: ignore[arg-type]
return entries
except FileNotFoundError:
return []

Check warning on line 976 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L975-L976

Added lines #L975 - L976 were not covered by tests


def visit(
Expand Down
24 changes: 24 additions & 0 deletions testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from _pytest.pathlib import resolve_package_path
from _pytest.pathlib import resolve_pkg_root_and_module_name
from _pytest.pathlib import safe_exists
from _pytest.pathlib import scandir
from _pytest.pathlib import spec_matches_module_path
from _pytest.pathlib import symlink_or_skip
from _pytest.pathlib import visit
Expand Down Expand Up @@ -569,6 +570,29 @@ def test_samefile_false_negatives(tmp_path: Path, monkeypatch: MonkeyPatch) -> N
assert getattr(module, "foo")() == 42


def test_scandir_with_non_existent_directory() -> None:
# Test with a directory that does not exist
non_existent_dir = "path_to_non_existent_dir"
result = scandir(non_existent_dir)
# Assert that the result is an empty list
assert result == []


def test_scandir_handles_os_error():
# Create a mock entry that will raise an OSError when is_file is called
mock_entry = unittest.mock.MagicMock()
mock_entry.is_file.side_effect = OSError("Permission denied")
# Mock os.scandir to return an iterator with our mock entry
with unittest.mock.patch("os.scandir") as mock_scandir:
mock_scandir.return_value.__enter__.return_value = [mock_entry]
# Call the scandir function with a path
# We expect an OSError to be raised here
with pytest.raises(OSError, match="Permission denied"):
scandir("/fake/path")
# Verify that the is_file method was called on the mock entry
mock_entry.is_file.assert_called_once()


class TestImportLibMode:
def test_importmode_importlib_with_dataclass(
self, tmp_path: Path, ns_param: bool
Expand Down
Loading