From 0c98f1923101e5905c54ba07650a043fca374f4b Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 8 Jan 2022 22:41:14 +0200 Subject: [PATCH] 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. --- src/_pytest/config/__init__.py | 25 ++++++++++++++++++------- src/_pytest/main.py | 3 +-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 79b2ad598..a30ee6cee 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -521,6 +521,19 @@ class PytestPluginManager(PluginManager): 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: @@ -552,14 +565,12 @@ class PytestPluginManager(PluginManager): # 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 diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 952c70350..afe613fd0 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -689,9 +689,8 @@ class Session(nodes.FSCollector): # 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():