diff --git a/changelog/4321.bugfix.rst b/changelog/4321.bugfix.rst new file mode 100644 index 00000000000..8bfa9efdeb6 --- /dev/null +++ b/changelog/4321.bugfix.rst @@ -0,0 +1 @@ +Fix ``item.nodeid`` with resolved symlinks. diff --git a/changelog/4325.bugfix.rst b/changelog/4325.bugfix.rst new file mode 100644 index 00000000000..71f13f429dd --- /dev/null +++ b/changelog/4325.bugfix.rst @@ -0,0 +1 @@ +Fix collection of direct symlinked files, where the target does not match ``python_files``. diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 1de5f656fd0..b959ef44529 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -488,7 +488,7 @@ def _collect(self, arg): from _pytest.python import Package names = self._parsearg(arg) - argpath = names.pop(0).realpath() + argpath = names.pop(0) # Start with a Session root, and delve to argpath item (dir or file) # and stack all Packages found on the way. @@ -636,7 +636,7 @@ def _parsearg(self, arg): "file or package not found: " + arg + " (missing __init__.py?)" ) raise UsageError("file not found: " + arg) - parts[0] = path + parts[0] = path.realpath() return parts def matchnodes(self, matching, names): diff --git a/testing/test_collection.py b/testing/test_collection.py index 18033b9c006..af565cf9fe7 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -6,6 +6,8 @@ import sys import textwrap +import py + import pytest from _pytest.main import _in_venv from _pytest.main import EXIT_NOTESTSCOLLECTED @@ -1051,3 +1053,22 @@ def test_1(): result = testdir.runpytest() assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed in*"]) + + +@pytest.mark.skipif( + not hasattr(py.path.local, "mksymlinkto"), + reason="symlink not available on this platform", +) +def test_collect_symlink_file_arg(testdir): + """Test that collecting a direct symlink, where the target does not match python_files works (#4325).""" + p = testdir.makepyfile( + hello=""" + def test_nodeid(request): + print(request.node.nodeid) + assert not request.node.nodeid.startswith('::') + """ + ) + testdir.tmpdir.join("symlink.py").mksymlinkto(p) + result = testdir.runpytest("-v", "symlink.py") + result.stdout.fnmatch_lines(["hello.py::test_nodeid PASSED*", "*1 passed in*"]) + assert result.ret == 0