Skip to content

Commit 122b10c

Browse files
committed
Move common code between Session and Package to FSCollector
1 parent 0336dc8 commit 122b10c

File tree

3 files changed

+41
-68
lines changed

3 files changed

+41
-68
lines changed

src/_pytest/main.py

Lines changed: 0 additions & 40 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

@@ -449,20 +436,6 @@ def pytest_runtest_logreport(self, report):
449436
def isinitpath(self, path):
450437
return path in self._initialpaths
451438

452-
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
465-
466439
def perform_collect(self, args=None, genitems=True):
467440
hook = self.config.hook
468441
try:
@@ -625,19 +598,6 @@ def _collectfile(self, path, handle_dupes=True):
625598

626599
return ihook.pytest_collect_file(path=path, parent=self)
627600

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-
641601
@staticmethod
642602
def _visit_filter(f):
643603
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: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from _pytest.compat import STRING_TYPES
3535
from _pytest.config import hookimpl
3636
from _pytest.deprecated import FUNCARGNAMES
37-
from _pytest.main import FSHookProxy
3837
from _pytest.mark import MARK_GEN
3938
from _pytest.mark.structures import get_unpacked_marks
4039
from _pytest.mark.structures import normalize_mark_list
@@ -559,33 +558,6 @@ def setup(self):
559558
func = partial(_call_with_optional_argument, teardown_module, self.obj)
560559
self.addfinalizer(func)
561560

562-
def _recurse(self, dirpath: py.path.local) -> bool:
563-
if dirpath.basename == "__pycache__":
564-
return False
565-
ihook = self.gethookproxy(dirpath.dirpath())
566-
if ihook.pytest_ignore_collect(path=dirpath, config=self.config):
567-
return False
568-
for pat in self._norecursepatterns:
569-
if dirpath.check(fnmatch=pat):
570-
return False
571-
ihook = self.gethookproxy(dirpath)
572-
ihook.pytest_collect_directory(path=dirpath, parent=self)
573-
return True
574-
575-
def gethookproxy(self, fspath):
576-
# check if we have the common case of running
577-
# hooks with all conftest.py files
578-
pm = self.config.pluginmanager
579-
my_conftestmodules = pm._getconftestmodules(fspath)
580-
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
581-
if remove_mods:
582-
# one or more conftests are not in use at this fspath
583-
proxy = FSHookProxy(fspath, pm, remove_mods)
584-
else:
585-
# all plugins are active for this fspath
586-
proxy = self.config.hook
587-
return proxy
588-
589561
def _collectfile(self, path, handle_dupes=True):
590562
assert (
591563
path.isfile()

0 commit comments

Comments
 (0)