Skip to content

Commit 8baeb90

Browse files
committed
Fix nodes._check_initialpaths_for_relpath for dirs
Ref: #4321 (comment)
1 parent 64762d2 commit 8baeb90

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
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/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

0 commit comments

Comments
 (0)