@@ -499,6 +499,7 @@ def __init__(self, config: Config) -> None:
499
499
self .shouldfail : Union [bool , str ] = False
500
500
self .trace = config .trace .root .get ("collection" )
501
501
self ._initialpaths : FrozenSet [Path ] = frozenset ()
502
+ self ._initialpaths_with_parents : FrozenSet [Path ] = frozenset ()
502
503
503
504
self ._bestrelpathcache : Dict [Path , str ] = _bestrelpath_cache (config .rootpath )
504
505
@@ -549,10 +550,29 @@ def pytest_runtest_logreport(
549
550
550
551
pytest_collectreport = pytest_runtest_logreport
551
552
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
+ """
553
570
# Optimization: Path(Path(...)) is much slower than isinstance.
554
571
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
556
576
557
577
def gethookproxy (self , fspath : "os.PathLike[str]" ) -> pluggy .HookRelay :
558
578
# Optimization: Path(Path(...)) is much slower than isinstance.
@@ -667,6 +687,7 @@ def perform_collect( # noqa: F811
667
687
items : Sequence [Union [nodes .Item , nodes .Collector ]] = self .items
668
688
try :
669
689
initialpaths : List [Path ] = []
690
+ initialpaths_with_parents : List [Path ] = []
670
691
for arg in args :
671
692
fspath , parts = resolve_collection_argument (
672
693
self .config .invocation_params .dir ,
@@ -675,7 +696,10 @@ def perform_collect( # noqa: F811
675
696
)
676
697
self ._initial_parts .append ((fspath , parts ))
677
698
initialpaths .append (fspath )
699
+ initialpaths_with_parents .append (fspath )
700
+ initialpaths_with_parents .extend (fspath .parents )
678
701
self ._initialpaths = frozenset (initialpaths )
702
+ self ._initialpaths_with_parents = frozenset (initialpaths_with_parents )
679
703
rep = collect_one_node (self )
680
704
self .ihook .pytest_collectreport (report = rep )
681
705
self .trace .root .indent -= 1
0 commit comments