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 407d4a0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
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.
7 changes: 6 additions & 1 deletion src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,12 @@ def _collect(self, arg):
yield y

def _collectfile(self, path, handle_dupes=True):
assert path.isfile()
assert path.isfile(), "%r is not a file (isdir=%r, exists=%r, islink=%r)" % (
path,
path.isdir(),
path.exists(),
path.islink(),
)
ihook = self.gethookproxy(path)
if not self.isinitpath(path):
if ihook.pytest_ignore_collect(path=path, config=self.config):
Expand Down
20 changes: 14 additions & 6 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,12 @@ def gethookproxy(self, fspath):
return proxy

def _collectfile(self, path, handle_dupes=True):
assert path.isfile()
assert path.isfile(), "%r is not a file (isdir=%r, exists=%r, islink=%r)" % (
path,
path.isdir(),
path.exists(),
path.islink(),
)
ihook = self.gethookproxy(path)
if not self.isinitpath(path):
if ihook.pytest_ignore_collect(path=path, config=self.config):
Expand Down Expand Up @@ -632,7 +637,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 @@ -643,12 +649,14 @@ def collect(self):
):
continue

if path.isdir():
if path.join("__init__.py").check(file=1):
pkg_prefixes.add(path)
else:
if is_file:
for x in self._collectfile(path):
yield x
elif not path.isdir():
# Broken symlink or invalid/missing file.
continue
elif path.join("__init__.py").check(file=1):
pkg_prefixes.add(path)


def _get_xunit_setup_teardown(holder, attr_name, param_obj=None):
Expand Down
27 changes: 27 additions & 0 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,3 +1186,30 @@ def test_collect_pkg_init_and_file_in_args(testdir):
"*2 passed in*",
]
)


@pytest.mark.skipif(
not hasattr(py.path.local, "mksymlinkto"),
reason="symlink not available on this platform",
)
@pytest.mark.parametrize("use_pkg", (True, False))
def test_collect_sub_with_symlinks(use_pkg, testdir):
sub = testdir.mkdir("sub")
if use_pkg:
sub.ensure("__init__.py")
sub.ensure("test_file.py").write("def test_file(): pass")

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

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

result = testdir.runpytest("-v", str(sub))
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 407d4a0

Please sign in to comment.