Skip to content

Commit 81b369f

Browse files
committed
Fix collection of direct symlinked files
Fixes #4325.
1 parent 64762d2 commit 81b369f

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

changelog/4325.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix collection of direct symlinked files.

src/_pytest/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def _collect(self, arg):
488488
from _pytest.python import Package
489489

490490
names = self._parsearg(arg)
491-
argpath = names.pop(0).realpath()
491+
argpath = names.pop(0)
492492

493493
# Start with a Session root, and delve to argpath item (dir or file)
494494
# and stack all Packages found on the way.
@@ -636,7 +636,7 @@ def _parsearg(self, arg):
636636
"file or package not found: " + arg + " (missing __init__.py?)"
637637
)
638638
raise UsageError("file not found: " + arg)
639-
parts[0] = path
639+
parts[0] = path.realpath()
640640
return parts
641641

642642
def matchnodes(self, matching, names):

testing/test_collection.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import sys
77
import textwrap
88

9+
import py
10+
911
import pytest
1012
from _pytest.main import _in_venv
1113
from _pytest.main import EXIT_NOTESTSCOLLECTED
@@ -1051,3 +1053,16 @@ def test_1():
10511053
result = testdir.runpytest()
10521054
assert result.ret == 0
10531055
result.stdout.fnmatch_lines(["*1 passed in*"])
1056+
1057+
1058+
@pytest.mark.skipif(
1059+
not hasattr(py.path.local, "mksymlinkto"),
1060+
reason="symlink not available on this platform",
1061+
)
1062+
def test_collect_symlink_file_arg(testdir):
1063+
"""Test that collecting a direct symlink works (#4325)."""
1064+
p = testdir.makepyfile("def test_1(): pass")
1065+
testdir.tmpdir.join("symlink.py").mksymlinkto(p)
1066+
result = testdir.runpytest("--collect-only", "symlink.py")
1067+
result.stdout.fnmatch_lines(["collected 1 item"])
1068+
assert result.ret == 0

0 commit comments

Comments
 (0)