Skip to content

Commit 0c32c88

Browse files
committed
main: add with_parents parameter to isinitpath
Will be used in upcoming commit.
1 parent 2d1710e commit 0c32c88

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/_pytest/main.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ def __init__(self, config: Config) -> None:
499499
self.shouldfail: Union[bool, str] = False
500500
self.trace = config.trace.root.get("collection")
501501
self._initialpaths: FrozenSet[Path] = frozenset()
502+
self._initialpaths_with_parents: FrozenSet[Path] = frozenset()
502503

503504
self._bestrelpathcache: Dict[Path, str] = _bestrelpath_cache(config.rootpath)
504505

@@ -549,10 +550,29 @@ def pytest_runtest_logreport(
549550

550551
pytest_collectreport = pytest_runtest_logreport
551552

552-
def isinitpath(self, path: Union[str, "os.PathLike[str]"]) -> bool:
553+
def isinitpath(
554+
self,
555+
path: Union[str, "os.PathLike[str]"],
556+
*,
557+
with_parents: bool = False,
558+
) -> bool:
559+
"""Is path an initial path?
560+
561+
An initial path is a path pytest starts with, e.g. given on the command
562+
line.
563+
564+
:param with_parents:
565+
If set, also return True if the path is a parent of an initial path.
566+
567+
.. versionchanged:: 8.0
568+
Added the ``with_parents`` parameter.
569+
"""
553570
# Optimization: Path(Path(...)) is much slower than isinstance.
554571
path_ = path if isinstance(path, Path) else Path(path)
555-
return path_ in self._initialpaths
572+
if with_parents:
573+
return path_ in self._initialpaths_with_parents
574+
else:
575+
return path_ in self._initialpaths
556576

557577
def gethookproxy(self, fspath: "os.PathLike[str]") -> pluggy.HookRelay:
558578
# Optimization: Path(Path(...)) is much slower than isinstance.
@@ -667,6 +687,7 @@ def perform_collect( # noqa: F811
667687
items: Sequence[Union[nodes.Item, nodes.Collector]] = self.items
668688
try:
669689
initialpaths: List[Path] = []
690+
initialpaths_with_parents: List[Path] = []
670691
for arg in args:
671692
fspath, parts = resolve_collection_argument(
672693
self.config.invocation_params.dir,
@@ -675,7 +696,10 @@ def perform_collect( # noqa: F811
675696
)
676697
self._initial_parts.append((fspath, parts))
677698
initialpaths.append(fspath)
699+
initialpaths_with_parents.append(fspath)
700+
initialpaths_with_parents.extend(fspath.parents)
678701
self._initialpaths = frozenset(initialpaths)
702+
self._initialpaths_with_parents = frozenset(initialpaths_with_parents)
679703
rep = collect_one_node(self)
680704
self.ihook.pytest_collectreport(report=rep)
681705
self.trace.root.indent -= 1

0 commit comments

Comments
 (0)