Rename pytest_ignore_collect fspath parameter to collection_path

This commit is contained in:
Bruno Oliveira 2021-12-01 17:43:28 -03:00
parent e2ee3144ed
commit bd2de2b442
8 changed files with 26 additions and 24 deletions

View File

@ -10,7 +10,7 @@ from _pytest.nodes import _check_path
# hookname: (Path, LEGACY_PATH) # hookname: (Path, LEGACY_PATH)
imply_paths_hooks = { imply_paths_hooks = {
"pytest_ignore_collect": ("fspath", "path"), "pytest_ignore_collect": ("collection_path", "path"),
"pytest_collect_file": ("fspath", "path"), "pytest_collect_file": ("fspath", "path"),
"pytest_pycollect_makemodule": ("fspath", "path"), "pytest_pycollect_makemodule": ("fspath", "path"),
"pytest_report_header": ("startpath", "startdir"), "pytest_report_header": ("startpath", "startdir"),

View File

@ -262,7 +262,7 @@ def pytest_collection_finish(session: "Session") -> None:
@hookspec(firstresult=True) @hookspec(firstresult=True)
def pytest_ignore_collect( def pytest_ignore_collect(
fspath: Path, path: "LEGACY_PATH", config: "Config" collection_path: Path, path: "LEGACY_PATH", config: "Config"
) -> Optional[bool]: ) -> Optional[bool]:
"""Return True to prevent considering this path for collection. """Return True to prevent considering this path for collection.
@ -271,12 +271,12 @@ def pytest_ignore_collect(
Stops at first non-None result, see :ref:`firstresult`. Stops at first non-None result, see :ref:`firstresult`.
:param pathlib.Path fspath: The path to analyze. :param pathlib.Path collection_path : The path to analyze.
:param LEGACY_PATH path: The path to analyze (deprecated). :param LEGACY_PATH path: The path to analyze (deprecated).
:param pytest.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
.. versionchanged:: 7.0.0 .. versionchanged:: 7.0.0
The ``fspath`` parameter was added as a :class:`pathlib.Path` The ``collection_path`` parameter was added as a :class:`pathlib.Path`
equivalent of the ``path`` parameter. The ``path`` parameter equivalent of the ``path`` parameter. The ``path`` parameter
has been deprecated. has been deprecated.
""" """

View File

@ -372,31 +372,31 @@ def _in_venv(path: Path) -> bool:
return any(fname.name in activates for fname in bindir.iterdir()) return any(fname.name in activates for fname in bindir.iterdir())
def pytest_ignore_collect(fspath: Path, config: Config) -> Optional[bool]: def pytest_ignore_collect(collection_path: Path, config: Config) -> Optional[bool]:
ignore_paths = config._getconftest_pathlist( ignore_paths = config._getconftest_pathlist(
"collect_ignore", path=fspath.parent, rootpath=config.rootpath "collect_ignore", path=collection_path.parent, rootpath=config.rootpath
) )
ignore_paths = ignore_paths or [] ignore_paths = ignore_paths or []
excludeopt = config.getoption("ignore") excludeopt = config.getoption("ignore")
if excludeopt: if excludeopt:
ignore_paths.extend(absolutepath(x) for x in excludeopt) ignore_paths.extend(absolutepath(x) for x in excludeopt)
if fspath in ignore_paths: if collection_path in ignore_paths:
return True return True
ignore_globs = config._getconftest_pathlist( ignore_globs = config._getconftest_pathlist(
"collect_ignore_glob", path=fspath.parent, rootpath=config.rootpath "collect_ignore_glob", path=collection_path.parent, rootpath=config.rootpath
) )
ignore_globs = ignore_globs or [] ignore_globs = ignore_globs or []
excludeglobopt = config.getoption("ignore_glob") excludeglobopt = config.getoption("ignore_glob")
if excludeglobopt: if excludeglobopt:
ignore_globs.extend(absolutepath(x) for x in excludeglobopt) ignore_globs.extend(absolutepath(x) for x in excludeglobopt)
if any(fnmatch.fnmatch(str(fspath), str(glob)) for glob in ignore_globs): if any(fnmatch.fnmatch(str(collection_path), str(glob)) for glob in ignore_globs):
return True return True
allow_in_venv = config.getoption("collect_in_virtualenv") allow_in_venv = config.getoption("collect_in_virtualenv")
if not allow_in_venv and _in_venv(fspath): if not allow_in_venv and _in_venv(collection_path):
return True return True
return None return None
@ -557,7 +557,7 @@ class Session(nodes.FSCollector):
return False return False
fspath = Path(direntry.path) fspath = Path(direntry.path)
ihook = self.gethookproxy(fspath.parent) ihook = self.gethookproxy(fspath.parent)
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config): if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
return False return False
norecursepatterns = self.config.getini("norecursedirs") norecursepatterns = self.config.getini("norecursedirs")
if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns): if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns):
@ -574,7 +574,7 @@ class Session(nodes.FSCollector):
) )
ihook = self.gethookproxy(fspath) ihook = self.gethookproxy(fspath)
if not self.isinitpath(fspath): if not self.isinitpath(fspath):
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config): if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
return () return ()
if handle_dupes: if handle_dupes:

