Skip to content

Commit 8532756

Browse files
committed
Fix nodes._check_initialpaths_for_relpath for dirs
Ref: #4321 (comment) Hardens some of the not many tests affected by this: 1. `testing/test_session.py::test_rootdir_option_arg` displayed: > root/test_rootdir_option_arg2/test_rootdir_option_arg.py 2. `test_cmdline_python_namespace_package` displayed "hello/" prefix for: > hello/test_hello.py::test_hello > hello/test_hello.py::test_other
1 parent d4fdf79 commit 8532756

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

src/_pytest/nodes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ def _prunetraceback(self, excinfo):
447447
def _check_initialpaths_for_relpath(session, fspath):
448448
for initial_path in session._initialpaths:
449449
if fspath.common(initial_path) == initial_path:
450+
if initial_path.isdir():
451+
return fspath.relto(initial_path)
450452
return fspath.relto(initial_path.dirname)
451453

452454

testing/acceptance_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -662,11 +662,11 @@ def test_cmdline_python_namespace_package(self, testdir, monkeypatch):
662662
assert result.ret == 0
663663
result.stdout.fnmatch_lines(
664664
[
665-
"*test_hello.py::test_hello*PASSED*",
666-
"*test_hello.py::test_other*PASSED*",
667-
"*test_world.py::test_world*PASSED*",
668-
"*test_world.py::test_other*PASSED*",
669-
"*4 passed*",
665+
"test_hello.py::test_hello*PASSED*",
666+
"test_hello.py::test_other*PASSED*",
667+
"ns_pkg/world/test_world.py::test_world*PASSED*",
668+
"ns_pkg/world/test_world.py::test_other*PASSED*",
669+
"*4 passed in*",
670670
]
671671
)
672672

testing/test_nodes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import py
2+
13
import pytest
24
from _pytest import nodes
35

@@ -29,3 +31,19 @@ def test():
2931
)
3032
with pytest.raises(ValueError, match=".*instance of PytestWarning.*"):
3133
items[0].warn(UserWarning("some warning"))
34+
35+
36+
def test__check_initialpaths_for_relpath():
37+
"""Ensure that it handles dirs, and does not always use dirname."""
38+
d = py.path.local()
39+
40+
class FakeSession:
41+
_initialpaths = [d]
42+
43+
assert nodes._check_initialpaths_for_relpath(FakeSession, d) == ""
44+
45+
f = d.join("file")
46+
assert nodes._check_initialpaths_for_relpath(FakeSession, f) == "file"
47+
48+
outside = py.path.local("/outside")
49+
assert nodes._check_initialpaths_for_relpath(FakeSession, outside) is None

testing/test_session.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,11 @@ def test_one():
323323

324324
result = testdir.runpytest("--rootdir={}".format(path))
325325
result.stdout.fnmatch_lines(
326-
["*rootdir: {}/root, inifile:*".format(testdir.tmpdir), "*1 passed*"]
326+
[
327+
"*rootdir: {}/root, inifile:*".format(testdir.tmpdir),
328+
"root/test_rootdir_option_arg.py *",
329+
"*1 passed*",
330+
]
327331
)
328332

329333

0 commit comments

Comments
 (0)