Rename pytest_collect_file fspath parameter to file_path

This commit is contained in:
Bruno Oliveira 2021-12-01 17:50:37 -03:00
parent bd2de2b442
commit 714d7f430a
13 changed files with 67 additions and 65 deletions

View File

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

View File

@ -120,18 +120,18 @@ def pytest_unconfigure() -> None:
def pytest_collect_file( def pytest_collect_file(
fspath: Path, file_path: Path,
parent: Collector, parent: Collector,
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]: ) -> Optional[Union["DoctestModule", "DoctestTextfile"]]:
config = parent.config config = parent.config
if fspath.suffix == ".py": if file_path.suffix == ".py":
if config.option.doctestmodules and not any( if config.option.doctestmodules and not any(
(_is_setup_py(fspath), _is_main_py(fspath)) (_is_setup_py(file_path), _is_main_py(file_path))
): ):
mod: DoctestModule = DoctestModule.from_parent(parent, path=fspath) mod: DoctestModule = DoctestModule.from_parent(parent, path=file_path)
return mod return mod
elif _is_doctest(config, fspath, parent): elif _is_doctest(config, file_path, parent):
txt: DoctestTextfile = DoctestTextfile.from_parent(parent, path=fspath) txt: DoctestTextfile = DoctestTextfile.from_parent(parent, path=file_path)
return txt return txt
return None return None

View File

@ -283,17 +283,17 @@ def pytest_ignore_collect(
def pytest_collect_file( def pytest_collect_file(
fspath: Path, path: "LEGACY_PATH", parent: "Collector" file_path: Path, path: "LEGACY_PATH", parent: "Collector"
) -> "Optional[Collector]": ) -> "Optional[Collector]":
"""Create a Collector for the given path, or None if not relevant. """Create a Collector for the given path, or None if not relevant.
The new node needs to have the specified ``parent`` as a parent. The new node needs to have the specified ``parent`` as a parent.
:param pathlib.Path fspath: The path to analyze. :param pathlib.Path file_path: The path to analyze.
:param LEGACY_PATH path: The path to collect (deprecated). :param LEGACY_PATH path: The path to collect (deprecated).
.. versionchanged:: 7.0.0 .. versionchanged:: 7.0.0
The ``fspath`` parameter was added as a :class:`pathlib.Path` The ``file_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

@ -586,7 +586,7 @@ class Session(nodes.FSCollector):
else: else:
duplicate_paths.add(fspath) duplicate_paths.add(fspath)
return ihook.pytest_collect_file(fspath=fspath, parent=self) # type: ignore[no-any-return] return ihook.pytest_collect_file(file_path=fspath, parent=self) # type: ignore[no-any-return]
@overload @overload
def perform_collect( def perform_collect(

View File

@ -195,15 +195,17 @@ def pytest_pyfunc_call(pyfuncitem: "Function") -> Optional[object]:
return True return True
def pytest_collect_file(fspath: Path, parent: nodes.Collector) -> Optional["Module"]: def pytest_collect_file(file_path: Path, parent: nodes.Collector) -> Optional["Module"]:
if fspath.suffix == ".py": if file_path.suffix == ".py":
if not parent.session.isinitpath(fspath): if not parent.session.isinitpath(file_path):
if not path_matches_patterns( if not path_matches_patterns(
fspath, parent.config.getini("python_files") + ["__init__.py"] file_path, parent.config.getini("python_files") + ["__init__.py"]
): ):
return None return None
ihook = parent.session.gethookproxy(fspath) ihook = parent.session.gethookproxy(file_path)
module: Module = ihook.pytest_pycollect_makemodule(fspath=fspath, parent=parent) module: Module = ihook.pytest_pycollect_makemodule(
fspath=file_path, parent=parent
)
return module return module
return None return None
@ -705,7 +707,7 @@ class Package(Module):
else: else:
duplicate_paths.add(fspath) duplicate_paths.add(fspath)
return ihook.pytest_collect_file(fspath=fspath, parent=self) # type: ignore[no-any-return] return ihook.pytest_collect_file(file_path=fspath, parent=self) # type: ignore[no-any-return]
def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]: def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]:
this_path = self.path.parent this_path = self.path.parent

View File

@ -303,9 +303,9 @@ class TestGeneralUsage:
class MyCollector(pytest.File): class MyCollector(pytest.File):
def collect(self): def collect(self):
return [MyItem.from_parent(name="xyz", parent=self)] return [MyItem.from_parent(name="xyz", parent=self)]
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.name.startswith("conftest"): if file_path.name.startswith("conftest"):
return MyCollector.from_parent(path=fspath, parent=parent) return MyCollector.from_parent(path=file_path, parent=parent)
""" """
) )
result = pytester.runpytest(c.name + "::" + "xyz") result = pytester.runpytest(c.name + "::" + "xyz")

