config: make `collect_ignore` accept any PathLike

The main reason is to remove a reference to `py.path`.
This commit is contained in:
Ran Benita 2021-03-14 22:50:27 +02:00
parent b26d1bb18f
commit a03ee02817
3 changed files with 12 additions and 7 deletions

View File

@ -936,7 +936,7 @@ pytest treats some global variables in a special manner when defined in a test m
**Tutorial**: :ref:`customizing-test-collection`
Can be declared in *conftest.py files* to exclude test directories or modules.
Needs to be ``list[str]``.
Needs to be a list of paths (``str``, :class:`pathlib.Path` or any :class:`os.PathLike`).
.. code-block:: python

View File

@ -1445,9 +1445,7 @@ class Config:
modpath = Path(mod.__file__).parent
values: List[Path] = []
for relroot in relroots:
if isinstance(relroot, Path):
pass
elif isinstance(relroot, LEGACY_PATH):
if isinstance(relroot, os.PathLike):
relroot = Path(relroot)
else:
relroot = relroot.replace("/", os.sep)

View File

@ -367,12 +367,19 @@ class TestCustomConftests:
def test_collectignore_exclude_on_option(self, pytester: Pytester) -> None:
pytester.makeconftest(
"""
# potentially avoid dependency on pylib
from _pytest.compat import legacy_path
from pathlib import Path
collect_ignore = [legacy_path('hello'), 'test_world.py', Path('bye')]
class MyPathLike:
def __init__(self, path):
self.path = path
def __fspath__(self):
return "path"
collect_ignore = [MyPathLike('hello'), 'test_world.py', Path('bye')]
def pytest_addoption(parser):
parser.addoption("--XX", action="store_true", default=False)
def pytest_configure(config):
if config.getvalue("XX"):
collect_ignore[:] = []