Skip to content

Commit 238bad2

Browse files
RonnyPfannschmidtpatchback[bot]
authored andcommitted
Merge pull request #12656 from RonnyPfannschmidt/fix-12652-detect-conda-env
(cherry picked from commit 6c806b4)
1 parent ae6034a commit 238bad2

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

Diff for: changelog/12652.bugfix.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Resolve regression `conda` environments where no longer being automatically detected.
2+
3+
-- by :user:`RonnyPfannschmidt`

Diff for: src/_pytest/main.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,20 @@ def pytest_runtestloop(session: Session) -> bool:
370370
def _in_venv(path: Path) -> bool:
371371
"""Attempt to detect if ``path`` is the root of a Virtual Environment by
372372
checking for the existence of the pyvenv.cfg file.
373-
[https://peps.python.org/pep-0405/]"""
373+
374+
[https://peps.python.org/pep-0405/]
375+
376+
For regression protection we also check for conda environments that do not include pyenv.cfg yet --
377+
https://github.com/conda/conda/issues/13337 is the conda issue tracking adding pyenv.cfg.
378+
379+
Checking for the `conda-meta/history` file per https://github.com/pytest-dev/pytest/issues/12652#issuecomment-2246336902.
380+
381+
"""
374382
try:
375-
return path.joinpath("pyvenv.cfg").is_file()
383+
return (
384+
path.joinpath("pyvenv.cfg").is_file()
385+
or path.joinpath("conda-meta", "history").is_file()
386+
)
376387
except OSError:
377388
return False
378389

Diff for: testing/test_collection.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
from pathlib import Path
6+
from pathlib import PurePath
67
import pprint
78
import shutil
89
import sys
@@ -152,8 +153,17 @@ def test_ignored_certain_directories(self, pytester: Pytester) -> None:
152153
assert "test_notfound" not in s
153154
assert "test_found" in s
154155

155-
def test_ignored_virtualenvs(self, pytester: Pytester) -> None:
156-
ensure_file(pytester.path / "virtual" / "pyvenv.cfg")
156+
known_environment_types = pytest.mark.parametrize(
157+
"env_path",
158+
[
159+
pytest.param(PurePath("pyvenv.cfg"), id="venv"),
160+
pytest.param(PurePath("conda-meta", "history"), id="conda"),
161+
],
162+
)
163+
164+
@known_environment_types
165+
def test_ignored_virtualenvs(self, pytester: Pytester, env_path: PurePath) -> None:
166+
ensure_file(pytester.path / "virtual" / env_path)
157167
testfile = ensure_file(pytester.path / "virtual" / "test_invenv.py")
158168
testfile.write_text("def test_hello(): pass", encoding="utf-8")
159169

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

180+
@known_environment_types
170181
def test_ignored_virtualenvs_norecursedirs_precedence(
171-
self, pytester: Pytester
182+
self, pytester: Pytester, env_path
172183
) -> None:
173184
# norecursedirs takes priority
174-
ensure_file(pytester.path / ".virtual" / "pyvenv.cfg")
185+
ensure_file(pytester.path / ".virtual" / env_path)
175186
testfile = ensure_file(pytester.path / ".virtual" / "test_invenv.py")
176187
testfile.write_text("def test_hello(): pass", encoding="utf-8")
177188
result = pytester.runpytest("--collect-in-virtualenv")
@@ -180,13 +191,14 @@ def test_ignored_virtualenvs_norecursedirs_precedence(
180191
result = pytester.runpytest("--collect-in-virtualenv", ".virtual")
181192
assert "test_invenv" in result.stdout.str()
182193

183-
def test__in_venv(self, pytester: Pytester) -> None:
194+
@known_environment_types
195+
def test__in_venv(self, pytester: Pytester, env_path: PurePath) -> None:
184196
"""Directly test the virtual env detection function"""
185-
# no pyvenv.cfg, not a virtualenv
197+
# no env path, not a env
186198
base_path = pytester.mkdir("venv")
187199
assert _in_venv(base_path) is False
188-
# with pyvenv.cfg, totally a virtualenv
189-
base_path.joinpath("pyvenv.cfg").touch()
200+
# with env path, totally a env
201+
ensure_file(base_path.joinpath(env_path))
190202
assert _in_venv(base_path) is True
191203

192204
def test_custom_norecursedirs(self, pytester: Pytester) -> None:

0 commit comments

Comments
 (0)