From 91477f0a125d7b48ca60149c83d8d25cef7403d7 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 8 Nov 2018 00:22:27 +0100 Subject: [PATCH] collection: cleanup/centralize keepduplicates handling --- src/_pytest/main.py | 24 ++++++++++++------------ src/_pytest/python.py | 11 +---------- testing/test_collection.py | 8 ++++++++ 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 1de5f656fd0..9f629548609 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -474,8 +474,18 @@ def collect(self): arg = "::".join(map(str, initialpart)) self.trace("processing argument", arg) self.trace.root.indent += 1 + + keepduplicates = self.config.getoption("keepduplicates") + if not keepduplicates: + duplicate_paths = self.config.pluginmanager._duplicatepaths + try: for x in self._collect(arg): + if not keepduplicates: + dupe_key = (type(x), x.fspath) + if dupe_key in duplicate_paths: + continue + duplicate_paths.add(dupe_key) yield x except NoMatch: # we are inside a make_report hook so @@ -503,7 +513,7 @@ def _collect(self, arg): pkginit = parent.join("__init__.py") if pkginit.isfile(): if pkginit not in self._node_cache: - col = self._collectfile(pkginit, handle_dupes=False) + col = self._collectfile(pkginit) if col: if isinstance(col[0], Package): self._pkg_roots[parent] = col[0] @@ -572,21 +582,11 @@ def filter_(f): for y in m: yield y - def _collectfile(self, path, handle_dupes=True): + def _collectfile(self, path): ihook = self.gethookproxy(path) if not self.isinitpath(path): if ihook.pytest_ignore_collect(path=path, config=self.config): return () - - if handle_dupes: - keepduplicates = self.config.getoption("keepduplicates") - if not keepduplicates: - duplicate_paths = self.config.pluginmanager._duplicatepaths - if path in duplicate_paths: - return () - else: - duplicate_paths.add(path) - return ihook.pytest_collect_file(path=path, parent=self) def _recurse(self, dirpath): diff --git a/src/_pytest/python.py b/src/_pytest/python.py index d360e2c8f3e..1d80bfa05f6 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -545,21 +545,12 @@ def gethookproxy(self, fspath): proxy = self.config.hook return proxy - def _collectfile(self, path, handle_dupes=True): + def _collectfile(self, path): ihook = self.gethookproxy(path) if not self.isinitpath(path): if ihook.pytest_ignore_collect(path=path, config=self.config): return () - if handle_dupes: - keepduplicates = self.config.getoption("keepduplicates") - if not keepduplicates: - duplicate_paths = self.config.pluginmanager._duplicatepaths - if path in duplicate_paths: - return () - else: - duplicate_paths.add(path) - if self.fspath == path: # __init__.py return [self] diff --git a/testing/test_collection.py b/testing/test_collection.py index 18033b9c006..659d39c8112 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -98,6 +98,14 @@ def pytest_collect_file(path, parent): node = testdir.getpathnode(hello) assert isinstance(node, pytest.File) assert node.name == "hello.xxx" + + # HACK: unset _duplicatepaths, which appears to be necessary for this + # specific test setup?! + assert node.session.config.pluginmanager._duplicatepaths == { + (type(node), hello) + } + node.session.config.pluginmanager._duplicatepaths = set() + nodes = node.session.perform_collect([node.nodeid], genitems=False) assert len(nodes) == 1 assert isinstance(nodes[0], pytest.File)