diff --git a/src/_pytest/config/compat.py b/src/_pytest/config/compat.py index 731641dde..f5f262ab5 100644 --- a/src/_pytest/config/compat.py +++ b/src/_pytest/config/compat.py @@ -10,7 +10,7 @@ from _pytest.nodes import _check_path # hookname: (Path, LEGACY_PATH) imply_paths_hooks = { - "pytest_ignore_collect": ("fspath", "path"), + "pytest_ignore_collect": ("collection_path", "path"), "pytest_collect_file": ("fspath", "path"), "pytest_pycollect_makemodule": ("fspath", "path"), "pytest_report_header": ("startpath", "startdir"), diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index ee9553e0f..df3b3bfc3 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -262,7 +262,7 @@ def pytest_collection_finish(session: "Session") -> None: @hookspec(firstresult=True) def pytest_ignore_collect( - fspath: Path, path: "LEGACY_PATH", config: "Config" + collection_path: Path, path: "LEGACY_PATH", config: "Config" ) -> Optional[bool]: """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`. - :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 pytest.Config config: The pytest config object. .. 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 has been deprecated. """ diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 5c33fc644..1042a5bb0 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -372,31 +372,31 @@ def _in_venv(path: Path) -> bool: 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( - "collect_ignore", path=fspath.parent, rootpath=config.rootpath + "collect_ignore", path=collection_path.parent, rootpath=config.rootpath ) ignore_paths = ignore_paths or [] excludeopt = config.getoption("ignore") if excludeopt: ignore_paths.extend(absolutepath(x) for x in excludeopt) - if fspath in ignore_paths: + if collection_path in ignore_paths: return True 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 [] excludeglobopt = config.getoption("ignore_glob") if 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 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 None @@ -557,7 +557,7 @@ class Session(nodes.FSCollector): return False fspath = Path(direntry.path) 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 norecursepatterns = self.config.getini("norecursedirs") if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns): @@ -574,7 +574,7 @@ class Session(nodes.FSCollector): ) ihook = self.gethookproxy(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 () if handle_dupes: diff --git a/src/_pytest/python.py b/src/_pytest/python.py index d9fccde9a..caa13693d 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -676,7 +676,7 @@ class Package(Module): return False fspath = Path(direntry.path) 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 norecursepatterns = self.config.getini("norecursedirs") if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns): @@ -693,7 +693,7 @@ class Package(Module): ) ihook = self.session.gethookproxy(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 () if handle_dupes: diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index 38af26d58..7d7e6d312 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -160,10 +160,10 @@ def test_raising_unittest_skiptest_during_collection_is_deprecated( @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_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": hooks = request.node.ihook 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: 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 (record,) = r assert record.filename == __file__ 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. with pytest.raises(ValueError, match=r"path.*fspath.*need to be equal"): with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r: hooks.pytest_ignore_collect( - config=request.config, path=path, fspath=Path("/bla/bla") + config=request.config, path=path, collection_path=Path("/bla/bla") ) diff --git a/testing/example_scripts/collect/package_infinite_recursion/conftest.py b/testing/example_scripts/collect/package_infinite_recursion/conftest.py index d9e7a89bd..973ccc0c0 100644 --- a/testing/example_scripts/collect/package_infinite_recursion/conftest.py +++ b/testing/example_scripts/collect/package_infinite_recursion/conftest.py @@ -1,2 +1,2 @@ -def pytest_ignore_collect(fspath): +def pytest_ignore_collect(collection_path): return False diff --git a/testing/test_collection.py b/testing/test_collection.py index 6d30a8a7a..a1a1fdcb5 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -333,8 +333,8 @@ class TestCustomConftests: def test_ignore_collect_path(self, pytester: Pytester) -> None: pytester.makeconftest( """ - def pytest_ignore_collect(fspath, config): - return fspath.name.startswith("x") or fspath.name == "test_one.py" + def pytest_ignore_collect(collection_path, config): + return collection_path.name.startswith("x") or collection_path.name == "test_one.py" """ ) sub = pytester.mkdir("xy123") @@ -349,7 +349,7 @@ class TestCustomConftests: def test_ignore_collect_not_called_on_argument(self, pytester: Pytester) -> None: pytester.makeconftest( """ - def pytest_ignore_collect(fspath, config): + def pytest_ignore_collect(collection_path, config): return True """ ) diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 3ccdeb964..64c1014a5 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -667,7 +667,7 @@ def test_hook_proxy(pytester: Pytester) -> None: "root/demo-0/test_foo1.py": "def test1(): pass", "root/demo-a/test_foo2.py": "def test1(): pass", "root/demo-a/conftest.py": """\ - def pytest_ignore_collect(fspath, config): + def pytest_ignore_collect(collection_path, config): return True """, "root/demo-b/test_foo3.py": "def test1(): pass",