Skip to content

Commit

Permalink
explicitly detect conda envs - fixes pytest-dev#12652
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RonnyPfannschmidt committed Jul 23, 2024
1 parent 8061af8 commit d0f7884
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
4 changes: 4 additions & 0 deletions changelog/12652.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Resolve the regression in conda environment detection by
explicitly expanding virtualenv detection to conda environents

-- by :user:`RonnyPfannschmidt`
13 changes: 11 additions & 2 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,18 @@ 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
"""
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

0 comments on commit d0f7884

Please sign in to comment.