Skip to content

Commit d0f7884

Browse files
explicitly detect conda envs - fixes #12652
initially we accidentially detected conda environmnts by just testing too many files after steamlining we no longer detected conda environments now we explicitly detect conda environments and test for support
1 parent 8061af8 commit d0f7884

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

changelog/12652.bugfix.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Resolve the regression in conda environment detection by
2+
explicitly expanding virtualenv detection to conda environents
3+
4+
-- by :user:`RonnyPfannschmidt`

src/_pytest/main.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,18 @@ 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+
"""
374380
try:
375-
return path.joinpath("pyvenv.cfg").is_file()
381+
return (
382+
path.joinpath("pyvenv.cfg").is_file()
383+
or path.joinpath("conda-meta", "history").is_file()
384+
)
376385
except OSError:
377386
return False
378387

testing/test_collection.py

Lines changed: 20 additions & 8 deletions
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)