View File

@ -114,9 +114,9 @@ def dummy_yaml_custom_test(pytester: Pytester):
""" """
import pytest import pytest
def pytest_collect_file(parent, fspath): def pytest_collect_file(parent, file_path):
if fspath.suffix == ".yaml" and fspath.name.startswith("test"): if file_path.suffix == ".yaml" and file_path.name.startswith("test"):
return YamlFile.from_parent(path=fspath, parent=parent) return YamlFile.from_parent(path=file_path, parent=parent)
class YamlFile(pytest.File): class YamlFile(pytest.File):
def collect(self): def collect(self):

View File

@ -11,5 +11,5 @@ class CustomFile(pytest.File):
yield CustomItem.from_parent(name="foo", parent=self) yield CustomItem.from_parent(name="foo", parent=self)
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
return CustomFile.from_parent(path=fspath, parent=parent) return CustomFile.from_parent(path=file_path, parent=parent)

View File

@ -6,8 +6,8 @@ class MyFile(pytest.File):
return [MyItem.from_parent(name="hello", parent=self)] return [MyItem.from_parent(name="hello", parent=self)]
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
return MyFile.from_parent(path=fspath, parent=parent) return MyFile.from_parent(path=file_path, parent=parent)
class MyItem(pytest.Item): class MyItem(pytest.Item):

View File

