From e121498f331436ff80318b27fe5bfebdd7abfe1c Mon Sep 17 00:00:00 2001 From: Harry Moss Date: Tue, 1 Dec 2026 09:46:24 +0000 Subject: [PATCH 1/3] Remove `pytest-venv` - replaces `pytest-venv` with a venv fixture and a bunch of subprocess calls --- pyproject.toml | 2 -- tests/conftest.py | 12 +++++++++++ tests/test_package_generation.py | 34 +++++++++++++++++++++++--------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4e0cedc3..d896e0aa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,6 @@ [dependency-groups] typechecking = [ "pytest", - "pytest-venv", ] [tool.pytest.ini_options] @@ -61,7 +60,6 @@ env_run_base = {commands = [ ], deps = [ "cookiecutter", "pytest", - "pytest-venv", ], description = "Test package creation", skip_install = true} gh.python = {"3.11" = [ "py311", diff --git a/tests/conftest.py b/tests/conftest.py index 5fd2c878..b6e9a93e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,12 +1,24 @@ """Fixtures for the cookiecutter template tests.""" +import os import pathlib import subprocess import typing +import venv import pytest +@pytest.fixture(scope="function") # noqa: PT003 +def test_venv(tmp_path: pathlib.Path) -> pathlib.Path: + """Create an isolated virtual environment for tests.""" + environment = tmp_path / "venv" + venv.create(environment, with_pip=True) + # Handle Windows environments as an exception that uses Scripts + # instead of bin + return environment / ("Scripts" if os.name == "nt" else "bin") + + @pytest.fixture(scope="function") # noqa: PT003 def default_config() -> dict[str, str]: """ diff --git a/tests/test_package_generation.py b/tests/test_package_generation.py index 3bde76b0..9175de27 100644 --- a/tests/test_package_generation.py +++ b/tests/test_package_generation.py @@ -5,7 +5,6 @@ import typing import pytest -import pytest_venv # type: ignore[import-not-found] def test_package_generation( @@ -43,15 +42,15 @@ def test_package_generation( def test_pip_installable( - venv: pytest_venv.VirtualEnvironment, generate_package: typing.Callable, + test_venv: pathlib.Path, ) -> None: """Test generated package is pip installable.""" _, test_project_dir = generate_package() # Try to install package in virtual environment with pip pipinstall = subprocess.run( # noqa: S603 [ - venv.python, + pathlib.Path(test_venv) / "python", "-m", "pip", "install", @@ -67,15 +66,20 @@ def test_pip_installable( def test_uv_installable( - venv: pytest_venv.VirtualEnvironment, generate_package: typing.Callable, + test_venv: pathlib.Path, ) -> None: """Test generated package is uv installable.""" _, test_project_dir = generate_package() - venv.install("uv", upgrade=True) + # Try to install uv in the virtual environment + subprocess.run( # noqa: S603 + [pathlib.Path(test_venv) / "python", "-m", "pip", "install", "uv", "--upgrade"], + capture_output=True, + check=False, + ) # Try to install the package in virtual environment with uv uvsync = subprocess.run( # noqa: S603 - [pathlib.Path(venv.bin) / "uv", "sync"], + [pathlib.Path(test_venv) / "uv", "sync"], cwd=test_project_dir, capture_output=True, check=False, @@ -107,15 +111,27 @@ def test_optional_funder( def test_docs_build( - venv: pytest_venv.VirtualEnvironment, + test_venv: pathlib.Path, generate_package: typing.Callable, ) -> None: """Test documentation build from package created from template.""" _, test_project_dir = generate_package() - venv.install("tox") + # Tox install + subprocess.run( # noqa: S603 + [ + pathlib.Path(test_venv) / "python", + "-m", + "pip", + "install", + "tox", + "--upgrade", + ], + capture_output=True, + check=False, + ) tox_docs_process = subprocess.run( # noqa: S603 [ - pathlib.Path(venv.bin) / "tox", + pathlib.Path(test_venv) / "tox", "-e", "docs", ], From 79feed5c0584996d33424b1d13f683d3e95cc1a9 Mon Sep 17 00:00:00 2001 From: Harry Moss Date: Tue, 1 Dec 2026 10:07:03 +0000 Subject: [PATCH 2/3] Stop tox upgrading to keep old behaviour --- tests/test_package_generation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_package_generation.py b/tests/test_package_generation.py index 9175de27..953b48de 100644 --- a/tests/test_package_generation.py +++ b/tests/test_package_generation.py @@ -124,7 +124,6 @@ def test_docs_build( "pip", "install", "tox", - "--upgrade", ], capture_output=True, check=False, From 59d9e784976555e2f65111354284f22ad5c95ab0 Mon Sep 17 00:00:00 2001 From: Harry Moss Date: Tue, 1 Dec 2026 10:08:19 +0000 Subject: [PATCH 3/3] test_venv -> temp_venv - make it clear that the fixture isn't a test --- tests/conftest.py | 4 ++-- tests/test_package_generation.py | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b6e9a93e..d0918cf5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,8 +10,8 @@ @pytest.fixture(scope="function") # noqa: PT003 -def test_venv(tmp_path: pathlib.Path) -> pathlib.Path: - """Create an isolated virtual environment for tests.""" +def temp_venv(tmp_path: pathlib.Path) -> pathlib.Path: + """Create a temporary isolated virtual environment for tests.""" environment = tmp_path / "venv" venv.create(environment, with_pip=True) # Handle Windows environments as an exception that uses Scripts diff --git a/tests/test_package_generation.py b/tests/test_package_generation.py index 953b48de..50009da0 100644 --- a/tests/test_package_generation.py +++ b/tests/test_package_generation.py @@ -43,14 +43,14 @@ def test_package_generation( def test_pip_installable( generate_package: typing.Callable, - test_venv: pathlib.Path, + temp_venv: pathlib.Path, ) -> None: """Test generated package is pip installable.""" _, test_project_dir = generate_package() # Try to install package in virtual environment with pip pipinstall = subprocess.run( # noqa: S603 [ - pathlib.Path(test_venv) / "python", + pathlib.Path(temp_venv) / "python", "-m", "pip", "install", @@ -67,19 +67,19 @@ def test_pip_installable( def test_uv_installable( generate_package: typing.Callable, - test_venv: pathlib.Path, + temp_venv: pathlib.Path, ) -> None: """Test generated package is uv installable.""" _, test_project_dir = generate_package() # Try to install uv in the virtual environment subprocess.run( # noqa: S603 - [pathlib.Path(test_venv) / "python", "-m", "pip", "install", "uv", "--upgrade"], + [pathlib.Path(temp_venv) / "python", "-m", "pip", "install", "uv", "--upgrade"], capture_output=True, check=False, ) # Try to install the package in virtual environment with uv uvsync = subprocess.run( # noqa: S603 - [pathlib.Path(test_venv) / "uv", "sync"], + [pathlib.Path(temp_venv) / "uv", "sync"], cwd=test_project_dir, capture_output=True, check=False, @@ -111,7 +111,7 @@ def test_optional_funder( def test_docs_build( - test_venv: pathlib.Path, + temp_venv: pathlib.Path, generate_package: typing.Callable, ) -> None: """Test documentation build from package created from template.""" @@ -119,7 +119,7 @@ def test_docs_build( # Tox install subprocess.run( # noqa: S603 [ - pathlib.Path(test_venv) / "python", + pathlib.Path(temp_venv) / "python", "-m", "pip", "install", @@ -130,7 +130,7 @@ def test_docs_build( ) tox_docs_process = subprocess.run( # noqa: S603 [ - pathlib.Path(test_venv) / "tox", + pathlib.Path(temp_venv) / "tox", "-e", "docs", ],