Skip to content

Commit 1d70b02

Browse files
committed
config: restore pre-pytest 7.1.0 confcutdir exclusion behavior
The change from `path not in confuctdir.parents` to the `relative_to` check in 0c98f19 broke picking up conftest files when running against an installed package/site-packages. See the issue for more details. Fix pytest-dev#9767.
1 parent d52a6e6 commit 1d70b02

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

changelog/9767.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a regression in pytest 7.1.0 where some conftest.py files outside of the source tree (e.g. in the `site-packages` directory) were not picked up.

src/_pytest/config/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,7 @@ def _is_in_confcutdir(self, path: Path) -> bool:
540540
"""
541541
if self._confcutdir is None:
542542
return True
543-
try:
544-
path.relative_to(self._confcutdir)
545-
except ValueError:
546-
return False
547-
return True
543+
return path not in self._confcutdir.parents
548544

549545
def _try_load_conftest(
550546
self, anchor: Path, importmode: Union[str, ImportMode], rootpath: Path

testing/test_conftest.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,34 @@ def pytest_addoption(parser):
252252
result.stdout.no_fnmatch_line("*warning: could not load initial*")
253253

254254

255+
def test_installed_conftest_is_picked_up(pytester: Pytester, tmp_path: Path) -> None:
256+
"""When using `--pyargs` to run tests in an installed packages (located e.g.
257+
in a site-packages in the PYTHONPATH), conftest files in there are picked
258+
up.
259+
260+
Regression test for #9767.
261+
"""
262+
# pytester dir - the source tree.
263+
# tmp_path - the simulated site-packages dir (not in source tree).
264+
265+
pytester.syspathinsert(tmp_path)
266+
pytester.makepyprojecttoml("[tool.pytest.ini_options]")
267+
tmp_path.joinpath("foo").mkdir()
268+
tmp_path.joinpath("foo", "__init__.py").touch()
269+
tmp_path.joinpath("foo", "conftest.py").write_text(
270+
textwrap.dedent(
271+
"""\
272+
import pytest
273+
@pytest.fixture
274+
def fix(): return None
275+
"""
276+
)
277+
)
278+
tmp_path.joinpath("foo", "test_it.py").write_text("def test_it(fix): pass")
279+
result = pytester.runpytest("--pyargs", "foo")
280+
assert result.ret == 0
281+
282+
255283
def test_conftest_symlink(pytester: Pytester) -> None:
256284
"""`conftest.py` discovery follows normal path resolution and does not resolve symlinks."""
257285
# Structure:

0 commit comments

Comments
 (0)