Skip to content

Commit

Permalink
Merge pull request #12656 from RonnyPfannschmidt/fix-12652-detect-con…
Browse files Browse the repository at this point in the history
…da-env

(cherry picked from commit 6c806b4)
  • Loading branch information
RonnyPfannschmidt authored and patchback[bot] committed Jul 24, 2024
1 parent ae6034a commit 238bad2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
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

0 comments on commit 238bad2

Please sign in to comment.