Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions extra/get_issues.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"]

Expand Down
27 changes: 18 additions & 9 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta love Path.parents. 😁

if _PLUGGY_DIR in parents:
return False
if _PYTEST_DIR in parents:
return False
if _PY_DIR in parents:
return False

return True
14 changes: 9 additions & 5 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from typing import Union

import attr
import py

from _pytest._io.saferepr import saferepr
from _pytest.outcomes import fail
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 6 additions & 3 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1006,12 +1006,15 @@ def _initini(self, args: Sequence[str]) -> None:
ns, unknown_args = self._parser.parse_known_and_unknown_args(
args, namespace=copy.copy(self.option)
)
self.rootdir, self.inifile, self.inicfg = determine_setup(
rootpath, inipath, inicfg = determine_setup(
ns.inifilename,
ns.file_or_dir + unknown_args,
rootdir_cmd_arg=ns.rootdir or None,
config=self,
)
self.rootdir = py.path.local(str(rootpath))
self.inifile = py.path.local(str(inipath)) if inipath else None
self.inicfg = inicfg
self._parser.extra_info["rootdir"] = self.rootdir
self._parser.extra_info["inifile"] = self.inifile
self._parser.addini("addopts", "extra command line options", "args")
Expand Down Expand Up @@ -1305,7 +1308,7 @@ def _getconftest_pathlist(
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
Expand Down
98 changes: 60 additions & 38 deletions src/_pytest/config/findpaths.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import itertools
import os
import sys
from typing import Dict
from typing import Iterable
from typing import List
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import Union

import iniconfig
import py

from .exceptions import UsageError
from _pytest.compat import TYPE_CHECKING
from _pytest.outcomes import fail
from _pytest.pathlib import absolutepath
from _pytest.pathlib import commonpath
from _pytest.pathlib import Path

if TYPE_CHECKING:
from . import Config


def _parse_ini_config(path: py.path.local) -> iniconfig.IniConfig:
def _parse_ini_config(path: Path) -> iniconfig.IniConfig:
"""Parse the given generic '.ini' file using legacy IniConfig parser, returning
the parsed object.

Expand All @@ -30,26 +35,26 @@ def _parse_ini_config(path: py.path.local) -> iniconfig.IniConfig:


def load_config_dict_from_file(
filepath: py.path.local,
filepath: Path,
) -> Optional[Dict[str, Union[str, List[str]]]]:
"""Load pytest configuration from the given file path, if supported.

Return None if the file does not contain valid pytest configuration.
"""

# Configuration from ini files are obtained from the [pytest] section, if present.
if filepath.ext == ".ini":
if filepath.suffix == ".ini":
iniconfig = _parse_ini_config(filepath)

if "pytest" in iniconfig:
return dict(iniconfig["pytest"].items())
else:
# "pytest.ini" files are always the source of configuration, even if empty.
if filepath.basename == "pytest.ini":
if filepath.name == "pytest.ini":
return {}

# '.cfg' files are considered if they contain a "[tool:pytest]" section.
elif filepath.ext == ".cfg":
elif filepath.suffix == ".cfg":
iniconfig = _parse_ini_config(filepath)

if "tool:pytest" in iniconfig.sections:
Expand All @@ -60,7 +65,7 @@ def load_config_dict_from_file(
fail(CFG_PYTEST_SECTION.format(filename="setup.cfg"), pytrace=False)

# '.toml' files are considered if they contain a [tool.pytest.ini_options] table.
elif filepath.ext == ".toml":
elif filepath.suffix == ".toml":
import toml

config = toml.load(str(filepath))
Expand All @@ -79,9 +84,9 @@ def make_scalar(v: object) -> Union[str, List[str]]:


def locate_config(
args: Iterable[Union[str, py.path.local]]
args: Iterable[Path],
) -> Tuple[
Optional[py.path.local], Optional[py.path.local], Dict[str, Union[str, List[str]]],
Optional[Path], Optional[Path], Dict[str, Union[str, List[str]]],
]:
"""Search in the list of arguments for a valid ini-file for pytest,
and return a tuple of (rootdir, inifile, cfg-dict)."""
Expand All @@ -93,104 +98,121 @@ def locate_config(
]
args = [x for x in args if not str(x).startswith("-")]
if not args:
args = [py.path.local()]
args = [Path.cwd()]
for arg in args:
arg = py.path.local(arg)
for base in arg.parts(reverse=True):
argpath = absolutepath(arg)
for base in itertools.chain((argpath,), reversed(argpath.parents)):
for config_name in config_names:
p = base.join(config_name)
if p.isfile():
p = base / config_name
if p.is_file():
ini_config = load_config_dict_from_file(p)
if ini_config is not None:
return base, p, ini_config
return None, None, {}


def get_common_ancestor(paths: Iterable[py.path.local]) -> py.path.local:
common_ancestor = None # type: Optional[py.path.local]
def get_common_ancestor(paths: Iterable[Path]) -> Path:
common_ancestor = None # type: Optional[Path]
for path in paths:
if not path.exists():
continue
if common_ancestor is None:
common_ancestor = path
else:
if path.relto(common_ancestor) or path == common_ancestor:
if common_ancestor in path.parents or path == common_ancestor:
continue
elif common_ancestor.relto(path):
elif path in common_ancestor.parents:
common_ancestor = path
else:
shared = path.common(common_ancestor)
shared = commonpath(path, common_ancestor)
if shared is not None:
common_ancestor = shared
if common_ancestor is None:
common_ancestor = py.path.local()
elif common_ancestor.isfile():
common_ancestor = common_ancestor.dirpath()
common_ancestor = Path.cwd()
elif common_ancestor.is_file():
common_ancestor = common_ancestor.parent
return common_ancestor


def get_dirs_from_args(args: Iterable[str]) -> List[py.path.local]:
def get_dirs_from_args(args: Iterable[str]) -> List[Path]:
def is_option(x: str) -> bool:
return x.startswith("-")

def get_file_part_from_node_id(x: str) -> str:
return x.split("::")[0]

def get_dir_from_path(path: py.path.local) -> py.path.local:
if path.isdir():
def get_dir_from_path(path: Path) -> Path:
if path.is_dir():
return path
return py.path.local(path.dirname)
return path.parent

if sys.version_info < (3, 8):

def safe_exists(path: Path) -> bool:
# On Python<3.8, this can throw on paths that contain characters
# unrepresentable at the OS level.
try:
return path.exists()
except OSError:
return False

else:

def safe_exists(path: Path) -> bool:
return path.exists()

# These look like paths but may not exist
possible_paths = (
py.path.local(get_file_part_from_node_id(arg))
absolutepath(get_file_part_from_node_id(arg))
for arg in args
if not is_option(arg)
)

return [get_dir_from_path(path) for path in possible_paths if path.exists()]
return [get_dir_from_path(path) for path in possible_paths if safe_exists(path)]


CFG_PYTEST_SECTION = "[pytest] section in {filename} files is no longer supported, change to [tool:pytest] instead."


def determine_setup(
inifile: Optional[str],
args: List[str],
args: Sequence[str],
rootdir_cmd_arg: Optional[str] = None,
config: Optional["Config"] = None,
) -> Tuple[py.path.local, Optional[py.path.local], Dict[str, Union[str, List[str]]]]:
) -> Tuple[Path, Optional[Path], Dict[str, Union[str, List[str]]]]:
rootdir = None
dirs = get_dirs_from_args(args)
if inifile:
inipath_ = py.path.local(inifile)
inipath = inipath_ # type: Optional[py.path.local]
inipath_ = absolutepath(inifile)
inipath = inipath_ # type: Optional[Path]
inicfg = load_config_dict_from_file(inipath_) or {}
if rootdir_cmd_arg is None:
rootdir = get_common_ancestor(dirs)
else:
ancestor = get_common_ancestor(dirs)
rootdir, inipath, inicfg = locate_config([ancestor])
if rootdir is None and rootdir_cmd_arg is None:
for possible_rootdir in ancestor.parts(reverse=True):
if possible_rootdir.join("setup.py").exists():
for possible_rootdir in itertools.chain(
(ancestor,), reversed(ancestor.parents)
):
if (possible_rootdir / "setup.py").is_file():
rootdir = possible_rootdir
break
else:
if dirs != [ancestor]:
rootdir, inipath, inicfg = locate_config(dirs)
if rootdir is None:
if config is not None:
cwd = config.invocation_dir
cwd = config.invocation_params.dir
else:
cwd = py.path.local()
cwd = Path.cwd()
rootdir = get_common_ancestor([cwd, ancestor])
is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
if is_fs_root:
rootdir = ancestor
if rootdir_cmd_arg:
rootdir = py.path.local(os.path.expandvars(rootdir_cmd_arg))
if not rootdir.isdir():
rootdir = absolutepath(os.path.expandvars(rootdir_cmd_arg))
if not rootdir.is_dir():
raise UsageError(
"Directory '{}' not found. Check your '--rootdir' option.".format(
rootdir
Expand Down
5 changes: 3 additions & 2 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import inspect
import os
import sys
import warnings
from collections import defaultdict
Expand Down Expand Up @@ -1515,8 +1516,8 @@ def pytest_plugin_registered(self, plugin: _PluggyPlugin) -> None:
# 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)

Expand Down
Loading