Skip to content

Commit e2934c3

Browse files
committed
Move common code between Session and Package to FSCollector
1 parent 6b7e1a2 commit e2934c3

File tree

3 files changed

+43
-64
lines changed

3 files changed

+43
-64
lines changed

src/_pytest/main.py

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,6 @@ def pytest_collection_modifyitems(items, config):
348348
items[:] = remaining
349349

350350

351-
class FSHookProxy:
352-
def __init__(self, fspath, pm, remove_mods):
353-
self.fspath = fspath
354-
self.pm = pm
355-
self.remove_mods = remove_mods
356-
357-
def __getattr__(self, name):
358-
x = self.pm.subset_hook_caller(name, remove_plugins=self.remove_mods)
359-
self.__dict__[name] = x
360-
return x
361-
362-
363351
class NoMatch(Exception):
364352
""" raised if matching cannot locate a matching names. """
365353

@@ -401,7 +389,6 @@ def __init__(self, config: Config) -> None:
401389
self.shouldstop = False
402390
self.shouldfail = False
403391
self.trace = config.trace.root.get("collection")
404-
self._norecursepatterns = config.getini("norecursedirs")
405392
self.startdir = config.invocation_dir
406393
self._initialpaths = frozenset() # type: FrozenSet[py.path.local]
407394

@@ -450,18 +437,7 @@ def isinitpath(self, path):
450437
return path in self._initialpaths
451438

452439
def gethookproxy(self, fspath):
453-
# check if we have the common case of running
454-
# hooks with all conftest.py files
455-
pm = self.config.pluginmanager
456-
my_conftestmodules = pm._getconftestmodules(fspath)
457-
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
458-
if remove_mods:
459-
# one or more conftests are not in use at this fspath
460-
proxy = FSHookProxy(fspath, pm, remove_mods)
461-
else:
462-
# all plugins are active for this fspath
463-
proxy = self.config.hook
464-
return proxy
440+
return super()._gethookproxy(fspath)
465441

466442
def perform_collect(self, args=None, genitems=True):
467443
hook = self.config.hook
@@ -625,19 +601,6 @@ def _collectfile(self, path, handle_dupes=True):
625601

626602
return ihook.pytest_collect_file(path=path, parent=self)
627603

628-
def _recurse(self, dirpath: py.path.local) -> bool:
629-
if dirpath.basename == "__pycache__":
630-
return False
631-
ihook = self.gethookproxy(dirpath.dirpath())
632-
if ihook.pytest_ignore_collect(path=dirpath, config=self.config):
633-
return False
634-
for pat in self._norecursepatterns:
635-
if dirpath.check(fnmatch=pat):
636-
return False
637-
ihook = self.gethookproxy(dirpath)
638-
ihook.pytest_collect_directory(path=dirpath, parent=self)
639-
return True
640-
641604
@staticmethod
642605
def _visit_filter(f):
643606
return f.check(file=1)

src/_pytest/nodes.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,18 @@ def _check_initialpaths_for_relpath(session, fspath):
393393
return fspath.relto(initial_path)
394394

395395

396+
class FSHookProxy:
397+
def __init__(self, fspath, pm, remove_mods):
398+
self.fspath = fspath
399+
self.pm = pm
400+
self.remove_mods = remove_mods
401+
402+
def __getattr__(self, name):
403+
x = self.pm.subset_hook_caller(name, remove_plugins=self.remove_mods)
404+
self.__dict__[name] = x
405+
return x
406+
407+
396408
class FSCollector(Collector):
397409
def __init__(
398410
self, fspath: py.path.local, parent=None, config=None, session=None, nodeid=None
@@ -417,6 +429,35 @@ def __init__(
417429

418430
super().__init__(name, parent, config, session, nodeid=nodeid, fspath=fspath)
419431

432+
self._norecursepatterns = self.config.getini("norecursedirs")
433+
434+
def _gethookproxy(self, fspath):
435+
# check if we have the common case of running
436+
# hooks with all conftest.py files
437+
pm = self.config.pluginmanager
438+
my_conftestmodules = pm._getconftestmodules(fspath)
439+
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
440+
if remove_mods:
441+
# one or more conftests are not in use at this fspath
442+
proxy = FSHookProxy(fspath, pm, remove_mods)
443+
else:
444+
# all plugins are active for this fspath
445+
proxy = self.config.hook
446+
return proxy
447+
448+
def _recurse(self, dirpath: py.path.local) -> bool:
449+
if dirpath.basename == "__pycache__":
450+
return False
451+
ihook = self._gethookproxy(dirpath.dirpath())
452+
if ihook.pytest_ignore_collect(path=dirpath, config=self.config):
453+
return False
454+
for pat in self._norecursepatterns:
455+
if dirpath.check(fnmatch=pat):
456+
return False
457+
ihook = self._gethookproxy(dirpath)
458+
ihook.pytest_collect_directory(path=dirpath, parent=self)
459+
return True
460+
420461

421462
class File(FSCollector):
422463
""" base class for collecting tests from a file. """

src/_pytest/python.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from _pytest.compat import STRING_TYPES
3636
from _pytest.config import hookimpl
3737
from _pytest.deprecated import FUNCARGNAMES
38-
from _pytest.main import FSHookProxy
3938
from _pytest.mark import MARK_GEN
4039
from _pytest.mark.structures import get_unpacked_marks
4140
from _pytest.mark.structures import normalize_mark_list
@@ -579,32 +578,8 @@ def setup(self):
579578
func = partial(_call_with_optional_argument, teardown_module, self.obj)
580579
self.addfinalizer(func)
581580

582-
def _recurse(self, dirpath: py.path.local) -> bool:
583-
if dirpath.basename == "__pycache__":
584-
return False
585-
ihook = self.gethookproxy(dirpath.dirpath())
586-
if ihook.pytest_ignore_collect(path=dirpath, config=self.config):
587-
return False
588-
for pat in self._norecursepatterns:
589-
if dirpath.check(fnmatch=pat):
590-
return False
591-
ihook = self.gethookproxy(dirpath)
592-
ihook.pytest_collect_directory(path=dirpath, parent=self)
593-
return True
594-
595581
def gethookproxy(self, fspath):
596-
# check if we have the common case of running
597-
# hooks with all conftest.py files
598-
pm = self.config.pluginmanager
599-
my_conftestmodules = pm._getconftestmodules(fspath)
600-
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
601-
if remove_mods:
602-
# one or more conftests are not in use at this fspath
603-
proxy = FSHookProxy(fspath, pm, remove_mods)
604-
else:
605-
# all plugins are active for this fspath
606-
proxy = self.config.hook
607-
return proxy
582+
return super()._gethookproxy(fspath)
608583

609584
def _collectfile(self, path, handle_dupes=True):
610585
assert (

0 commit comments

Comments
 (0)