From 8532756fe549edb6ecb01733f33f714cebdc35b2 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 8 Nov 2018 20:35:53 +0100 Subject: [PATCH] Fix nodes._check_initialpaths_for_relpath for dirs Ref: https://github.com/pytest-dev/pytest/issues/4321#issuecomment-436951894 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 --- src/_pytest/nodes.py | 2 ++ testing/acceptance_test.py | 10 +++++----- testing/test_nodes.py | 18 ++++++++++++++++++ testing/test_session.py | 6 +++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 76d8d6a8aeb..b700ad47d29 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -447,6 +447,8 @@ def _prunetraceback(self, excinfo): def _check_initialpaths_for_relpath(session, fspath): for initial_path in session._initialpaths: if fspath.common(initial_path) == initial_path: + if initial_path.isdir(): + return fspath.relto(initial_path) return fspath.relto(initial_path.dirname) diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 8917de6b2ad..40caf38eb65 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -662,11 +662,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*", ] ) diff --git a/testing/test_nodes.py b/testing/test_nodes.py index d55184ef221..82b7c112e73 100644 --- a/testing/test_nodes.py +++ b/testing/test_nodes.py @@ -1,3 +1,5 @@ +import py + import pytest from _pytest import nodes @@ -29,3 +31,19 @@ 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.""" + d = py.path.local() + + class FakeSession: + _initialpaths = [d] + + assert nodes._check_initialpaths_for_relpath(FakeSession, d) == "" + + f = d.join("file") + assert nodes._check_initialpaths_for_relpath(FakeSession, f) == "file" + + outside = py.path.local("/outside") + assert nodes._check_initialpaths_for_relpath(FakeSession, outside) is None diff --git a/testing/test_session.py b/testing/test_session.py index c1785b91668..7466183088b 100644 --- a/testing/test_session.py +++ b/testing/test_session.py @@ -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*", + ] )