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 e097acc
Show file tree
Hide file tree
Showing 3 changed files with 47 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.
2 changes: 1 addition & 1 deletion src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def collect(self):
if path.isdir():
if path.join("__init__.py").check(file=1):
pkg_prefixes.add(path)
else:
elif path.isfile():
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 the test above, but 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 e097acc

Please sign in to comment.