Skip to content

Commit 0c98f19

Browse files
committed
config: make confcutdir check a bit more clear & correct
I think this named function makes the code a bit easier to understand. Also change the check to explicitly check for "is a sub-path of" instead of the previous check which only worked assuming that path is within confcutdir or a direct parent of it.
1 parent 1c7644c commit 0c98f19

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/_pytest/config/__init__.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,19 @@ def _set_initial_conftests(
521521
if not foundanchor:
522522
self._try_load_conftest(current, namespace.importmode, rootpath)
523523

524+
def _is_in_confcutdir(self, path: Path) -> bool:
525+
"""Whether a path is within the confcutdir.
526+
527+
When false, should not load conftest.
528+
"""
529+
if self._confcutdir is None:
530+
return True
531+
try:
532+
path.relative_to(self._confcutdir)
533+
except ValueError:
534+
return False
535+
return True
536+
524537
def _try_load_conftest(
525538
self, anchor: Path, importmode: Union[str, ImportMode], rootpath: Path
526539
) -> None:
@@ -552,14 +565,12 @@ def _getconftestmodules(
552565
# and allow users to opt into looking into the rootdir parent
553566
# directories instead of requiring to specify confcutdir.
554567
clist = []
555-
confcutdir_parents = self._confcutdir.parents if self._confcutdir else []
556568
for parent in reversed((directory, *directory.parents)):
557-
if parent in confcutdir_parents:
558-
continue
559-
conftestpath = parent / "conftest.py"
560-
if conftestpath.is_file():
561-
mod = self._importconftest(conftestpath, importmode, rootpath)
562-
clist.append(mod)
569+
if self._is_in_confcutdir(parent):
570+
conftestpath = parent / "conftest.py"
571+
if conftestpath.is_file():
572+
mod = self._importconftest(conftestpath, importmode, rootpath)
573+
clist.append(mod)
563574
self._dirpath2confmods[directory] = clist
564575
return clist
565576

src/_pytest/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,8 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
689689
# No point in finding packages when collecting doctests.
690690
if not self.config.getoption("doctestmodules", False):
691691
pm = self.config.pluginmanager
692-
confcutdir = pm._confcutdir
693692
for parent in (argpath, *argpath.parents):
694-
if confcutdir and parent in confcutdir.parents:
693+
if not pm._is_in_confcutdir(argpath):
695694
break
696695

697696
if parent.is_dir():

0 commit comments

Comments
 (0)