@ -913,9 +913,9 @@ class TestConftestCustomization:
return Loader() return Loader()
sys.meta_path.append(Finder()) sys.meta_path.append(Finder())
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".narf": if file_path.suffix == ".narf":
return Module.from_parent(path=fspath, parent=parent)""" return Module.from_parent(path=file_path, parent=parent)"""
) )
pytester.makefile( pytester.makefile(
".narf", ".narf",

View File

@ -93,9 +93,9 @@ class TestCollector:
import pytest import pytest
class CustomFile(pytest.File): class CustomFile(pytest.File):
pass pass
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".xxx": if file_path.suffix == ".xxx":
return CustomFile.from_parent(path=fspath, parent=parent) return CustomFile.from_parent(path=file_path, parent=parent)
""" """
) )
node = pytester.getpathnode(hello) node = pytester.getpathnode(hello)
@ -269,10 +269,10 @@ class TestCollectPluginHookRelay:
wascalled = [] wascalled = []
class Plugin: class Plugin:
def pytest_collect_file(self, fspath: Path) -> None: def pytest_collect_file(self, file_path: Path) -> None:
if not fspath.name.startswith("."): if not file_path.name.startswith("."):
# Ignore hidden files, e.g. .testmondata. # Ignore hidden files, e.g. .testmondata.
wascalled.append(fspath) wascalled.append(file_path)
pytester.makefile(".abc", "xyz") pytester.makefile(".abc", "xyz")
pytest.main(pytester.path, plugins=[Plugin()]) pytest.main(pytester.path, plugins=[Plugin()])
@ -290,8 +290,8 @@ class TestPrunetraceback:
pytester.makeconftest( pytester.makeconftest(
""" """
import pytest import pytest
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
return MyFile.from_parent(path=fspath, parent=parent) return MyFile.from_parent(path=file_path, parent=parent)
class MyError(Exception): class MyError(Exception):
pass pass
class MyFile(pytest.File): class MyFile(pytest.File):
@ -417,9 +417,9 @@ class TestCustomConftests:
import pytest import pytest
class MyModule(pytest.Module): class MyModule(pytest.Module):
pass pass
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".py": if file_path.suffix == ".py":
return MyModule.from_parent(path=fspath, parent=parent) return MyModule.from_parent(path=file_path, parent=parent)
""" """
) )
pytester.mkdir("sub") pytester.mkdir("sub")
@ -435,9 +435,9 @@ class TestCustomConftests:
import pytest import pytest
class MyModule1(pytest.Module): class MyModule1(pytest.Module):
pass pass
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".py": if file_path.suffix == ".py":
return MyModule1.from_parent(path=fspath, parent=parent) return MyModule1.from_parent(path=file_path, parent=parent)
""" """
) )
conf1.replace(sub1.joinpath(conf1.name)) conf1.replace(sub1.joinpath(conf1.name))
@ -446,9 +446,9 @@ class TestCustomConftests:
import pytest import pytest
class MyModule2(pytest.Module): class MyModule2(pytest.Module):
pass pass
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".py": if file_path.suffix == ".py":
return MyModule2.from_parent(path=fspath, parent=parent) return MyModule2.from_parent(path=file_path, parent=parent)
""" """
) )
conf2.replace(sub2.joinpath(conf2.name)) conf2.replace(sub2.joinpath(conf2.name))
@ -537,9 +537,9 @@ class TestSession:
class SpecialFile(pytest.File): class SpecialFile(pytest.File):
def collect(self): def collect(self):
return [SpecialItem.from_parent(name="check", parent=self)] return [SpecialItem.from_parent(name="check", parent=self)]
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.name == %r: if file_path.name == %r:
return SpecialFile.from_parent(path=fspath, parent=parent) return SpecialFile.from_parent(path=file_path, parent=parent)
""" """
% p.name % p.name
) )
@ -757,13 +757,13 @@ def test_matchnodes_two_collections_same_file(pytester: Pytester) -> None:
config.pluginmanager.register(Plugin2()) config.pluginmanager.register(Plugin2())
class Plugin2(object): class Plugin2(object):
def pytest_collect_file(self, fspath, parent): def pytest_collect_file(self, file_path, parent):
if fspath.suffix == ".abc": if file_path.suffix == ".abc":
return MyFile2.from_parent(path=fspath, parent=parent) return MyFile2.from_parent(path=file_path, parent=parent)
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".abc": if file_path.suffix == ".abc":
return MyFile1.from_parent(path=fspath, parent=parent) return MyFile1.from_parent(path=file_path, parent=parent)
class MyFile1(pytest.File): class MyFile1(pytest.File):
def collect(self): def collect(self):

View File

@ -979,9 +979,9 @@ class TestNonPython:
pytester.makeconftest( pytester.makeconftest(
""" """
import pytest import pytest
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == ".xyz": if file_path.suffix == ".xyz":
return MyItem.from_parent(name=fspath.name, parent=parent) return MyItem.from_parent(name=file_path.name, parent=parent)
class MyItem(pytest.Item): class MyItem(pytest.Item):
def runtest(self): def runtest(self):
raise ValueError(42) raise ValueError(42)
@ -1430,9 +1430,9 @@ def test_fancy_items_regression(pytester: Pytester, run_and_parse: RunAndParse)
NoFunItem.from_parent(name='b', parent=self), NoFunItem.from_parent(name='b', parent=self),
] ]
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
if fspath.suffix == '.py': if file_path.suffix == '.py':
return FunCollector.from_parent(path=fspath, parent=parent) return FunCollector.from_parent(path=file_path, parent=parent)
""" """
) )

View File

@ -1317,7 +1317,7 @@ def test_xfail_item(pytester: Pytester) -> None:
def runtest(self): def runtest(self):
pytest.xfail("Expected Failure") pytest.xfail("Expected Failure")
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
return MyItem.from_parent(name="foo", parent=parent) return MyItem.from_parent(name="foo", parent=parent)
""" """
) )
@ -1391,7 +1391,7 @@ def test_mark_xfail_item(pytester: Pytester) -> None:
def runtest(self): def runtest(self):
assert False assert False
def pytest_collect_file(fspath, parent): def pytest_collect_file(file_path, parent):
return MyItem.from_parent(name="foo", parent=parent) return MyItem.from_parent(name="foo", parent=parent)
""" """
) )