Use Path() objects to store conftest files
Using Path().resolve() is better than py.path.realpath because it resolves to the correct path/drive in case-insensitive file systems (#5792): >>> from py.path import local >>> from pathlib import Path >>> >>> local('d:\\projects').realpath() local('d:\\projects') >>> Path('d:\\projects').resolve() WindowsPath('D:/projects') Fix #5819
This commit is contained in:
parent
cf5b544db3
commit
b48f51eb03
|
@ -414,10 +414,7 @@ class PytestPluginManager(PluginManager):
|
||||||
continue
|
continue
|
||||||
conftestpath = parent.join("conftest.py")
|
conftestpath = parent.join("conftest.py")
|
||||||
if conftestpath.isfile():
|
if conftestpath.isfile():
|
||||||
# Use realpath to avoid loading the same conftest twice
|
mod = self._importconftest(conftestpath)
|
||||||
# with build systems that create build directories containing
|
|
||||||
# symlinks to actual files.
|
|
||||||
mod = self._importconftest(conftestpath.realpath())
|
|
||||||
clist.append(mod)
|
clist.append(mod)
|
||||||
self._dirpath2confmods[directory] = clist
|
self._dirpath2confmods[directory] = clist
|
||||||
return clist
|
return clist
|
||||||
|
@ -432,8 +429,14 @@ class PytestPluginManager(PluginManager):
|
||||||
raise KeyError(name)
|
raise KeyError(name)
|
||||||
|
|
||||||
def _importconftest(self, conftestpath):
|
def _importconftest(self, conftestpath):
|
||||||
|
# Use a resolved Path object as key to avoid loading the same conftest twice
|
||||||
|
# with build systems that create build directories containing
|
||||||
|
# symlinks to actual files.
|
||||||
|
# Using Path().resolve() is better than py.path.realpath because
|
||||||
|
# it resolves to the correct path/drive in case-insensitive file systems (#5792)
|
||||||
|
key = Path(str(conftestpath)).resolve()
|
||||||
try:
|
try:
|
||||||
return self._conftestpath2mod[conftestpath]
|
return self._conftestpath2mod[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pkgpath = conftestpath.pypkgpath()
|
pkgpath = conftestpath.pypkgpath()
|
||||||
if pkgpath is None:
|
if pkgpath is None:
|
||||||
|
@ -450,7 +453,7 @@ class PytestPluginManager(PluginManager):
|
||||||
raise ConftestImportFailure(conftestpath, sys.exc_info())
|
raise ConftestImportFailure(conftestpath, sys.exc_info())
|
||||||
|
|
||||||
self._conftest_plugins.add(mod)
|
self._conftest_plugins.add(mod)
|
||||||
self._conftestpath2mod[conftestpath] = mod
|
self._conftestpath2mod[key] = mod
|
||||||
dirpath = conftestpath.dirpath()
|
dirpath = conftestpath.dirpath()
|
||||||
if dirpath in self._dirpath2confmods:
|
if dirpath in self._dirpath2confmods:
|
||||||
for path, mods in self._dirpath2confmods.items():
|
for path, mods in self._dirpath2confmods.items():
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
import os
|
||||||
import textwrap
|
import textwrap
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import py
|
import py
|
||||||
|
|
||||||
|
@ -163,11 +165,12 @@ def test_setinitial_conftest_subdirs(testdir, name):
|
||||||
subconftest = sub.ensure("conftest.py")
|
subconftest = sub.ensure("conftest.py")
|
||||||
conftest = PytestPluginManager()
|
conftest = PytestPluginManager()
|
||||||
conftest_setinitial(conftest, [sub.dirpath()], confcutdir=testdir.tmpdir)
|
conftest_setinitial(conftest, [sub.dirpath()], confcutdir=testdir.tmpdir)
|
||||||
|
key = Path(str(subconftest)).resolve()
|
||||||
if name not in ("whatever", ".dotdir"):
|
if name not in ("whatever", ".dotdir"):
|
||||||
assert subconftest in conftest._conftestpath2mod
|
assert key in conftest._conftestpath2mod
|
||||||
assert len(conftest._conftestpath2mod) == 1
|
assert len(conftest._conftestpath2mod) == 1
|
||||||
else:
|
else:
|
||||||
assert subconftest not in conftest._conftestpath2mod
|
assert key not in conftest._conftestpath2mod
|
||||||
assert len(conftest._conftestpath2mod) == 0
|
assert len(conftest._conftestpath2mod) == 0
|
||||||
|
|
||||||
|
|
||||||
|
@ -275,6 +278,31 @@ def test_conftest_symlink_files(testdir):
|
||||||
assert result.ret == ExitCode.OK
|
assert result.ret == ExitCode.OK
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
os.path.normcase("x") != os.path.normcase("X"),
|
||||||
|
reason="only relevant for case insensitive file systems",
|
||||||
|
)
|
||||||
|
def test_conftest_badcase(testdir):
|
||||||
|
"""Check conftest.py loading when directory casing is wrong (#5792)."""
|
||||||
|
testdir.tmpdir.mkdir("JenkinsRoot").mkdir("test")
|
||||||
|
source = {"setup.py": "", "test/__init__.py": "", "test/conftest.py": ""}
|
||||||
|
testdir.makepyfile(**{"JenkinsRoot/%s" % k: v for k, v in source.items()})
|
||||||
|
|
||||||
|
testdir.tmpdir.join("jenkinsroot/test").chdir()
|
||||||
|
result = testdir.runpytest()
|
||||||
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
||||||
|
|
||||||
|
|
||||||
|
def test_conftest_uppercase(testdir):
|
||||||
|
"""Check conftest.py whose qualified name contains uppercase characters (#5819)"""
|
||||||
|
source = {"__init__.py": "", "Foo/conftest.py": "", "Foo/__init__.py": ""}
|
||||||
|
testdir.makepyfile(**source)
|
||||||
|
|
||||||
|
testdir.tmpdir.chdir()
|
||||||
|
result = testdir.runpytest()
|
||||||
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
||||||
|
|
||||||
|
|
||||||
def test_no_conftest(testdir):
|
def test_no_conftest(testdir):
|
||||||
testdir.makeconftest("assert 0")
|
testdir.makeconftest("assert 0")
|
||||||
result = testdir.runpytest("--noconftest")
|
result = testdir.runpytest("--noconftest")
|
||||||
|
|
Loading…
Reference in New Issue