Skip to content

Commit

Permalink
config: make confcutdir check a bit more clear & correct
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bluetech committed Jan 8, 2022
1 parent 1c7644c commit 0c98f19
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
25 changes: 18 additions & 7 deletions src/_pytest/config/__init__.py
Expand Up @@ -521,6 +521,19 @@ def _set_initial_conftests(
if not foundanchor:
self._try_load_conftest(current, namespace.importmode, rootpath)

def _is_in_confcutdir(self, path: Path) -> bool:
"""Whether a path is within the confcutdir.
When false, should not load conftest.
"""
if self._confcutdir is None:
return True
try:
path.relative_to(self._confcutdir)
except ValueError:
return False
return True

def _try_load_conftest(
self, anchor: Path, importmode: Union[str, ImportMode], rootpath: Path
) -> None:
Expand Down Expand Up @@ -552,14 +565,12 @@ def _getconftestmodules(
# and allow users to opt into looking into the rootdir parent
# directories instead of requiring to specify confcutdir.
clist = []
confcutdir_parents = self._confcutdir.parents if self._confcutdir else []
for parent in reversed((directory, *directory.parents)):
if parent in confcutdir_parents:
continue
conftestpath = parent / "conftest.py"
if conftestpath.is_file():
mod = self._importconftest(conftestpath, importmode, rootpath)
clist.append(mod)
if self._is_in_confcutdir(parent):
conftestpath = parent / "conftest.py"
if conftestpath.is_file():
mod = self._importconftest(conftestpath, importmode, rootpath)
clist.append(mod)
self._dirpath2confmods[directory] = clist
return clist

Expand Down
3 changes: 1 addition & 2 deletions src/_pytest/main.py
Expand Up @@ -689,9 +689,8 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
# No point in finding packages when collecting doctests.
if not self.config.getoption("doctestmodules", False):
pm = self.config.pluginmanager
confcutdir = pm._confcutdir
for parent in (argpath, *argpath.parents):
if confcutdir and parent in confcutdir.parents:
if not pm._is_in_confcutdir(argpath):
break

if parent.is_dir():
Expand Down

0 comments on commit 0c98f19

Please sign in to comment.