Skip to content

Commit bcc267a

Browse files
committed
FSCollector: use session._node_location_to_relpath for nodeids
This is relevant for `pytest t/foo.py --rootdir=/tmp`. Before: ``` ../../../../tmp F.sxx … t/foo.py:5: ValueError … FAILED ../../../../tmp/::test_fail - ValueError ``` This removes `_check_initialpaths_for_relpath` (added via pytest-dev#2776 to address part of the issue), but it is apparently bad trying to make them relative to any given arg, when they are meant to be relative to `rootdir` really.
1 parent 6a7df7f commit bcc267a

File tree

7 files changed

+31
-42
lines changed

7 files changed

+31
-42
lines changed

changelog/6701.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Node ids for paths outside of the rootdir are generated properly, e.g. for ``pytest testing --rootdir=/tmp -vv``.

src/_pytest/nodes.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,6 @@ def _prunetraceback(self, excinfo):
422422
excinfo.traceback = ntraceback.filter()
423423

424424

425-
def _check_initialpaths_for_relpath(session, fspath):
426-
for initial_path in session._initialpaths:
427-
if fspath.common(initial_path) == initial_path:
428-
return fspath.relto(initial_path)
429-
430-
431425
class FSHookProxy:
432426
def __init__(
433427
self, fspath: py.path.local, pm: PytestPluginManager, remove_mods
@@ -444,7 +438,12 @@ def __getattr__(self, name: str):
444438

445439
class FSCollector(Collector):
446440
def __init__(
447-
self, fspath: py.path.local, parent=None, config=None, session=None, nodeid=None
441+
self,
442+
fspath: py.path.local,
443+
parent=None,
444+
config=None,
445+
session=None,
446+
nodeid: Optional[str] = None,
448447
) -> None:
449448
name = fspath.basename
450449
if parent is not None:
@@ -457,11 +456,8 @@ def __init__(
457456
session = session or parent.session
458457

459458
if nodeid is None:
460-
nodeid = self.fspath.relto(session.config.rootdir)
461-
462-
if not nodeid:
463-
nodeid = _check_initialpaths_for_relpath(session, fspath)
464-
if nodeid and os.sep != SEP:
459+
nodeid = session._node_location_to_relpath(self.fspath)
460+
if os.sep != SEP:
465461
nodeid = nodeid.replace(os.sep, SEP)
466462

467463
super().__init__(name, parent, config, session, nodeid=nodeid, fspath=fspath)

testing/acceptance_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,8 @@ def test_cmdline_python_namespace_package(self, testdir, monkeypatch):
730730
assert result.ret == 0
731731
result.stdout.fnmatch_lines(
732732
[
733-
"test_hello.py::test_hello*PASSED*",
734-
"test_hello.py::test_other*PASSED*",
733+
"../hello/ns_pkg/hello/test_hello.py::test_hello PASSED *",
734+
"../hello/ns_pkg/hello/test_hello.py::test_other PASSED *",
735735
"ns_pkg/world/test_world.py::test_world*PASSED*",
736736
"ns_pkg/world/test_world.py::test_other*PASSED*",
737737
"*4 passed in*",

testing/python/fixtures.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3690,6 +3690,7 @@ def test_foo(request):
36903690
result = testdir.runpytest()
36913691
result.stdout.fnmatch_lines(
36923692
[
3693+
"test_foos.py F *",
36933694
"The requested fixture has no parameter defined for test:",
36943695
" test_foos.py::test_foo",
36953696
"",
@@ -3707,8 +3708,9 @@ def test_foo(request):
37073708
result = testdir.runpytest("--rootdir", rootdir, tests_dir)
37083709
result.stdout.fnmatch_lines(
37093710
[
3711+
"../tests/test_foos.py F *",
37103712
"The requested fixture has no parameter defined for test:",
3711-
" test_foos.py::test_foo",
3713+
" ../tests/test_foos.py::test_foo",
37123714
"",
37133715
"Requested fixture 'fix_with_param' defined in:",
37143716
"{}:4".format(fixfile),

testing/test_conftest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,14 @@ def fixture():
274274
build.join(f).mksymlinkto(real.join(f))
275275
build.chdir()
276276
result = testdir.runpytest("-vs", "app/test_foo.py")
277-
result.stdout.fnmatch_lines(["*conftest_loaded*", "PASSED"])
277+
result.stdout.fnmatch_lines(
278+
[
279+
"conftest_loaded",
280+
"../real/app/test_foo.py::test1 fixture_used",
281+
"PASSED",
282+
"*= 1 passed in *",
283+
]
284+
)
278285
assert result.ret == ExitCode.OK
279286

280287

testing/test_nodes.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import py
2-
31
import pytest
42
from _pytest import nodes
53

@@ -38,23 +36,3 @@ def test():
3836
)
3937
with pytest.raises(ValueError, match=".*instance of PytestWarning.*"):
4038
items[0].warn(UserWarning("some warning"))
41-
42-
43-
def test__check_initialpaths_for_relpath():
44-
"""Ensure that it handles dirs, and does not always use dirname."""
45-
cwd = py.path.local()
46-
47-
class FakeSession:
48-
_initialpaths = [cwd]
49-
50-
assert nodes._check_initialpaths_for_relpath(FakeSession, cwd) == ""
51-
52-
sub = cwd.join("file")
53-
54-
class FakeSession:
55-
_initialpaths = [cwd]
56-
57-
assert nodes._check_initialpaths_for_relpath(FakeSession, sub) == "file"
58-
59-
outside = py.path.local("/outside")
60-
assert nodes._check_initialpaths_for_relpath(FakeSession, outside) is None

testing/test_session.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import os
2+
13
import pytest
24
from _pytest.config import ExitCode
5+
from _pytest.pytester import Testdir
36

47

58
class SessionTests:
@@ -334,7 +337,7 @@ def pytest_sessionfinish():
334337

335338

336339
@pytest.mark.parametrize("path", ["root", "{relative}/root", "{environment}/root"])
337-
def test_rootdir_option_arg(testdir, monkeypatch, path):
340+
def test_rootdir_option_arg(testdir: Testdir, monkeypatch, path: str) -> None:
338341
monkeypatch.setenv("PY_ROOTDIR_PATH", str(testdir.tmpdir))
339342
path = path.format(relative=str(testdir.tmpdir), environment="$PY_ROOTDIR_PATH")
340343

@@ -344,15 +347,17 @@ def test_rootdir_option_arg(testdir, monkeypatch, path):
344347
"""
345348
import os
346349
def test_one():
347-
assert 1
348-
"""
350+
assert os.getcwd() == {!r}
351+
""".format(
352+
os.getcwd()
353+
)
349354
)
350355

351356
result = testdir.runpytest("--rootdir={}".format(path))
352357
result.stdout.fnmatch_lines(
353358
[
354359
"*rootdir: {}/root".format(testdir.tmpdir),
355-
"root/test_rootdir_option_arg.py *",
360+
"test_rootdir_option_arg.py *",
356361
"*1 passed*",
357362
]
358363
)

0 commit comments

Comments
 (0)