From d0f7884a1d11ac08a20af3ddf0a17bc4990c4784 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 23 Jul 2024 23:43:59 +0200 Subject: [PATCH 1/3] 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 --- changelog/12652.bugfix.rst | 4 ++++ src/_pytest/main.py | 13 +++++++++++-- testing/test_collection.py | 28 ++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 changelog/12652.bugfix.rst diff --git a/changelog/12652.bugfix.rst b/changelog/12652.bugfix.rst new file mode 100644 index 00000000000..78398212ceb --- /dev/null +++ b/changelog/12652.bugfix.rst @@ -0,0 +1,4 @@ +Resolve the regression in conda environment detection by +explicitly expanding virtualenv detection to conda environents + +-- by :user:`RonnyPfannschmidt` diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 8ec26906003..0ec3377163e 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -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 diff --git a/testing/test_collection.py b/testing/test_collection.py index f5822240335..aba8f8ea48d 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -3,6 +3,7 @@ import os from pathlib import Path +from pathlib import PurePath import pprint import shutil import sys @@ -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") @@ -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") @@ -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: From fa915f6aa8e5c5aa223efd23b44248cf4e6e4582 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Wed, 24 Jul 2024 07:48:58 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Bruno Oliveira --- changelog/12652.bugfix.rst | 3 +-- src/_pytest/main.py | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/changelog/12652.bugfix.rst b/changelog/12652.bugfix.rst index 78398212ceb..da7644df06d 100644 --- a/changelog/12652.bugfix.rst +++ b/changelog/12652.bugfix.rst @@ -1,4 +1,3 @@ -Resolve the regression in conda environment detection by -explicitly expanding virtualenv detection to conda environents +Resolve regression `conda` environments where no longer being automatically detected. -- by :user:`RonnyPfannschmidt` diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 0ec3377163e..226693805f7 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -373,8 +373,10 @@ def _in_venv(path: Path) -> bool: [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 + 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: From 1f7c917f07716d861235770e0eeef642c0bddf33 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 24 Jul 2024 05:51:11 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 226693805f7..befc7ccce6e 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -373,9 +373,9 @@ def _in_venv(path: Path) -> bool: [https://peps.python.org/pep-0405/] - For regression protection we also check for conda environments that do not include pyenv.cfg yet -- + 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. """