Skip to content

explicitly detect conda envs - fixes #12652 #12656

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/12652.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Resolve regression `conda` environments where no longer being automatically detected.

-- by :user:`RonnyPfannschmidt`
15 changes: 13 additions & 2 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,20 @@ def pytest_runtestloop(session: Session) -> bool:
def _in_venv(path: Path) -> bool:
"""Attempt to detect if ``path`` is the root of a Virtual Environment by
checking for the existence of the pyvenv.cfg file.
[https://peps.python.org/pep-0405/]"""

[https://peps.python.org/pep-0405/]

For regression protection we also check for conda environments that do not include pyenv.cfg yet --
https://github.com/conda/conda/issues/13337 is the conda issue tracking adding pyenv.cfg.

Checking for the `conda-meta/history` file per https://github.com/pytest-dev/pytest/issues/12652#issuecomment-2246336902.

"""
try:
return path.joinpath("pyvenv.cfg").is_file()
return (
path.joinpath("pyvenv.cfg").is_file()
or path.joinpath("conda-meta", "history").is_file()
)
except OSError:
return False

Expand Down
28 changes: 20 additions & 8 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
from pathlib import Path
from pathlib import PurePath
import pprint
import shutil
import sys
Expand Down Expand Up @@ -152,8 +153,17 @@ def test_ignored_certain_directories(self, pytester: Pytester) -> None:
assert "test_notfound" not in s
assert "test_found" in s

def test_ignored_virtualenvs(self, pytester: Pytester) -> None:
ensure_file(pytester.path / "virtual" / "pyvenv.cfg")
known_environment_types = pytest.mark.parametrize(
"env_path",
[
pytest.param(PurePath("pyvenv.cfg"), id="venv"),
pytest.param(PurePath("conda-meta", "history"), id="conda"),
],
)

@known_environment_types
def test_ignored_virtualenvs(self, pytester: Pytester, env_path: PurePath) -> None:
ensure_file(pytester.path / "virtual" / env_path)
testfile = ensure_file(pytester.path / "virtual" / "test_invenv.py")
testfile.write_text("def test_hello(): pass", encoding="utf-8")

Expand All @@ -167,11 +177,12 @@ def test_ignored_virtualenvs(self, pytester: Pytester) -> None:
result = pytester.runpytest("virtual")
assert "test_invenv" in result.stdout.str()

@known_environment_types
def test_ignored_virtualenvs_norecursedirs_precedence(
self, pytester: Pytester
self, pytester: Pytester, env_path
) -> None:
# norecursedirs takes priority
ensure_file(pytester.path / ".virtual" / "pyvenv.cfg")
ensure_file(pytester.path / ".virtual" / env_path)
testfile = ensure_file(pytester.path / ".virtual" / "test_invenv.py")
testfile.write_text("def test_hello(): pass", encoding="utf-8")
result = pytester.runpytest("--collect-in-virtualenv")
Expand All @@ -180,13 +191,14 @@ def test_ignored_virtualenvs_norecursedirs_precedence(
result = pytester.runpytest("--collect-in-virtualenv", ".virtual")
assert "test_invenv" in result.stdout.str()

def test__in_venv(self, pytester: Pytester) -> None:
@known_environment_types
def test__in_venv(self, pytester: Pytester, env_path: PurePath) -> None:
"""Directly test the virtual env detection function"""
# no pyvenv.cfg, not a virtualenv
# no env path, not a env
base_path = pytester.mkdir("venv")
assert _in_venv(base_path) is False
# with pyvenv.cfg, totally a virtualenv
base_path.joinpath("pyvenv.cfg").touch()
# with env path, totally a env
ensure_file(base_path.joinpath(env_path))
assert _in_venv(base_path) is True

def test_custom_norecursedirs(self, pytester: Pytester) -> None:
Expand Down
Loading