Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: restore pre-pytest 7.1.0 confcutdir exclusion behavior #9780

Merged
merged 1 commit into from Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/9767.bugfix.rst
@@ -0,0 +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.
6 changes: 1 addition & 5 deletions src/_pytest/config/__init__.py
Expand Up @@ -540,11 +540,7 @@ def _is_in_confcutdir(self, path: Path) -> bool:
"""
if self._confcutdir is None:
return True
try:
path.relative_to(self._confcutdir)
except ValueError:
return False
return True
return path not in self._confcutdir.parents
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i recall parents being painfully expensive,

presuming the given paths are absolute, would it make sense to ensure that the path parts match (aka the common path between path and confcutdir being the path)

this is a idea for a follow-up unless its a easy check

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's super slow. I was going to say that this just brings things to the way they were, but previously at least the parents call was hoisted out of the loop, so it'd be worse now. That said, I'll work on it for the next release, to unblock the fix.


def _try_load_conftest(
self, anchor: Path, importmode: Union[str, ImportMode], rootpath: Path
Expand Down
28 changes: 28 additions & 0 deletions testing/test_conftest.py
Expand Up @@ -252,6 +252,34 @@ def pytest_addoption(parser):
result.stdout.no_fnmatch_line("*warning: could not load initial*")


def test_installed_conftest_is_picked_up(pytester: Pytester, tmp_path: Path) -> None:
"""When using `--pyargs` to run tests in an installed packages (located e.g.
in a site-packages in the PYTHONPATH), conftest files in there are picked
up.

Regression test for #9767.
"""
# pytester dir - the source tree.
# tmp_path - the simulated site-packages dir (not in source tree).

pytester.syspathinsert(tmp_path)
pytester.makepyprojecttoml("[tool.pytest.ini_options]")
tmp_path.joinpath("foo").mkdir()
tmp_path.joinpath("foo", "__init__.py").touch()
tmp_path.joinpath("foo", "conftest.py").write_text(
textwrap.dedent(
"""\
import pytest
@pytest.fixture
def fix(): return None
"""
)
)
tmp_path.joinpath("foo", "test_it.py").write_text("def test_it(fix): pass")
result = pytester.runpytest("--pyargs", "foo")
assert result.ret == 0


def test_conftest_symlink(pytester: Pytester) -> None:
"""`conftest.py` discovery follows normal path resolution and does not resolve symlinks."""
# Structure:
Expand Down