Skip to content

Commit

Permalink
Move Node.fspath to legacypath plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
bluetech committed Oct 28, 2021
1 parent 8d52ce3 commit 301c830
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/_pytest/legacypath.py
Expand Up @@ -15,6 +15,7 @@
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path
from _pytest.deprecated import check_ispytest
from _pytest.nodes import Node
from _pytest.terminal import TerminalReporter

if TYPE_CHECKING:
Expand Down Expand Up @@ -386,6 +387,15 @@ def Config__getini_unknown_type(
raise ValueError(f"unknown configuration type: {type}", value)


def Node_fspath(self: Node) -> LEGACY_PATH:
"""(deprecated) returns a legacy_path copy of self.path"""
return legacy_path(self.path)


def Node_fspath_set(self: Node, value: LEGACY_PATH) -> None:
self.path = Path(value)


def pytest_configure(config: pytest.Config) -> None:
mp = pytest.MonkeyPatch()
config.add_cleanup(mp.undo)
Expand Down Expand Up @@ -429,3 +439,6 @@ def pytest_configure(config: pytest.Config) -> None:

# Add pathlist configuration type.
mp.setattr(pytest.Config, "_getini_unknown_type", Config__getini_unknown_type)

# Add Node.fspath property.
mp.setattr(Node, "fspath", property(Node_fspath, Node_fspath_set), raising=False)
10 changes: 0 additions & 10 deletions src/_pytest/nodes.py
Expand Up @@ -24,7 +24,6 @@
from _pytest._code.code import TerminalRepr
from _pytest.compat import cached_property
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path
from _pytest.config import Config
from _pytest.config import ConftestImportFailure
from _pytest.deprecated import FSCOLLECTOR_GETHOOKPROXY_ISINITPATH
Expand Down Expand Up @@ -238,15 +237,6 @@ def __init__(
# Deprecated alias. Was never public. Can be removed in a few releases.
self._store = self.stash

@property
def fspath(self) -> LEGACY_PATH:
"""(deprecated) returns a legacy_path copy of self.path"""
return legacy_path(self.path)

@fspath.setter
def fspath(self, value: LEGACY_PATH) -> None:
self.path = Path(value)

@classmethod
def from_parent(cls, parent: "Node", **kw):
"""Public constructor for Nodes.
Expand Down
12 changes: 12 additions & 0 deletions testing/test_legacypath.py
Expand Up @@ -6,6 +6,18 @@
from _pytest.legacypath import Testdir


def test_item_fspath(pytester: pytest.Pytester) -> None:
pytester.makepyfile("def test_func(): pass")
items, hookrec = pytester.inline_genitems()
assert len(items) == 1
(item,) = items
items2, hookrec = pytester.inline_genitems(item.nodeid)
(item2,) = items2
assert item2.name == item.name
assert item2.fspath == item.fspath # type: ignore[attr-defined]
assert item2.path == item.path


def test_testdir_testtmproot(testdir: Testdir) -> None:
"""Check test_tmproot is a py.path attribute for backward compatibility."""
assert testdir.test_tmproot.check(dir=1)
Expand Down

0 comments on commit 301c830

Please sign in to comment.