From 161bc481178c7dbd59b3ee3de92606a006a269aa Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sun, 9 Jan 2022 00:38:40 +0200 Subject: [PATCH] config: get rid of _conftestpath2mod It duplicates what PluginManager already knows, and no longer needed now that symlinks are not resolved (see previous commit). --- src/_pytest/config/__init__.py | 9 +++------ testing/test_conftest.py | 21 ++++++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index b75a675b6d8..3896313e033 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1,7 +1,6 @@ """Command line options, ini-file and conftest.py processing.""" import argparse import collections.abc -import contextlib import copy import enum import inspect @@ -353,8 +352,6 @@ def __init__(self) -> None: # This includes the directory's own conftest modules as well # as those of its parent directories. self._dirpath2confmods: Dict[Path, List[types.ModuleType]] = {} - # The conftest module of a conftest path. - self._conftestpath2mod: Dict[Path, types.ModuleType] = {} # Cutoff directory above which conftests are no longer discovered. self._confcutdir: Optional[Path] = None # If set, conftest loading is skipped. @@ -592,8 +589,9 @@ def _rget_with_confmod( def _importconftest( self, conftestpath: Path, importmode: Union[str, ImportMode], rootpath: Path ) -> types.ModuleType: - with contextlib.suppress(KeyError): - return self._conftestpath2mod[conftestpath] + existing = self.get_plugin(str(conftestpath)) + if existing is not None: + return cast(types.ModuleType, existing) pkgpath = resolve_package_path(conftestpath) if pkgpath is None: @@ -609,7 +607,6 @@ def _importconftest( self._check_non_top_pytest_plugins(mod, conftestpath) self._conftest_plugins.add(mod) - self._conftestpath2mod[conftestpath] = mod dirpath = conftestpath.parent if dirpath in self._dirpath2confmods: for path, mods in self._dirpath2confmods.items(): diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 92a5ffb72cf..4cbc2d14c36 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -146,10 +146,9 @@ def test_issue151_load_all_conftests(pytester: Pytester) -> None: p = pytester.mkdir(name) p.joinpath("conftest.py").touch() - conftest = PytestPluginManager() - conftest_setinitial(conftest, names) - d = list(conftest._conftestpath2mod.values()) - assert len(d) == len(names) + pm = PytestPluginManager() + conftest_setinitial(pm, names) + assert len(set(pm.get_plugins()) - {pm}) == len(names) def test_conftest_global_import(pytester: Pytester) -> None: @@ -192,7 +191,7 @@ def test_conftestcutdir(pytester: Pytester) -> None: conf.parent, importmode="prepend", rootpath=pytester.path ) assert len(values) == 0 - assert Path(conf) not in conftest._conftestpath2mod + assert not conftest.has_plugin(str(conf)) # but we can still import a conftest directly conftest._importconftest(conf, importmode="prepend", rootpath=pytester.path) values = conftest._getconftestmodules( @@ -226,15 +225,15 @@ def test_setinitial_conftest_subdirs(pytester: Pytester, name: str) -> None: sub = pytester.mkdir(name) subconftest = sub.joinpath("conftest.py") subconftest.touch() - conftest = PytestPluginManager() - conftest_setinitial(conftest, [sub.parent], confcutdir=pytester.path) + pm = PytestPluginManager() + conftest_setinitial(pm, [sub.parent], confcutdir=pytester.path) key = subconftest.resolve() if name not in ("whatever", ".dotdir"): - assert key in conftest._conftestpath2mod - assert len(conftest._conftestpath2mod) == 1 + assert pm.has_plugin(str(key)) + assert len(set(pm.get_plugins()) - {pm}) == 1 else: - assert key not in conftest._conftestpath2mod - assert len(conftest._conftestpath2mod) == 0 + assert not pm.has_plugin(str(key)) + assert len(set(pm.get_plugins()) - {pm}) == 0 def test_conftest_confcutdir(pytester: Pytester) -> None: