From 6fce1f0ac7a9a569429efa740f5cdd04805b4a6c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 7 Nov 2018 20:06:35 +0100 Subject: [PATCH] pkg_roots per session --- src/_pytest/main.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 50bd10979..5d5d02a23 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -386,6 +386,7 @@ class Session(nodes.FSCollector): self._initialpaths = frozenset() # Keep track of any collected nodes in here, so we don't duplicate fixtures self._node_cache = {} + self._pkg_roots = {} self.config.pluginmanager.register(self, name="session") @@ -488,7 +489,6 @@ class Session(nodes.FSCollector): names = self._parsearg(arg) argpath = names.pop(0).realpath() - pkg_roots = {} root = self # Start with a Session root, and delve to argpath item (dir or file) @@ -509,7 +509,7 @@ class Session(nodes.FSCollector): col = root._collectfile(pkginit, handle_dupes=False) if col: if isinstance(col[0], Package): - pkg_roots[parent] = col[0] + self._pkg_roots[parent] = col[0] # always store a list in the cache, matchnodes expects it self._node_cache[col[0].fspath] = [col[0]] @@ -533,15 +533,25 @@ class Session(nodes.FSCollector): fil=filter_, rec=self._recurse, bf=True, sort=True ): dirpath = path.dirpath() - collect_root = pkg_roots.get(dirpath, root) + collect_root = self._pkg_roots.get(dirpath, root) if dirpath not in seen_dirs: + # Collect packages first. seen_dirs.add(dirpath) pkginit = dirpath.join("__init__.py") if pkginit.exists(): + got_pkg = False for x in collect_root._collectfile(pkginit): yield x if isinstance(x, Package): - pkg_roots[dirpath] = x + self._pkg_roots[dirpath] = x + got_pkg = True + if got_pkg: + continue + if path.basename == "__init__.py": + continue + + if dirpath in self._pkg_roots: + continue for x in collect_root._collectfile(path): key = (type(x), x.fspath) @@ -556,7 +566,7 @@ class Session(nodes.FSCollector): if argpath in self._node_cache: col = self._node_cache[argpath] else: - collect_root = pkg_roots.get(argpath.dirname, root) + collect_root = self._pkg_roots.get(argpath.dirname, root) col = collect_root._collectfile(argpath) if col: self._node_cache[argpath] = col