Skip to content

Commit ebf9969

Browse files
committed
Move handling of duplicate files
This removes the hack added in #3802. Adjusts test: - it appears to not have been changed to 7 intentionally. - removes XXX comment, likely not relevant anymore since 6dac774. Backport of e041823 (#4241) from features.
1 parent 56e6bb0 commit ebf9969

File tree

4 files changed

+12
-19
lines changed

4 files changed

+12
-19
lines changed

changelog/4046.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix collecting test from package ``__init__.py`` files.

src/_pytest/main.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,6 @@ def pytest_ignore_collect(path, config):
281281
if _in_venv(path) and not allow_in_venv:
282282
return True
283283

284-
# Skip duplicate paths.
285-
keepduplicates = config.getoption("keepduplicates")
286-
duplicate_paths = config.pluginmanager._duplicatepaths
287-
if not keepduplicates:
288-
if path in duplicate_paths:
289-
return True
290-
else:
291-
duplicate_paths.add(path)
292-
293284
return False
294285

295286

@@ -559,6 +550,16 @@ def _collectfile(self, path):
559550
if not self.isinitpath(path):
560551
if ihook.pytest_ignore_collect(path=path, config=self.config):
561552
return ()
553+
554+
# Skip duplicate paths.
555+
keepduplicates = self.config.getoption("keepduplicates")
556+
if not keepduplicates:
557+
duplicate_paths = self.config.pluginmanager._duplicatepaths
558+
if path in duplicate_paths:
559+
return ()
560+
else:
561+
duplicate_paths.add(path)
562+
562563
return ihook.pytest_collect_file(path=path, parent=self)
563564

564565
def _recurse(self, path):

src/_pytest/python.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,6 @@ def isinitpath(self, path):
552552
return path in self.session._initialpaths
553553

554554
def collect(self):
555-
# XXX: HACK!
556-
# Before starting to collect any files from this package we need
557-
# to cleanup the duplicate paths added by the session's collect().
558-
# Proper fix is to not track these as duplicates in the first place.
559-
for path in list(self.session.config.pluginmanager._duplicatepaths):
560-
# if path.parts()[:len(self.fspath.dirpath().parts())] == self.fspath.dirpath().parts():
561-
if path.dirname.startswith(self.name):
562-
self.session.config.pluginmanager._duplicatepaths.remove(path)
563-
564555
this_path = self.fspath.dirpath()
565556
init_module = this_path.join("__init__.py")
566557
if init_module.check(file=1) and path_matches_patterns(

testing/test_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class TestY(TestX):
219219
started = reprec.getcalls("pytest_collectstart")
220220
finished = reprec.getreports("pytest_collectreport")
221221
assert len(started) == len(finished)
222-
assert len(started) == 7 # XXX extra TopCollector
222+
assert len(started) == 8
223223
colfail = [x for x in finished if x.failed]
224224
assert len(colfail) == 1
225225

0 commit comments

Comments
 (0)