From af2d1950632cc5cef6b808801d1e1519c5908905 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 25 Oct 2018 20:03:14 +0200 Subject: [PATCH] Move handling of duplicate files This removes the hack added in https://github.com/pytest-dev/pytest/pull/3802. Adjusts test: - it appears to not have been changed to 7 intentionally. - removes XXX comment, likely not relevant anymore since 6dac7743. Backport of e0418236 (https://github.com/pytest-dev/pytest/pull/4241) from features. --- changelog/4046.bugfix.rst | 1 + src/_pytest/main.py | 19 ++++++++++--------- src/_pytest/python.py | 9 --------- testing/test_session.py | 2 +- 4 files changed, 12 insertions(+), 19 deletions(-) create mode 100644 changelog/4046.bugfix.rst diff --git a/changelog/4046.bugfix.rst b/changelog/4046.bugfix.rst new file mode 100644 index 00000000000..220cfe915f0 --- /dev/null +++ b/changelog/4046.bugfix.rst @@ -0,0 +1 @@ +Fix collecting tests from packages ``__init__.py`` files. diff --git a/src/_pytest/main.py b/src/_pytest/main.py index f27270f262c..7e5d096a5b7 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -281,15 +281,6 @@ def pytest_ignore_collect(path, config): if _in_venv(path) and not allow_in_venv: return True - # Skip duplicate paths. - keepduplicates = config.getoption("keepduplicates") - duplicate_paths = config.pluginmanager._duplicatepaths - if not keepduplicates: - if path in duplicate_paths: - return True - else: - duplicate_paths.add(path) - return False @@ -559,6 +550,16 @@ def _collectfile(self, path): if not self.isinitpath(path): if ihook.pytest_ignore_collect(path=path, config=self.config): return () + + # Skip duplicate paths. + 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, path): diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 58f95034d2c..414eabec6cb 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -552,15 +552,6 @@ def isinitpath(self, path): return path in self.session._initialpaths def collect(self): - # XXX: HACK! - # Before starting to collect any files from this package we need - # to cleanup the duplicate paths added by the session's collect(). - # Proper fix is to not track these as duplicates in the first place. - for path in list(self.session.config.pluginmanager._duplicatepaths): - # if path.parts()[:len(self.fspath.dirpath().parts())] == self.fspath.dirpath().parts(): - if path.dirname.startswith(self.name): - self.session.config.pluginmanager._duplicatepaths.remove(path) - this_path = self.fspath.dirpath() init_module = this_path.join("__init__.py") if init_module.check(file=1) and path_matches_patterns( diff --git a/testing/test_session.py b/testing/test_session.py index 6225a2c0db6..c1785b91668 100644 --- a/testing/test_session.py +++ b/testing/test_session.py @@ -219,7 +219,7 @@ class TestY(TestX): started = reprec.getcalls("pytest_collectstart") finished = reprec.getreports("pytest_collectreport") assert len(started) == len(finished) - assert len(started) == 7 # XXX extra TopCollector + assert len(started) == 8 colfail = [x for x in finished if x.failed] assert len(colfail) == 1