summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRan Benita <ran@unusedvar.com>2020-08-02 17:56:36 +0300
committerRan Benita <ran@unusedvar.com>2020-08-06 18:46:17 +0300
commitf8c4e038fde9e58732ef6ffad77cc25d0746cbc3 (patch)
tree4b6b1a00d631f73cb38ecf6b3423a4e6a2ad1a99
parent70f3ad1c1f31b35d4004f92734b4afd6c8fbdecf (diff)
downloadpytest-f8c4e038fde9e58732ef6ffad77cc25d0746cbc3.tar.gz
Replace some usages of py.path.local
-rw-r--r--extra/get_issues.py8
-rw-r--r--src/_pytest/_code/code.py27
-rw-r--r--src/_pytest/compat.py14
-rw-r--r--src/_pytest/config/__init__.py4
-rw-r--r--src/_pytest/fixtures.py5
-rw-r--r--src/_pytest/python.py6
-rw-r--r--src/_pytest/resultlog.py4
-rw-r--r--testing/acceptance_test.py2
8 files changed, 41 insertions, 29 deletions
diff --git a/extra/get_issues.py b/extra/get_issues.py
index c264b2644..4aaa3c3ec 100644
--- a/extra/get_issues.py
+++ b/extra/get_issues.py
@@ -1,6 +1,6 @@
import json
+from pathlib import Path
-import py
import requests
issues_url = "https://api.github.com/repos/pytest-dev/pytest/issues"
@@ -31,12 +31,12 @@ def get_issues():
def main(args):
- cachefile = py.path.local(args.cache)
+ cachefile = Path(args.cache)
if not cachefile.exists() or args.refresh:
issues = get_issues()
- cachefile.write(json.dumps(issues))
+ cachefile.write_text(json.dumps(issues), "utf-8")
else:
- issues = json.loads(cachefile.read())
+ issues = json.loads(cachefile.read_text("utf-8"))
open_issues = [x for x in issues if x["state"] == "open"]
diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py
index b2e4fcd33..420135b4e 100644
--- a/src/_pytest/_code/code.py
+++ b/src/_pytest/_code/code.py
@@ -41,6 +41,7 @@ from _pytest.compat import ATTRS_EQ_FIELD
from _pytest.compat import get_real_func
from _pytest.compat import overload
from _pytest.compat import TYPE_CHECKING
+from _pytest.pathlib import Path
if TYPE_CHECKING:
from typing import Type
@@ -1190,12 +1191,12 @@ def getfslineno(obj: object) -> Tuple[Union[str, py.path.local], int]:
# note: if we need to add more paths than what we have now we should probably use a list
# for better maintenance.
-_PLUGGY_DIR = py.path.local(pluggy.__file__.rstrip("oc"))
+_PLUGGY_DIR = Path(pluggy.__file__.rstrip("oc"))
# pluggy is either a package or a single module depending on the version
-if _PLUGGY_DIR.basename == "__init__.py":
- _PLUGGY_DIR = _PLUGGY_DIR.dirpath()
-_PYTEST_DIR = py.path.local(_pytest.__file__).dirpath()
-_PY_DIR = py.path.local(py.__file__).dirpath()
+if _PLUGGY_DIR.name == "__init__.py":
+ _PLUGGY_DIR = _PLUGGY_DIR.parent
+_PYTEST_DIR = Path(_pytest.__file__).parent
+_PY_DIR = Path(py.__file__).parent
def filter_traceback(entry: TracebackEntry) -> bool:
@@ -1213,9 +1214,17 @@ def filter_traceback(entry: TracebackEntry) -> bool:
is_generated = "<" in raw_filename and ">" in raw_filename
if is_generated:
return False
+
# entry.path might point to a non-existing file, in which case it will
# also return a str object. See #1133.
- p = py.path.local(entry.path)
- return (
- not p.relto(_PLUGGY_DIR) and not p.relto(_PYTEST_DIR) and not p.relto(_PY_DIR)
- )
+ p = Path(entry.path)
+
+ parents = p.parents
+ if _PLUGGY_DIR in parents:
+ return False
+ if _PYTEST_DIR in parents:
+ return False
+ if _PY_DIR in parents:
+ return False
+
+ return True
diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py
index ff98492dc..4b46d9c95 100644
--- a/src/_pytest/compat.py
+++ b/src/_pytest/compat.py
@@ -18,7 +18,6 @@ from typing import TypeVar
from typing import Union
import attr
-import py
from _pytest._io.saferepr import saferepr
from _pytest.outcomes import fail
@@ -104,13 +103,18 @@ def is_async_function(func: object) -> bool:
)
-def getlocation(function, curdir=None) -> str:
+def getlocation(function, curdir: Optional[str] = None) -> str:
+ from _pytest.pathlib import Path
+
function = get_real_func(function)
- fn = py.path.local(inspect.getfile(function))
+ fn = Path(inspect.getfile(function))
lineno = function.__code__.co_firstlineno
if curdir is not None:
- relfn = fn.relto(curdir)
- if relfn:
+ try:
+ relfn = fn.relative_to(curdir)
+ except ValueError:
+ pass
+ else:
return "%s:%d" % (relfn, lineno + 1)
return "%s:%d" % (fn, lineno + 1)
diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py
index 6305cdbd5..453dd8345 100644
--- a/src/_pytest/config/__init__.py
+++ b/src/_pytest/config/__init__.py
@@ -123,7 +123,7 @@ def filter_traceback_for_conftest_import_failure(
def main(
- args: Optional[List[str]] = None,
+ args: Optional[Union[List[str], py.path.local]] = None,
plugins: Optional[Sequence[Union[str, _PluggyPlugin]]] = None,
) -> Union[int, ExitCode]:
"""Perform an in-process test run.
@@ -1308,7 +1308,7 @@ class Config:
values = [] # type: List[py.path.local]
for relroot in relroots:
if not isinstance(relroot, py.path.local):
- relroot = relroot.replace("/", py.path.local.sep)
+ relroot = relroot.replace("/", os.sep)
relroot = modpath.join(relroot, abs=True)
values.append(relroot)
return values
diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py
index d2ff6203b..846cc2bb1 100644
--- a/src/_pytest/fixtures.py
+++ b/src/_pytest/fixtures.py
@@ -1,5 +1,6 @@
import functools
import inspect
+import os
import sys
import warnings
from collections import defaultdict
@@ -1515,8 +1516,8 @@ class FixtureManager:
# by their test id).
if p.basename.startswith("conftest.py"):
nodeid = p.dirpath().relto(self.config.rootdir)
- if p.sep != nodes.SEP:
- nodeid = nodeid.replace(p.sep, nodes.SEP)
+ if os.sep != nodes.SEP:
+ nodeid = nodeid.replace(os.sep, nodes.SEP)
self.parsefactories(plugin, nodeid)
diff --git a/src/_pytest/python.py b/src/_pytest/python.py
index 741624565..0661f3402 100644
--- a/src/_pytest/python.py
+++ b/src/_pytest/python.py
@@ -1339,7 +1339,7 @@ def _show_fixtures_per_test(config: Config, session: Session) -> None:
verbose = config.getvalue("verbose")
def get_best_relpath(func):
- loc = getlocation(func, curdir)
+ loc = getlocation(func, str(curdir))
return curdir.bestrelpath(py.path.local(loc))
def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None:
@@ -1404,7 +1404,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
if not fixturedefs:
continue
for fixturedef in fixturedefs:
- loc = getlocation(fixturedef.func, curdir)
+ loc = getlocation(fixturedef.func, str(curdir))
if (fixturedef.argname, loc) in seen:
continue
seen.add((fixturedef.argname, loc))
@@ -1434,7 +1434,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
if verbose > 0:
tw.write(" -- %s" % bestrel, yellow=True)
tw.write("\n")
- loc = getlocation(fixturedef.func, curdir)
+ loc = getlocation(fixturedef.func, str(curdir))
doc = inspect.getdoc(fixturedef.func)
if doc:
write_docstring(tw, doc)
diff --git a/src/_pytest/resultlog.py b/src/_pytest/resultlog.py
index c043c749f..686f7f3b0 100644
--- a/src/_pytest/resultlog.py
+++ b/src/_pytest/resultlog.py
@@ -3,8 +3,6 @@ import os
from typing import IO
from typing import Union
-import py
-
from _pytest._code.code import ExceptionRepr
from _pytest.config import Config
from _pytest.config.argparsing import Parser
@@ -106,5 +104,5 @@ class ResultLog:
if excrepr.reprcrash is not None:
path = excrepr.reprcrash.path
else:
- path = "cwd:%s" % py.path.local()
+ path = "cwd:%s" % os.getcwd()
self.write_log_entry(path, "!", str(excrepr))
diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py
index 3172dad7c..b37cfa0cb 100644
--- a/testing/acceptance_test.py
+++ b/testing/acceptance_test.py
@@ -586,7 +586,7 @@ class TestInvocationVariants:
):
pytest.main("-h") # type: ignore[arg-type]
- def test_invoke_with_path(self, tmpdir, capsys):
+ def test_invoke_with_path(self, tmpdir: py.path.local, capsys) -> None:
retcode = pytest.main(tmpdir)
assert retcode == ExitCode.NO_TESTS_COLLECTED
out, err = capsys.readouterr()