View File

@ -676,7 +676,7 @@ class Package(Module):
return False return False
fspath = Path(direntry.path) fspath = Path(direntry.path)
ihook = self.session.gethookproxy(fspath.parent) ihook = self.session.gethookproxy(fspath.parent)
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config): if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
return False return False
norecursepatterns = self.config.getini("norecursedirs") norecursepatterns = self.config.getini("norecursedirs")
if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns): if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns):
@ -693,7 +693,7 @@ class Package(Module):
) )
ihook = self.session.gethookproxy(fspath) ihook = self.session.gethookproxy(fspath)
if not self.session.isinitpath(fspath): if not self.session.isinitpath(fspath):
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config): if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
return () return ()
if handle_dupes: if handle_dupes:

View File

@ -160,10 +160,10 @@ def test_raising_unittest_skiptest_during_collection_is_deprecated(
@pytest.mark.parametrize("hooktype", ["hook", "ihook"]) @pytest.mark.parametrize("hooktype", ["hook", "ihook"])
def test_hookproxy_warnings_for_fspath(tmp_path, hooktype, request): def test_hookproxy_warnings_for_pathlib(tmp_path, hooktype, request):
path = legacy_path(tmp_path) path = legacy_path(tmp_path)
PATH_WARN_MATCH = r".*path: py\.path\.local\) argument is deprecated, please use \(fspath: pathlib\.Path.*" PATH_WARN_MATCH = r".*path: py\.path\.local\) argument is deprecated, please use \(collection_path: pathlib\.Path.*"
if hooktype == "ihook": if hooktype == "ihook":
hooks = request.node.ihook hooks = request.node.ihook
else: else:
@ -171,20 +171,22 @@ def test_hookproxy_warnings_for_fspath(tmp_path, hooktype, request):
with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r: with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r:
l1 = sys._getframe().f_lineno l1 = sys._getframe().f_lineno
hooks.pytest_ignore_collect(config=request.config, path=path, fspath=tmp_path) hooks.pytest_ignore_collect(
config=request.config, path=path, collection_path=tmp_path
)
l2 = sys._getframe().f_lineno l2 = sys._getframe().f_lineno
(record,) = r (record,) = r
assert record.filename == __file__ assert record.filename == __file__
assert l1 < record.lineno < l2 assert l1 < record.lineno < l2
hooks.pytest_ignore_collect(config=request.config, fspath=tmp_path) hooks.pytest_ignore_collect(config=request.config, collection_path=tmp_path)
# Passing entirely *different* paths is an outright error. # Passing entirely *different* paths is an outright error.
with pytest.raises(ValueError, match=r"path.*fspath.*need to be equal"): with pytest.raises(ValueError, match=r"path.*fspath.*need to be equal"):
with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r: with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r:
hooks.pytest_ignore_collect( hooks.pytest_ignore_collect(
config=request.config, path=path, fspath=Path("/bla/bla") config=request.config, path=path, collection_path=Path("/bla/bla")
) )

View File

@ -1,2 +1,2 @@
def pytest_ignore_collect(fspath): def pytest_ignore_collect(collection_path):
return False return False

View File

@ -333,8 +333,8 @@ class TestCustomConftests:
def test_ignore_collect_path(self, pytester: Pytester) -> None: def test_ignore_collect_path(self, pytester: Pytester) -> None:
pytester.makeconftest( pytester.makeconftest(
""" """
def pytest_ignore_collect(fspath, config): def pytest_ignore_collect(collection_path, config):
return fspath.name.startswith("x") or fspath.name == "test_one.py" return collection_path.name.startswith("x") or collection_path.name == "test_one.py"
""" """
) )
sub = pytester.mkdir("xy123") sub = pytester.mkdir("xy123")
@ -349,7 +349,7 @@ class TestCustomConftests:
def test_ignore_collect_not_called_on_argument(self, pytester: Pytester) -> None: def test_ignore_collect_not_called_on_argument(self, pytester: Pytester) -> None:
pytester.makeconftest( pytester.makeconftest(
""" """
def pytest_ignore_collect(fspath, config): def pytest_ignore_collect(collection_path, config):
return True return True
""" """
) )

View File

@ -667,7 +667,7 @@ def test_hook_proxy(pytester: Pytester) -> None:
"root/demo-0/test_foo1.py": "def test1(): pass", "root/demo-0/test_foo1.py": "def test1(): pass",
"root/demo-a/test_foo2.py": "def test1(): pass", "root/demo-a/test_foo2.py": "def test1(): pass",
"root/demo-a/conftest.py": """\ "root/demo-a/conftest.py": """\
def pytest_ignore_collect(fspath, config): def pytest_ignore_collect(collection_path, config):
return True return True
""", """,
"root/demo-b/test_foo3.py": "def test1(): pass", "root/demo-b/test_foo3.py": "def test1(): pass",