Skip to content

Commit

Permalink
Fix handling recursive symlinks
Browse files Browse the repository at this point in the history
When pytest was run on a directory containing a recursive symlink it failed
with ELOOP as the library was not able to determine the type of the
direntry:

src/_pytest/main.py:685: in collect
    if not direntry.is_file():
E   OSError: [Errno 40] Too many levels of symbolic links: '/home/florian/proj/pytest/tests/recursive'

This is fixed now by handling ELOOP.

Fixes pytest-dev#7951
  • Loading branch information
csernazs committed Oct 28, 2020
1 parent 78c09b9 commit c55bb10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import importlib
import os
import sys
import errno
from pathlib import Path
from typing import Callable
from typing import Dict
Expand Down Expand Up @@ -676,8 +677,14 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:

seen_dirs: Set[py.path.local] = set()
for direntry in visit(str(argpath), self._recurse):
if not direntry.is_file():
continue
try:
if not direntry.is_file():
continue
except OSError as err:
if err.errno == errno.ELOOP:
continue
else:
raise

path = py.path.local(direntry.path)
dirpath = path.dirpath()
Expand Down
14 changes: 14 additions & 0 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,3 +1404,17 @@ def a(): return 4
result = testdir.runpytest()
# Not INTERNAL_ERROR
assert result.ret == ExitCode.INTERRUPTED


def test_does_not_crash_on_recursive_symlink(testdir: Testdir) -> None:
"""Regression test for an issue around recursive symlinks (#7951)."""
testdir.makepyfile(
"""
def test_foo(): assert True
"""
)
os.symlink("recursive", str(testdir.tmpdir.join("recursive")))
result = testdir.runpytest()

assert result.ret == ExitCode.OK
assert result.parseoutcomes() == {"passed": 1}

0 comments on commit c55bb10

Please sign in to comment.