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

Bug fix/scandir collection error #13084

Closed
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 @@ -21,6 +21,7 @@ Alexander Johnson
Alexander King
Alexei Kozlenok
Alice Purcell
Alison Day
Allan Feldman
Aly Sivji
Amir Elkess
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 @@
Fixes an issue where the :func:`_pytest.pathlib.scandir` function crashes if the directory to scan does not exist by catching the ``FileNotFoundError`` and returning an empty array.
26 changes: 15 additions & 11 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,17 +957,21 @@
The default is to sort by name.
"""
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)
try:
with os.scandir(path) as s:
scanned = list(s)
except FileNotFoundError:
return []

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

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L963-L964

Added lines #L963 - L964 were not covered by tests
# Skip entries with symlink loops and other brokenness, so the caller
# doesn't have to deal with it.
for entry in scanned:
try:
entry.is_file()
except OSError as err:
if _ignore_error(err):
continue
raise

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

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L973

Added line #L973 was not covered by tests
entries.append(entry)
entries.sort(key=sort_key) # type: ignore[arg-type]
return entries

Expand Down
Loading