Skip to content

Commit 126b151

Browse files
committed
Use normcase(conftest_path) as registration name
1 parent 549f70b commit 126b151

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

src/_pytest/config/__init__.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from _pytest.pathlib import safe_exists
6464
from _pytest.stash import Stash
6565
from _pytest.warning_types import PytestConfigWarning
66+
from _pytest.warning_types import PytestDeprecationWarning
6667
from _pytest.warning_types import warn_explicit_for
6768

6869
if TYPE_CHECKING:
@@ -634,10 +635,8 @@ def _rget_with_confmod(
634635
def _importconftest(
635636
self, conftestpath: Path, importmode: Union[str, ImportMode], rootpath: Path
636637
) -> types.ModuleType:
637-
# Avoid inconsistent path issues on Windows, see
638-
# https://github.com/pytest-dev/pytest/issues/9765
639-
normalized_conftestpath = os.path.normcase(conftestpath)
640-
existing = self.get_plugin(normalized_conftestpath)
638+
conftestpath_str = str(conftestpath)
639+
existing = self.get_plugin(conftestpath_str)
641640
if existing is not None:
642641
return cast(types.ModuleType, existing)
643642

@@ -662,7 +661,7 @@ def _importconftest(
662661
assert mod not in mods
663662
mods.append(mod)
664663
self.trace(f"loading conftestmodule {mod!r}")
665-
self.consider_conftest(mod)
664+
self.consider_conftest(mod, registration_name=conftestpath_str)
666665
return mod
667666

668667
def _check_non_top_pytest_plugins(
@@ -742,13 +741,19 @@ def consider_pluginarg(self, arg: str) -> None:
742741
del self._name2plugin["pytest_" + name]
743742
self.import_plugin(arg, consider_entry_points=True)
744743

745-
def consider_conftest(self, conftestmodule: types.ModuleType) -> None:
744+
def consider_conftest(
745+
self, conftestmodule: types.ModuleType, registration_name: Optional[str] = None
746+
) -> None:
746747
""":meta private:"""
747-
name = conftestmodule.__file__
748-
# Avoid inconsistent path issues on Windows, see
749-
# https://github.com/pytest-dev/pytest/issues/9765
750-
normalized_name = name if name is None else os.path.normcase(name)
751-
self.register(conftestmodule, name=normalized_name)
748+
if registration_name is None:
749+
warnings.warn(
750+
"'consider_conftest' was called with registration_name=None, this will become an error in the future'",
751+
PytestDeprecationWarning,
752+
)
753+
# Keep Pytest < 8 previous behaviour
754+
registration_name = conftestmodule.__file__
755+
756+
self.register(conftestmodule, name=registration_name)
752757

753758
def consider_env(self) -> None:
754759
""":meta private:"""

testing/test_pluginmanager.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,28 @@ def pytest_configure(self):
103103
reason="requires a case-insensitive file system",
104104
)
105105
def test_conftestpath_case_sensitivity(self, pytester: Pytester) -> None:
106-
"""Non-regression test for https://github.com/pytest-dev/pytest/issues/9765
107-
108-
Ensures that the conftests are registered in the plugin dictionary with a key
109-
that does not depend on the case of the input path if the filesystem is
110-
case-insensitive.
111-
"""
112-
113106
config = pytester.parseconfig()
114107
pytester.makepyfile(**{"tests/conftest.py": ""})
115108

116109
conftest = pytester.path.joinpath("tests/conftest.py")
117-
conftest_lower = pytester.path.joinpath("TESTS/conftest.py")
110+
conftest_upper_case = pytester.path.joinpath("TESTS/conftest.py")
118111

119112
mod = config.pluginmanager._importconftest(
120113
conftest, importmode="prepend", rootpath=pytester.path
121114
)
115+
assert config.pluginmanager._name2plugin[str(conftest)] is mod
122116

123-
# Those two calls should succeed because the conftest should be registered
124-
# independently of the casing of conftestpath.
125-
assert mod is config.pluginmanager._importconftest(
126-
conftest, importmode="prepend", rootpath=pytester.path
117+
mod_uppercase = config.pluginmanager._importconftest(
118+
conftest_upper_case, importmode="prepend", rootpath=pytester.path
127119
)
128-
assert mod is config.pluginmanager._importconftest(
129-
conftest_lower, importmode="prepend", rootpath=pytester.path
120+
assert (
121+
config.pluginmanager._name2plugin[str(conftest_upper_case)] is mod_uppercase
130122
)
131123

124+
# No str(conftestpath) normalization so conftest should be imported
125+
# twice and modules should be different objects
126+
assert mod is not mod_uppercase
127+
132128
def test_hook_tracing(self, _config_for_test: Config) -> None:
133129
pytestpm = _config_for_test.pluginmanager # fully initialized with plugins
134130
saveindent = []
@@ -399,7 +395,7 @@ def test_consider_conftest_deps(
399395
pytester.makepyfile("pytest_plugins='xyz'"), root=pytester.path
400396
)
401397
with pytest.raises(ImportError):
402-
pytestpm.consider_conftest(mod)
398+
pytestpm.consider_conftest(mod, registration_name="unused")
403399

404400

405401
class TestPytestPluginManagerBootstrapming:

0 commit comments

Comments
 (0)