Skip to content

Commit

Permalink
Fix nodes._check_initialpaths_for_relpath for dirs
Browse files Browse the repository at this point in the history
Ref: pytest-dev#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
  • Loading branch information
blueyed committed Nov 9, 2018
1 parent e00f3a2 commit bee72a6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def _prunetraceback(self, excinfo):
def _check_initialpaths_for_relpath(session, fspath):
for initial_path in session._initialpaths:
if fspath.common(initial_path) == initial_path:
return fspath.relto(initial_path.dirname)
return fspath.relto(initial_path)


class FSCollector(Collector):
Expand Down
10 changes: 5 additions & 5 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,11 @@ def test_cmdline_python_namespace_package(self, testdir, monkeypatch):
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"*test_hello.py::test_hello*PASSED*",
"*test_hello.py::test_other*PASSED*",
"*test_world.py::test_world*PASSED*",
"*test_world.py::test_other*PASSED*",
"*4 passed*",
"test_hello.py::test_hello*PASSED*",
"test_hello.py::test_other*PASSED*",
"ns_pkg/world/test_world.py::test_world*PASSED*",
"ns_pkg/world/test_world.py::test_other*PASSED*",
"*4 passed in*",
]
)

Expand Down
22 changes: 22 additions & 0 deletions testing/test_nodes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import py

import pytest
from _pytest import nodes

Expand Down Expand Up @@ -29,3 +31,23 @@ def test():
)
with pytest.raises(ValueError, match=".*instance of PytestWarning.*"):
items[0].warn(UserWarning("some warning"))


def test__check_initialpaths_for_relpath():
"""Ensure that it handles dirs, and does not always use dirname."""
cwd = py.path.local()

class FakeSession:
_initialpaths = [cwd]

assert nodes._check_initialpaths_for_relpath(FakeSession, cwd) == ""

sub = cwd.join("file")

class FakeSession:
_initialpaths = [cwd]

assert nodes._check_initialpaths_for_relpath(FakeSession, sub) == "file"

outside = py.path.local("/outside")
assert nodes._check_initialpaths_for_relpath(FakeSession, outside) is None
6 changes: 5 additions & 1 deletion testing/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,11 @@ def test_one():

result = testdir.runpytest("--rootdir={}".format(path))
result.stdout.fnmatch_lines(
["*rootdir: {}/root, inifile:*".format(testdir.tmpdir), "*1 passed*"]
[
"*rootdir: {}/root, inifile:*".format(testdir.tmpdir),
"root/test_rootdir_option_arg.py *",
"*1 passed*",
]
)


Expand Down

0 comments on commit bee72a6

Please sign in to comment.