Skip to content

Commit

Permalink
collect: python: fix AssertionError with broken symlinks
Browse files Browse the repository at this point in the history
Fixes #4782.
  • Loading branch information
blueyed committed Feb 13, 2019
1 parent 8726be2 commit 28de780
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/4782.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``AssertionError`` with collection of broken symlinks with packages.
6 changes: 5 additions & 1 deletion src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,8 @@ def collect(self):
pkg_prefixes = set()
for path in this_path.visit(rec=self._recurse, bf=True, sort=True):
# We will visit our own __init__.py file, in which case we skip it.
if path.isfile():
is_file = path.isfile()
if is_file:
if path.basename == "__init__.py" and path.dirpath() == this_path:
continue

Expand All @@ -646,6 +647,9 @@ def collect(self):
if path.isdir():
if path.join("__init__.py").check(file=1):
pkg_prefixes.add(path)
elif not is_file:
# Broken symlinks.
continue
else:
for x in self._collectfile(path):
yield x
Expand Down
45 changes: 45 additions & 0 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,3 +1186,48 @@ def test_collect_pkg_init_and_file_in_args(testdir):
"*2 passed in*",
]
)


def test_collect_pkg_with_symlinks(testdir):
pkg = testdir.mkpydir("pkg")
pkg.ensure("test_file.py").write("def test_file(): pass")

# Create a broken symlink.
pkg.join("test_broken.py").mksymlinkto("test_doesnotexist.py")

# Symlink that gets collected.
pkg.join("test_symlink.py").mksymlinkto("test_file.py")

result = testdir.runpytest("-v", str(pkg))
result.stdout.fnmatch_lines(
[
"pkg/test_file.py::test_file PASSED*",
"pkg/test_symlink.py::test_file PASSED*",
"*2 passed in*",
]
)


def test_collect_sub_with_symlinks(testdir):
"""Similar to test_collect_pkg_with_symlinks above, without __init__.py.
Here the _visit_filter only considers files already, and skips the broken
symlink.
"""
pkg = testdir.mkdir("sub")
pkg.ensure("test_file.py").write("def test_file(): pass")

# Create a broken symlink.
pkg.join("test_broken.py").mksymlinkto("test_doesnotexist.py")

# Symlink that gets collected.
pkg.join("test_symlink.py").mksymlinkto("test_file.py")

result = testdir.runpytest("-v", str(pkg))
result.stdout.fnmatch_lines(
[
"sub/test_file.py::test_file PASSED*",
"sub/test_symlink.py::test_file PASSED*",
"*2 passed in*",
]
)

0 comments on commit 28de780

Please sign in to comment.