Skip to content

Commit

Permalink
Fix crash when directory is removed during collection (pytest-dev#13086)
Browse files Browse the repository at this point in the history
Fixes pytest-dev#13083

---------

Co-authored-by: Bruno Oliveira <[email protected]>
  • Loading branch information
delta87 and nicoddemus authored Jan 8, 2025
1 parent bdfc3a9 commit 3214263
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
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
1 change: 1 addition & 0 deletions changelog/13083.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed issue where pytest could crash if one of the collected directories got removed during collection.
13 changes: 10 additions & 3 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,17 +955,24 @@ def scandir(
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.
# Attempt to create a scandir iterator for the given path.
try:
scandir_iter = os.scandir(path)
except FileNotFoundError:
# If the directory does not exist, return an empty list.
return []
# Use the scandir iterator in a context manager to ensure it is properly closed.
with scandir_iter as s:
for entry in s:
try:
entry.is_file()
except OSError as err:
if _ignore_error(err):
continue
# Reraise non-ignorable errors to avoid hiding issues.
raise
entries.append(entry)
entries.sort(key=sort_key) # type: ignore[arg-type]
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() -> None:
# 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("some permission error")
# 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="some permission error"):
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

0 comments on commit 3214263

Please sign in to comment.