Skip to content

Commit a65c47a

Browse files
authored
Merge pull request #9783 from pytest-dev/backport-9780-to-7.1.x
[7.1.x] config: restore pre-pytest 7.1.0 confcutdir exclusion behavior
2 parents 7d4d1ec + f4cfc59 commit a65c47a

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

changelog/9767.bugfix.rst

+1
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.

extra/setup-py.test/setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
23
from distutils.core import setup
34

45
if __name__ == "__main__":

src/_pytest/config/__init__.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,7 @@ def _is_in_confcutdir(self, path: Path) -> bool:
538538
"""
539539
if self._confcutdir is None:
540540
return True
541-
try:
542-
path.relative_to(self._confcutdir)
543-
except ValueError:
544-
return False
545-
return True
541+
return path not in self._confcutdir.parents
546542

547543
def _try_load_conftest(
548544
self, anchor: Path, importmode: Union[str, ImportMode], rootpath: Path

testing/test_conftest.py

+28
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)